-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement PLR2004
(MagicValueComparison
)
#1828
Conversation
This looks great! Couple comments to get it fully aligned with the Pylint implementation. Speaking of: how do you enable this check with Pylint? I notice it's part of an "extension". Trying to figure out how to test it locally... |
To test R2004 with pylint you will need to follow their instructions for Contributor installation. Once the basic installation is complete you can run the following to confirm you are on the main branch. $ pylint --version
pylint 2.16.0-dev Once installed you can run the following command to test R2004 (with the flag documented here) $ pylint --load-plugins=pylint.extensions.magic_value ./magic_value_comparison.py | grep R2004
<PATH>/ruff/resources/test/fixtures/pylint/magic_value_comparison.py:6:3: R2004: Consider using a named constant or an enum instead of '10'. (magic-value-comparison)
<PATH>/ruff/resources/test/fixtures/pylint/magic_value_comparison.py:29:3: R2004: Consider using a named constant or an enum instead of '2'. (magic-value-comparison)
<PATH>/ruff/resources/test/fixtures/pylint/magic_value_comparison.py:44:3: R2004: Consider using a named constant or an enum instead of 'Hunter2'. (magic-value-comparison)
<PATH>/ruff/resources/test/fixtures/pylint/magic_value_comparison.py:50:3: R2004: Consider using a named constant or an enum instead of '3.141592653589793'. (magic-value-comparison)
<PATH>/ruff/resources/test/fixtures/pylint/magic_value_comparison.py:59:3: R2004: Consider using a named constant or an enum instead of 'b'something''. (magic-value-comparison) Hope this helps! Current resources/test/fixtures/pylint/magic_value_comparison.py:6:4: PLR2004 Magic number used in comparison, consider replacing 10 with a constant variable
resources/test/fixtures/pylint/magic_value_comparison.py:29:4: PLR2004 Magic number used in comparison, consider replacing 2 with a constant variable
resources/test/fixtures/pylint/magic_value_comparison.py:44:4: PLR2004 Magic number used in comparison, consider replacing 'Hunter2' with a constant variable
resources/test/fixtures/pylint/magic_value_comparison.py:50:4: PLR2004 Magic number used in comparison, consider replacing 3.141592653589793 with a constant variable
resources/test/fixtures/pylint/magic_value_comparison.py:59:4: PLR2004 Magic number used in comparison, consider replacing b'something' with a constant variable
Found 5 error(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, thank you! So helpful.
I think there are still a few false-positives here:
if x == 3:
pass
if 1 == 3:
pass
if 4 == 3 == x:
pass
Ruff gives:
❯ cargo run foo.py --select PLR2004 -n
Finished dev [unoptimized + debuginfo] target(s) in 0.14s
Running `target/debug/ruff foo.py --select PLR2004 -n`
foo.py:1:4: PLR2004 Magic number used in comparison, consider replacing 3 with a constant variable
foo.py:4:4: PLR2004 Magic number used in comparison, consider replacing 3 with a constant variable
foo.py:7:4: PLR2004 Magic number used in comparison, consider replacing 3 with a constant variable
foo.py:7:4: PLR2004 Magic number used in comparison, consider replacing 4 with a constant variable
Found 4 error(s).
Whereas Pylint gives:
❯ pylint --load-plugins=pylint.extensions.magic_value foo.py | grep R2004
foo.py:1:3: R2004: Consider using a named constant or an enum instead of '3'. (magic-value-comparison)
I think this comes down to: (1) for the purpose of skipping, Pylint considers 1
and such to be constants, even if they don't get flagged as magic, whereas the current implementation here does not (since it relies on the number of diagnostics generated); and (2) it looks like Pylint actually skips if there's any constant comparison, not just if they're all constant.
Let me know if you would prefer the 2nd for loop to be refactored into an iterator, have left as is for now. |
PLR2004
(MagicValueComparison
)
Thank you! Running CI now. Will merge on pass. |
Cheers! Appreciate you being so responsive to PRs and a credit to |
Thank you, that's awesome to hear! (\cc @not-my-profile as he's made some refactors lately that, IMO, made adding rules a lot more accessible.) |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://togithub.com/charliermarsh/ruff) | `^0.0.220` -> `^0.0.221` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.221/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.221/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.221/compatibility-slim/0.0.220)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.221/confidence-slim/0.0.220)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.221`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.221) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.220...v0.0.221) #### What's Changed - Document the way extend-ignore/select are applied by [@​jankatins](https://togithub.com/jankatins) in [https://github.com/charliermarsh/ruff/pull/1839](https://togithub.com/charliermarsh/ruff/pull/1839) - Implement `PLR2004` (`MagicValueComparison`) by [@​max0x53](https://togithub.com/max0x53) in [https://github.com/charliermarsh/ruff/pull/1828](https://togithub.com/charliermarsh/ruff/pull/1828) - Use absolute paths for --stdin-filename matching by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1843](https://togithub.com/charliermarsh/ruff/pull/1843) - \[`flake8-bugbear`] Fix False Positives for `B024` & `B027` by [@​saadmk11](https://togithub.com/saadmk11) in [https://github.com/charliermarsh/ruff/pull/1851](https://togithub.com/charliermarsh/ruff/pull/1851) - Clarify that some flake8-bugbear opinionated rules are already implemented by [@​nsoranzo](https://togithub.com/nsoranzo) in [https://github.com/charliermarsh/ruff/pull/1847](https://togithub.com/charliermarsh/ruff/pull/1847) - \[`isort`] Add `classes` Config Option by [@​saadmk11](https://togithub.com/saadmk11) in [https://github.com/charliermarsh/ruff/pull/1849](https://togithub.com/charliermarsh/ruff/pull/1849) - Implement `PLR0133` (`ComparisonOfConstants`) by [@​max0x53](https://togithub.com/max0x53) in [https://github.com/charliermarsh/ruff/pull/1841](https://togithub.com/charliermarsh/ruff/pull/1841) - Remove non-magic trailing comma from tuple by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1854](https://togithub.com/charliermarsh/ruff/pull/1854) - Improve spacing preservation for `C405` fixes by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1855](https://togithub.com/charliermarsh/ruff/pull/1855) - Refactor import-tracking to leverage existing AST bindings by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1856](https://togithub.com/charliermarsh/ruff/pull/1856) - Split off ruff_cli crate from ruff library by [@​not-my-profile](https://togithub.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1816](https://togithub.com/charliermarsh/ruff/pull/1816) - Added ALE by [@​colin99d](https://togithub.com/colin99d) in [https://github.com/charliermarsh/ruff/pull/1857](https://togithub.com/charliermarsh/ruff/pull/1857) - Add workaround for wasm-pack bug to fix the playground CI by [@​not-my-profile](https://togithub.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1861](https://togithub.com/charliermarsh/ruff/pull/1861) - Actually fix wasm-pack build command by [@​not-my-profile](https://togithub.com/not-my-profile) in [https://github.com/charliermarsh/ruff/pull/1862](https://togithub.com/charliermarsh/ruff/pull/1862) - Avoid unnecessary allocations for module names by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/1863](https://togithub.com/charliermarsh/ruff/pull/1863) #### New Contributors - [@​jankatins](https://togithub.com/jankatins) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1839](https://togithub.com/charliermarsh/ruff/pull/1839) - [@​max0x53](https://togithub.com/max0x53) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1828](https://togithub.com/charliermarsh/ruff/pull/1828) - [@​nsoranzo](https://togithub.com/nsoranzo) made their first contribution in [https://github.com/charliermarsh/ruff/pull/1847](https://togithub.com/charliermarsh/ruff/pull/1847) **Full Changelog**: astral-sh/ruff@v0.0.220...v0.0.221 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/ixm-one/pytest-cmake-presets). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMDIuMCIsInVwZGF0ZWRJblZlciI6IjM0LjEwMi4wIn0=--> Signed-off-by: Renovate Bot <bot@renovateapp.com> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Now how do I ignore this lint? Because when I put Edit: nvm, it does have an effect, Edit 2: The linting was based on the version of ruff that comes with the VSCode extension where this lint wasn't implemented yet. So yes, it was a problem on my end |
(We have to update the JSON Schema manually in the Schema Store, unfortunately, so it sometimes gets a little out-of-date with latest Ruff.) |
Feature request: #1949 |
This PR adds Pylint
R2004
Feel free to suggest changes and additions, I have tried to maintain parity with the Pylint implementation
magic_value.py
See #970