Skip to content

DTZ rules: Clarify error messages and docs#10621

Merged
AlexWaygood merged 7 commits intoastral-sh:mainfrom
AlexWaygood:dtz-clarify
Mar 27, 2024
Merged

DTZ rules: Clarify error messages and docs#10621
AlexWaygood merged 7 commits intoastral-sh:mainfrom
AlexWaygood:dtz-clarify

Conversation

@AlexWaygood
Copy link
Copy Markdown
Member

@AlexWaygood AlexWaygood commented Mar 26, 2024

Fixes #10251. Closes #10590.

Summary

This PR builds upon #10590, by @cclauss. The problem with DTZ005 pointed out in #10251 is really a common problem to most of the DTZ rules, so it makes sense to tackle them all at the same time.

Changes made:

  • Clearly state in the documentation that passing tz=None is just as bad as not passing a tz= argument, from the perspective of these rules.
  • Clearly state in the error messages exactly what the user is doing wrong, if the user is passing tz=None rather than failing to pass a tz= argument at all.
  • Make error messages more concise, and separate out the suggested remedy from the thing that the user is identified as doing wrong.

Test Plan

cargo test


Co-authored-by: Christian Clauss cclauss@me.com

@AlexWaygood AlexWaygood requested a review from MichaReiser March 26, 2024 18:53
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2024

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+868 -868 violations, +0 -0 fixes in 3 projects; 41 projects unchanged)

