Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pylint] Show verbatim constant in magic-value-comparison (PLR2004) #9694

Merged
merged 1 commit into from
Jan 30, 2024

Conversation

diceroll123
Copy link
Contributor

Summary

Tweaks PLR2004 to show the literal source text, rather than the constant value.

I noticed this when I had a hexadecimal constant, and the linter turned it into base-10.

Now, if you have 0x300, it will show 0x300 instead of 768.

Also, added backticks around the constant in the output message.

Test Plan

cargo test

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+3007 -3007 violations, +0 -0 fixes in 4 projects; 39 projects unchanged)

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

ruff check --no-cache --exit-zero --no-preview --select ALL

- airflow/api_connexion/security.py:50:36: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/api_connexion/security.py:50:36: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/api_internal/internal_api_call.py:104:36: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/api_internal/internal_api_call.py:104:36: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/cli/commands/kubernetes_command.py:83:30: PLR2004 Magic value used in comparison, consider replacing 5 with a constant variable
+ airflow/cli/commands/kubernetes_command.py:83:30: PLR2004 Magic value used in comparison, consider replacing `5` with a constant variable
- airflow/configuration.py:2196:37: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ airflow/configuration.py:2196:37: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- airflow/example_dags/example_kubernetes_executor.py:135:28: PLR2004 Magic value used in comparison, consider replacing 4 with a constant variable
+ airflow/example_dags/example_kubernetes_executor.py:135:28: PLR2004 Magic value used in comparison, consider replacing `4` with a constant variable
- airflow/executors/base_executor.py:449:27: PLR2004 Magic value used in comparison, consider replacing 3 with a constant variable
+ airflow/executors/base_executor.py:449:27: PLR2004 Magic value used in comparison, consider replacing `3` with a constant variable
- airflow/jobs/triggerer_job_runner.py:481:49: PLR2004 Magic value used in comparison, consider replacing 60 with a constant variable
+ airflow/jobs/triggerer_job_runner.py:481:49: PLR2004 Magic value used in comparison, consider replacing `60` with a constant variable
- airflow/jobs/triggerer_job_runner.py:577:31: PLR2004 Magic value used in comparison, consider replacing 0.2 with a constant variable
+ airflow/jobs/triggerer_job_runner.py:577:31: PLR2004 Magic value used in comparison, consider replacing `0.2` with a constant variable
- airflow/kubernetes/pre_7_4_0_compatibility/pod_generator.py:401:26: PLR2004 Magic value used in comparison, consider replacing 253 with a constant variable
+ airflow/kubernetes/pre_7_4_0_compatibility/pod_generator.py:401:26: PLR2004 Magic value used in comparison, consider replacing `253` with a constant variable
- airflow/models/connection.py:230:35: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ airflow/models/connection.py:230:35: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- airflow/models/connection.py:232:54: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ airflow/models/connection.py:232:54: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- airflow/providers/airbyte/hooks/airbyte.py:189:35: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/providers/airbyte/hooks/airbyte.py:189:35: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/providers/amazon/aws/hooks/base_aws.py:261:40: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/providers/amazon/aws/hooks/base_aws.py:261:40: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/providers/amazon/aws/hooks/base_aws.py:870:50: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/providers/amazon/aws/hooks/base_aws.py:870:50: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/providers/amazon/aws/hooks/chime.py:88:27: PLR2004 Magic value used in comparison, consider replacing 4096 with a constant variable
+ airflow/providers/amazon/aws/hooks/chime.py:88:27: PLR2004 Magic value used in comparison, consider replacing `4096` with a constant variable
- airflow/providers/amazon/aws/hooks/emr.py:168:62: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
... 3763 additional changes omitted for project

aws/aws-sam-cli (+13 -13 violations, +0 -0 fixes)

