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

PLR2004: Accept 0.0 and 1.0 as common magic values #9964

Merged
merged 3 commits into from
Feb 13, 2024

Conversation

augustelalande
Copy link
Contributor

Summary

Accept 0.0 and 1.0 as common magic values. This is in line with the pylint behaviour, and I think makes sense conceptually.

Test Plan

Test cases were added to crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py

Copy link
Contributor

github-actions bot commented Feb 12, 2024

ruff-ecosystem results

Linter (stable)

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

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

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

- tests/core/test_configuration.py:229:16: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/jobs/test_scheduler_job.py:4941:24: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/jobs/test_scheduler_job.py:4966:24: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/providers/google/cloud/transfers/test_cassandra_to_gcs.py:87:41: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/utils/test_sqlalchemy.py:89:62: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/utils/test_sqlalchemy.py:90:58: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable

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

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

- src/bokeh/colors/color.py:321:22: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/colors/color.py:337:21: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/colors/color.py:460:22: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/core/property/numeric.py:280:12: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- src/bokeh/core/property/numeric.py:280:28: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/palettes.py:1621:17: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/plotting/contour.py:414:53: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:107:23: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:156:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:164:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:194:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:211:23: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:283:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:333:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:341:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:362:63: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:365:63: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:85:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_named.py:189:19: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/_util_models.py:121:30: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/_util_models.py:130:30: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:155:37: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:497:32: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:498:38: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:547:28: PLR2004 Magic value used in comparison, consider replacing `-1.` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:551:25: PLR2004 Magic value used in comparison, consider replacing `-1.` with a constant variable
- tests/unit/bokeh/models/test_glyphs.py:356:34: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_ranges.py:160:37: PLR2004 Magic value used in comparison, consider replacing `-1.0` with a constant variable
- tests/unit/bokeh/models/test_ranges.py:71:33: PLR2004 Magic value used in comparison, consider replacing `-1.0` with a constant variable

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR2004 35 0 35 0 0

Linter (preview)

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

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

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

- tests/core/test_configuration.py:229:16: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/jobs/test_scheduler_job.py:4941:24: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/jobs/test_scheduler_job.py:4966:24: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/providers/google/cloud/transfers/test_cassandra_to_gcs.py:87:41: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/utils/test_sqlalchemy.py:89:62: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/utils/test_sqlalchemy.py:90:58: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable

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

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

- src/bokeh/colors/color.py:321:22: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/colors/color.py:337:21: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/colors/color.py:460:22: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/core/property/numeric.py:280:12: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- src/bokeh/core/property/numeric.py:280:28: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/palettes.py:1621:17: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- src/bokeh/plotting/contour.py:414:53: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:107:23: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:156:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:164:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:194:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:211:23: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:283:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:333:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:341:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:362:63: PLR2004 Magic value used in comparison, consider replacing `0.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:365:63: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_color__colors.py:85:24: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/colors/test_named.py:189:19: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/_util_models.py:121:30: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/_util_models.py:130:30: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:155:37: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:497:32: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:498:38: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:547:28: PLR2004 Magic value used in comparison, consider replacing `-1.` with a constant variable
- tests/unit/bokeh/models/test_annotations.py:551:25: PLR2004 Magic value used in comparison, consider replacing `-1.` with a constant variable
- tests/unit/bokeh/models/test_glyphs.py:356:34: PLR2004 Magic value used in comparison, consider replacing `1.0` with a constant variable
- tests/unit/bokeh/models/test_ranges.py:160:37: PLR2004 Magic value used in comparison, consider replacing `-1.0` with a constant variable
- tests/unit/bokeh/models/test_ranges.py:71:33: PLR2004 Magic value used in comparison, consider replacing `-1.0` with a constant variable

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
PLR2004 35 0 35 0 0

@charliermarsh
Copy link
Member

I think this is a good change and arguably a "bug" (or at least an oversight) given that we already allow 0 and 1. Wdyt @zanieb (with respect to gating this in preview vs. not)?

Copy link
Member

@zanieb zanieb left a comment

Choose a reason for hiding this comment

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

Seems reasonable without preview — looks like a bug.

@charliermarsh charliermarsh added the bug Something isn't working label Feb 13, 2024
@charliermarsh charliermarsh enabled auto-merge (squash) February 13, 2024 01:13
@charliermarsh charliermarsh merged commit 8fba97f into astral-sh:main Feb 13, 2024
17 checks passed
@augustelalande augustelalande deleted the magic-values branch March 7, 2024 02:22
nkxxll pushed a commit to nkxxll/ruff that referenced this pull request Mar 10, 2024
## Summary

Accept 0.0 and 1.0 as common magic values. This is in line with the
pylint behaviour, and I think makes sense conceptually.


## Test Plan

Test cases were added to
`crates/ruff_linter/resources/test/fixtures/pylint/magic_value_comparison.py`
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.

3 participants