apache/airflow (+797 -797 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --no-preview --select ALL

- airflow/dag_processing/manager.py:415:62: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
+ airflow/dag_processing/manager.py:415:62: DTZ006 `datetime.datetime.fromtimestamp()` called without a `tz` argument
- airflow/dag_processing/manager.py:419:68: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
+ airflow/dag_processing/manager.py:419:68: DTZ006 `datetime.datetime.fromtimestamp()` called without a `tz` argument
- airflow/example_dags/example_dynamic_task_mapping.py:27:60: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ airflow/example_dags/example_dynamic_task_mapping.py:27:60: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- airflow/example_dags/example_dynamic_task_mapping_with_no_taskflow_operators.py:56:16: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ airflow/example_dags/example_dynamic_task_mapping_with_no_taskflow_operators.py:56:16: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- airflow/example_dags/example_latest_only.py:31:16: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ airflow/example_dags/example_latest_only.py:31:16: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- airflow/example_dags/example_local_kubernetes_executor.py:48:20: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
... 1280 additional changes omitted for rule DTZ001
- airflow/example_dags/example_params_ui_tutorial.py:145:16: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ airflow/example_dags/example_params_ui_tutorial.py:145:16: DTZ011 `datetime.date.today()` used
- airflow/example_dags/example_params_ui_tutorial.py:152:16: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ airflow/example_dags/example_params_ui_tutorial.py:152:16: DTZ011 `datetime.date.today()` used
+ airflow/macros/__init__.py:59:10: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
- airflow/macros/__init__.py:59:10: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)` or `.astimezone()`
+ airflow/macros/__init__.py:76:12: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
- airflow/macros/__init__.py:76:12: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)` or `.astimezone()`
- airflow/models/dagbag.py:298:41: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
+ airflow/models/dagbag.py:298:41: DTZ006 `datetime.datetime.fromtimestamp()` called without a `tz` argument
- airflow/providers/amazon/aws/hooks/s3.py:699:34: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ airflow/providers/amazon/aws/hooks/s3.py:699:34: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- airflow/providers/amazon/aws/hooks/s3.py:714:38: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ airflow/providers/amazon/aws/hooks/s3.py:714:38: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- airflow/providers/amazon/aws/hooks/s3.py:738:34: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ airflow/providers/amazon/aws/hooks/s3.py:738:34: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- airflow/providers/amazon/aws/sensors/s3.py:291:39: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
... 194 additional changes omitted for rule DTZ005
+ airflow/providers/apache/hive/macros/hive.py:113:18: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
- airflow/providers/apache/hive/macros/hive.py:113:18: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)` or `.astimezone()`
+ airflow/providers/apache/hive/macros/hive.py:114:21: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
... 34 additional changes omitted for rule DTZ007
- airflow/providers/apache/kylin/operators/kylin_cube.py:147:22: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
... 22 additional changes omitted for rule DTZ006
- airflow/providers/google/cloud/operators/cloud_storage_transfer_service.py:108:61: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ airflow/providers/google/cloud/operators/cloud_storage_transfer_service.py:108:61: DTZ011 `datetime.date.today()` used
- airflow/providers/google/cloud/operators/cloud_storage_transfer_service.py:108:94: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
... 6 additional changes omitted for rule DTZ011
- airflow/sensors/time_sensor.py:70:39: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ airflow/sensors/time_sensor.py:70:39: DTZ002 `datetime.datetime.today()` used
- airflow/utils/log/file_processor_handler.py:52:26: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ airflow/utils/log/file_processor_handler.py:52:26: DTZ002 `datetime.datetime.today()` used
- airflow/utils/log/file_processor_handler.py:68:29: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ airflow/utils/log/file_processor_handler.py:68:29: DTZ002 `datetime.datetime.today()` used
- airflow/utils/log/file_processor_handler.py:70:30: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
... 20 additional changes omitted for rule DTZ002
- tests/providers/sftp/triggers/test_sftp.py:152:38: DTZ012 The use of `datetime.date.fromtimestamp()` is not allowed, use `datetime.datetime.fromtimestamp(ts, tz=).date()` instead
+ tests/providers/sftp/triggers/test_sftp.py:152:38: DTZ012 `datetime.date.fromtimestamp()` used
... 1550 additional changes omitted for project

bokeh/bokeh (+70 -70 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --no-preview --select ALL

- docs/bokeh/source/conf.py:21:8: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ docs/bokeh/source/conf.py:21:8: DTZ011 `datetime.date.today()` used
- docs/bokeh/source/docs/first_steps/examples/first_steps_4_datetime_axis.py:8:11: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ docs/bokeh/source/docs/first_steps/examples/first_steps_4_datetime_axis.py:8:11: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- examples/basic/annotations/polygon_annotation.py:17:14: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ examples/basic/annotations/polygon_annotation.py:17:14: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- examples/basic/annotations/polygon_annotation.py:20:12: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ examples/basic/annotations/polygon_annotation.py:20:12: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- examples/basic/annotations/span.py:18:27: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ examples/basic/annotations/span.py:18:27: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
... 105 additional changes omitted for rule DTZ001
... 130 additional changes omitted for project

rotki/rotki (+1 -1 violations, +0 -0 fixes)

- packaging/docker/entrypoint.py:26:20: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ packaging/docker/entrypoint.py:26:20: DTZ002 `datetime.datetime.today()` used

Changes by rule (7 rules affected)

code total + violation - violation + fix - fix
DTZ001 1396 698 698 0 0
DTZ005 218 109 109 0 0
DTZ007 40 20 20 0 0
DTZ006 34 17 17 0 0
DTZ002 30 15 15 0 0
DTZ011 16 8 8 0 0
DTZ012 2 1 1 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+868 -868 violations, +0 -0 fixes in 3 projects; 41 projects unchanged)

apache/airflow (+797 -797 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

- airflow/dag_processing/manager.py:415:62: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
+ airflow/dag_processing/manager.py:415:62: DTZ006 `datetime.datetime.fromtimestamp()` called without a `tz` argument
- airflow/dag_processing/manager.py:419:68: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
+ airflow/dag_processing/manager.py:419:68: DTZ006 `datetime.datetime.fromtimestamp()` called without a `tz` argument
- airflow/example_dags/example_dynamic_task_mapping.py:27:60: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ airflow/example_dags/example_dynamic_task_mapping.py:27:60: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- airflow/example_dags/example_dynamic_task_mapping_with_no_taskflow_operators.py:56:16: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ airflow/example_dags/example_dynamic_task_mapping_with_no_taskflow_operators.py:56:16: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- airflow/example_dags/example_latest_only.py:31:16: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ airflow/example_dags/example_latest_only.py:31:16: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- airflow/example_dags/example_local_kubernetes_executor.py:48:20: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
... 1280 additional changes omitted for rule DTZ001
- airflow/example_dags/example_params_ui_tutorial.py:145:16: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ airflow/example_dags/example_params_ui_tutorial.py:145:16: DTZ011 `datetime.date.today()` used
- airflow/example_dags/example_params_ui_tutorial.py:152:16: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ airflow/example_dags/example_params_ui_tutorial.py:152:16: DTZ011 `datetime.date.today()` used
+ airflow/macros/__init__.py:59:10: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
- airflow/macros/__init__.py:59:10: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)` or `.astimezone()`
+ airflow/macros/__init__.py:76:12: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
- airflow/macros/__init__.py:76:12: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)` or `.astimezone()`
- airflow/models/dagbag.py:298:41: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
+ airflow/models/dagbag.py:298:41: DTZ006 `datetime.datetime.fromtimestamp()` called without a `tz` argument
- airflow/providers/amazon/aws/hooks/s3.py:699:34: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ airflow/providers/amazon/aws/hooks/s3.py:699:34: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- airflow/providers/amazon/aws/hooks/s3.py:714:38: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ airflow/providers/amazon/aws/hooks/s3.py:714:38: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- airflow/providers/amazon/aws/hooks/s3.py:738:34: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ airflow/providers/amazon/aws/hooks/s3.py:738:34: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- airflow/providers/amazon/aws/sensors/s3.py:291:39: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
... 194 additional changes omitted for rule DTZ005
+ airflow/providers/apache/hive/macros/hive.py:113:18: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
- airflow/providers/apache/hive/macros/hive.py:113:18: DTZ007 The use of `datetime.datetime.strptime()` without %z must be followed by `.replace(tzinfo=)` or `.astimezone()`
+ airflow/providers/apache/hive/macros/hive.py:114:21: DTZ007 Naive datetime constructed using `datetime.datetime.strptime()` without %z
... 34 additional changes omitted for rule DTZ007
- airflow/providers/apache/kylin/operators/kylin_cube.py:147:22: DTZ006 The use of `datetime.datetime.fromtimestamp()` without `tz` argument is not allowed
... 22 additional changes omitted for rule DTZ006
- airflow/providers/google/cloud/operators/cloud_storage_transfer_service.py:108:61: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ airflow/providers/google/cloud/operators/cloud_storage_transfer_service.py:108:61: DTZ011 `datetime.date.today()` used
- airflow/providers/google/cloud/operators/cloud_storage_transfer_service.py:108:94: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
... 6 additional changes omitted for rule DTZ011
- airflow/sensors/time_sensor.py:70:39: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ airflow/sensors/time_sensor.py:70:39: DTZ002 `datetime.datetime.today()` used
- airflow/utils/log/file_processor_handler.py:52:26: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ airflow/utils/log/file_processor_handler.py:52:26: DTZ002 `datetime.datetime.today()` used
- airflow/utils/log/file_processor_handler.py:68:29: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ airflow/utils/log/file_processor_handler.py:68:29: DTZ002 `datetime.datetime.today()` used
- airflow/utils/log/file_processor_handler.py:70:30: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
... 20 additional changes omitted for rule DTZ002
- tests/providers/sftp/triggers/test_sftp.py:152:38: DTZ012 The use of `datetime.date.fromtimestamp()` is not allowed, use `datetime.datetime.fromtimestamp(ts, tz=).date()` instead
+ tests/providers/sftp/triggers/test_sftp.py:152:38: DTZ012 `datetime.date.fromtimestamp()` used
... 1550 additional changes omitted for project

bokeh/bokeh (+70 -70 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL

- docs/bokeh/source/conf.py:21:8: DTZ011 The use of `datetime.date.today()` is not allowed, use `datetime.datetime.now(tz=).date()` instead
+ docs/bokeh/source/conf.py:21:8: DTZ011 `datetime.date.today()` used
- docs/bokeh/source/docs/first_steps/examples/first_steps_4_datetime_axis.py:8:11: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
+ docs/bokeh/source/docs/first_steps/examples/first_steps_4_datetime_axis.py:8:11: DTZ005 `datetime.datetime.now()` called without a `tz` argument
- examples/basic/annotations/polygon_annotation.py:17:14: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ examples/basic/annotations/polygon_annotation.py:17:14: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- examples/basic/annotations/polygon_annotation.py:20:12: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ examples/basic/annotations/polygon_annotation.py:20:12: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
- examples/basic/annotations/span.py:18:27: DTZ001 The use of `datetime.datetime()` without `tzinfo` argument is not allowed
+ examples/basic/annotations/span.py:18:27: DTZ001 `datetime.datetime()` called without a `tzinfo` argument
... 105 additional changes omitted for rule DTZ001
... 130 additional changes omitted for project

rotki/rotki (+1 -1 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview

- packaging/docker/entrypoint.py:26:20: DTZ002 The use of `datetime.datetime.today()` is not allowed, use `datetime.datetime.now(tz=)` instead
+ packaging/docker/entrypoint.py:26:20: DTZ002 `datetime.datetime.today()` used

Changes by rule (7 rules affected)

code total + violation - violation + fix - fix
DTZ001 1396 698 698 0 0
DTZ005 218 109 109 0 0
DTZ007 40 20 20 0 0
DTZ006 34 17 17 0 0
DTZ002 30 15 15 0 0
DTZ011 16 8 8 0 0
DTZ012 2 1 1 0 0

@AlexWaygood AlexWaygood added documentation Improvements or additions to documentation rule Implementing or modifying a lint rule labels Mar 26, 2024
Copy link
Copy Markdown
Contributor

@cclauss cclauss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@AlexWaygood AlexWaygood enabled auto-merge (squash) March 27, 2024 19:34
@AlexWaygood AlexWaygood merged commit abbefae into astral-sh:main Mar 27, 2024
@AlexWaygood AlexWaygood deleted the dtz-clarify branch July 1, 2024 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DTZ005 false postitive for tz=None

4 participants