- tests/integration/testdata/remote_invoke/lambda-fns/main.py:37:26: PLR2004 Magic value used in comparison, consider replacing 100 with a constant variable
+ tests/integration/testdata/remote_invoke/lambda-fns/main.py:37:26: PLR2004 Magic value used in comparison, consider replacing `100` with a constant variable
- tests/smoke/download_sar_templates.py:19:47: PLR2004 Magic value used in comparison, consider replacing 10 with a constant variable
+ tests/smoke/download_sar_templates.py:19:47: PLR2004 Magic value used in comparison, consider replacing `10` with a constant variable
- tests/smoke/download_sar_templates.py:24:50: PLR2004 Magic value used in comparison, consider replacing 10 with a constant variable
+ tests/smoke/download_sar_templates.py:24:50: PLR2004 Magic value used in comparison, consider replacing `10` with a constant variable
- tests/unit/lib/bootstrap/companion_stack/test_companion_stack_manager.py:127:39: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ tests/unit/lib/bootstrap/companion_stack/test_companion_stack_manager.py:127:39: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- tests/unit/lib/package/test_artifact_exporter.py:1849:70: PLR2004 Magic value used in comparison, consider replacing 32841 with a constant variable
+ tests/unit/lib/package/test_artifact_exporter.py:1849:70: PLR2004 Magic value used in comparison, consider replacing `0o100111` with a constant variable
... 16 additional changes omitted for project

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

ruff check --no-cache --exit-zero --no-preview --select ALL

- examples/basic/data/filter_boolean.py:7:26: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ examples/basic/data/filter_boolean.py:7:26: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- examples/basic/data/linked_brushing_subsets.py:13:50: PLR2004 Magic value used in comparison, consider replacing 250 with a constant variable
+ examples/basic/data/linked_brushing_subsets.py:13:50: PLR2004 Magic value used in comparison, consider replacing `250` with a constant variable
- examples/basic/data/linked_brushing_subsets.py:13:61: PLR2004 Magic value used in comparison, consider replacing 100 with a constant variable
+ examples/basic/data/linked_brushing_subsets.py:13:61: PLR2004 Magic value used in comparison, consider replacing `100` with a constant variable
- examples/basic/scatters/elements.py:21:50: PLR2004 Magic value used in comparison, consider replacing 82 with a constant variable
+ examples/basic/scatters/elements.py:21:50: PLR2004 Magic value used in comparison, consider replacing `82` with a constant variable
- examples/interaction/tools/hover_tooltips_image.py:7:36: PLR2004 Magic value used in comparison, consider replacing 0.5 with a constant variable
+ examples/interaction/tools/hover_tooltips_image.py:7:36: PLR2004 Magic value used in comparison, consider replacing `0.5` with a constant variable
- examples/models/trail.py:44:46: PLR2004 Magic value used in comparison, consider replacing 4 with a constant variable
+ examples/models/trail.py:44:46: PLR2004 Magic value used in comparison, consider replacing `4` with a constant variable
- examples/models/trail.py:45:31: PLR2004 Magic value used in comparison, consider replacing 4 with a constant variable
+ examples/models/trail.py:45:31: PLR2004 Magic value used in comparison, consider replacing `4` with a constant variable
- examples/models/trail.py:45:46: PLR2004 Magic value used in comparison, consider replacing 6 with a constant variable
... 1845 additional changes omitted for project

zulip/zulip (+167 -167 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --no-preview --select ALL

- analytics/lib/counts.py:466:23: PLR2004 Magic value used in comparison, consider replacing 60 with a constant variable
+ analytics/lib/counts.py:466:23: PLR2004 Magic value used in comparison, consider replacing `60` with a constant variable
- analytics/lib/fixtures.py:58:17: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ analytics/lib/fixtures.py:58:17: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- analytics/tests/test_activity_views.py:303:20: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ analytics/tests/test_activity_views.py:303:20: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- analytics/views/installation_activity.py:241:48: PLR2004 Magic value used in comparison, consider replacing 5 with a constant variable
+ analytics/views/installation_activity.py:241:48: PLR2004 Magic value used in comparison, consider replacing `5` with a constant variable
- analytics/views/support.py:219:25: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ analytics/views/support.py:219:25: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
... 324 additional changes omitted for project

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR2004 6014 3007 3007 0 0

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+3007 -3007 violations, +0 -0 fixes in 4 projects; 39 projects unchanged)

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

ruff check --no-cache --exit-zero --preview --select ALL

- airflow/api_connexion/security.py:50:36: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/api_connexion/security.py:50:36: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/api_internal/internal_api_call.py:104:36: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/api_internal/internal_api_call.py:104:36: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/cli/commands/kubernetes_command.py:83:30: PLR2004 Magic value used in comparison, consider replacing 5 with a constant variable
+ airflow/cli/commands/kubernetes_command.py:83:30: PLR2004 Magic value used in comparison, consider replacing `5` with a constant variable
- airflow/configuration.py:2196:37: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ airflow/configuration.py:2196:37: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- airflow/example_dags/example_kubernetes_executor.py:135:28: PLR2004 Magic value used in comparison, consider replacing 4 with a constant variable
+ airflow/example_dags/example_kubernetes_executor.py:135:28: PLR2004 Magic value used in comparison, consider replacing `4` with a constant variable
- airflow/executors/base_executor.py:449:27: PLR2004 Magic value used in comparison, consider replacing 3 with a constant variable
+ airflow/executors/base_executor.py:449:27: PLR2004 Magic value used in comparison, consider replacing `3` with a constant variable
- airflow/jobs/triggerer_job_runner.py:481:49: PLR2004 Magic value used in comparison, consider replacing 60 with a constant variable
+ airflow/jobs/triggerer_job_runner.py:481:49: PLR2004 Magic value used in comparison, consider replacing `60` with a constant variable
- airflow/jobs/triggerer_job_runner.py:577:31: PLR2004 Magic value used in comparison, consider replacing 0.2 with a constant variable
+ airflow/jobs/triggerer_job_runner.py:577:31: PLR2004 Magic value used in comparison, consider replacing `0.2` with a constant variable
- airflow/kubernetes/pre_7_4_0_compatibility/pod_generator.py:401:26: PLR2004 Magic value used in comparison, consider replacing 253 with a constant variable
+ airflow/kubernetes/pre_7_4_0_compatibility/pod_generator.py:401:26: PLR2004 Magic value used in comparison, consider replacing `253` with a constant variable
- airflow/models/connection.py:230:35: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ airflow/models/connection.py:230:35: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- airflow/models/connection.py:232:54: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ airflow/models/connection.py:232:54: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- airflow/providers/airbyte/hooks/airbyte.py:189:35: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/providers/airbyte/hooks/airbyte.py:189:35: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/providers/amazon/aws/hooks/base_aws.py:261:40: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/providers/amazon/aws/hooks/base_aws.py:261:40: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/providers/amazon/aws/hooks/base_aws.py:870:50: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
+ airflow/providers/amazon/aws/hooks/base_aws.py:870:50: PLR2004 Magic value used in comparison, consider replacing `200` with a constant variable
- airflow/providers/amazon/aws/hooks/chime.py:88:27: PLR2004 Magic value used in comparison, consider replacing 4096 with a constant variable
+ airflow/providers/amazon/aws/hooks/chime.py:88:27: PLR2004 Magic value used in comparison, consider replacing `4096` with a constant variable
- airflow/providers/amazon/aws/hooks/emr.py:168:62: PLR2004 Magic value used in comparison, consider replacing 200 with a constant variable
... 3763 additional changes omitted for project

aws/aws-sam-cli (+13 -13 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview

- tests/integration/testdata/remote_invoke/lambda-fns/main.py:37:26: PLR2004 Magic value used in comparison, consider replacing 100 with a constant variable
+ tests/integration/testdata/remote_invoke/lambda-fns/main.py:37:26: PLR2004 Magic value used in comparison, consider replacing `100` with a constant variable
- tests/smoke/download_sar_templates.py:19:47: PLR2004 Magic value used in comparison, consider replacing 10 with a constant variable
+ tests/smoke/download_sar_templates.py:19:47: PLR2004 Magic value used in comparison, consider replacing `10` with a constant variable
- tests/smoke/download_sar_templates.py:24:50: PLR2004 Magic value used in comparison, consider replacing 10 with a constant variable
+ tests/smoke/download_sar_templates.py:24:50: PLR2004 Magic value used in comparison, consider replacing `10` with a constant variable
- tests/unit/lib/bootstrap/companion_stack/test_companion_stack_manager.py:127:39: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ tests/unit/lib/bootstrap/companion_stack/test_companion_stack_manager.py:127:39: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- tests/unit/lib/package/test_artifact_exporter.py:1849:70: PLR2004 Magic value used in comparison, consider replacing 32841 with a constant variable
+ tests/unit/lib/package/test_artifact_exporter.py:1849:70: PLR2004 Magic value used in comparison, consider replacing `0o100111` with a constant variable
... 16 additional changes omitted for project

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

ruff check --no-cache --exit-zero --preview --select ALL

- examples/basic/data/filter_boolean.py:7:26: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ examples/basic/data/filter_boolean.py:7:26: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- examples/basic/data/linked_brushing_subsets.py:13:50: PLR2004 Magic value used in comparison, consider replacing 250 with a constant variable
+ examples/basic/data/linked_brushing_subsets.py:13:50: PLR2004 Magic value used in comparison, consider replacing `250` with a constant variable
- examples/basic/data/linked_brushing_subsets.py:13:61: PLR2004 Magic value used in comparison, consider replacing 100 with a constant variable
+ examples/basic/data/linked_brushing_subsets.py:13:61: PLR2004 Magic value used in comparison, consider replacing `100` with a constant variable
- examples/basic/scatters/elements.py:21:50: PLR2004 Magic value used in comparison, consider replacing 82 with a constant variable
+ examples/basic/scatters/elements.py:21:50: PLR2004 Magic value used in comparison, consider replacing `82` with a constant variable
- examples/interaction/tools/hover_tooltips_image.py:7:36: PLR2004 Magic value used in comparison, consider replacing 0.5 with a constant variable
+ examples/interaction/tools/hover_tooltips_image.py:7:36: PLR2004 Magic value used in comparison, consider replacing `0.5` with a constant variable
- examples/models/trail.py:44:46: PLR2004 Magic value used in comparison, consider replacing 4 with a constant variable
+ examples/models/trail.py:44:46: PLR2004 Magic value used in comparison, consider replacing `4` with a constant variable
- examples/models/trail.py:45:31: PLR2004 Magic value used in comparison, consider replacing 4 with a constant variable
+ examples/models/trail.py:45:31: PLR2004 Magic value used in comparison, consider replacing `4` with a constant variable
- examples/models/trail.py:45:46: PLR2004 Magic value used in comparison, consider replacing 6 with a constant variable
... 1845 additional changes omitted for project

zulip/zulip (+167 -167 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --preview --select ALL

- analytics/lib/counts.py:466:23: PLR2004 Magic value used in comparison, consider replacing 60 with a constant variable
+ analytics/lib/counts.py:466:23: PLR2004 Magic value used in comparison, consider replacing `60` with a constant variable
- analytics/lib/fixtures.py:58:17: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ analytics/lib/fixtures.py:58:17: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- analytics/tests/test_activity_views.py:303:20: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ analytics/tests/test_activity_views.py:303:20: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
- analytics/views/installation_activity.py:241:48: PLR2004 Magic value used in comparison, consider replacing 5 with a constant variable
+ analytics/views/installation_activity.py:241:48: PLR2004 Magic value used in comparison, consider replacing `5` with a constant variable
- analytics/views/support.py:219:25: PLR2004 Magic value used in comparison, consider replacing 2 with a constant variable
+ analytics/views/support.py:219:25: PLR2004 Magic value used in comparison, consider replacing `2` with a constant variable
... 324 additional changes omitted for project

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR2004 6014 3007 3007 0 0

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Thanks! Makes sense to me.

@charliermarsh charliermarsh added the bug Something isn't working label Jan 30, 2024
@charliermarsh charliermarsh changed the title [pylint] - improve PLR2004 (magic-value-comparison) to show the constant itself instead of the value [pylint] Improve magic-value-comparison (PLR2004) to show verbatim constant Jan 30, 2024
@charliermarsh charliermarsh changed the title [pylint] Improve magic-value-comparison (PLR2004) to show verbatim constant [pylint] Show verbatim constant in magic-value-comparison (PLR2004) Jan 30, 2024
@charliermarsh charliermarsh merged commit dacda0f into astral-sh:main Jan 30, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants