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

Implement PLR2004 (MagicValueComparison) #1828

Merged
merged 4 commits into from
Jan 13, 2023
Merged

Conversation

max0x53
Copy link
Contributor

@max0x53 max0x53 commented Jan 12, 2023

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

@charliermarsh
Copy link
Member

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...

@max0x53
Copy link
Contributor Author

max0x53 commented Jan 12, 2023

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 ruff output for completeness

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).

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.

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.

src/pylint/rules/magic_value_comparison.rs Outdated Show resolved Hide resolved
@max0x53
Copy link
Contributor Author

max0x53 commented Jan 12, 2023

Let me know if you would prefer the 2nd for loop to be refactored into an iterator, have left as is for now.

@charliermarsh charliermarsh changed the title Implement PLR2004 Implement PLR2004 (MagicValueComparison) Jan 12, 2023
@charliermarsh
Copy link
Member

Thank you! Running CI now. Will merge on pass.

@max0x53
Copy link
Contributor Author

max0x53 commented Jan 12, 2023

Cheers! Appreciate you being so responsive to PRs and a credit to ruff for being as easy as it was to add new linting rules (docs and code were great)!

@charliermarsh
Copy link
Member

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.)

@charliermarsh charliermarsh merged commit b47e8e6 into astral-sh:main Jan 13, 2023
@max0x53 max0x53 deleted the plr2004 branch January 13, 2023 00:47
renovate bot referenced this pull request in ixm-one/pytest-cmake-presets Jan 14, 2023
[![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
[@&#8203;jankatins](https://togithub.com/jankatins) in
[https://github.com/charliermarsh/ruff/pull/1839](https://togithub.com/charliermarsh/ruff/pull/1839)
- Implement `PLR2004` (`MagicValueComparison`) by
[@&#8203;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
[@&#8203;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
[@&#8203;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 [@&#8203;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
[@&#8203;saadmk11](https://togithub.com/saadmk11) in
[https://github.com/charliermarsh/ruff/pull/1849](https://togithub.com/charliermarsh/ruff/pull/1849)
- Implement `PLR0133` (`ComparisonOfConstants`) by
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;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 [@&#8203;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
[@&#8203;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
[@&#8203;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
[@&#8203;charliermarsh](https://togithub.com/charliermarsh) in
[https://github.com/charliermarsh/ruff/pull/1863](https://togithub.com/charliermarsh/ruff/pull/1863)

#### New Contributors

- [@&#8203;jankatins](https://togithub.com/jankatins) made their first
contribution in
[https://github.com/charliermarsh/ruff/pull/1839](https://togithub.com/charliermarsh/ruff/pull/1839)
- [@&#8203;max0x53](https://togithub.com/max0x53) made their first
contribution in
[https://github.com/charliermarsh/ruff/pull/1828](https://togithub.com/charliermarsh/ruff/pull/1828)
- [@&#8203;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>
@martinkozle
Copy link

martinkozle commented Jan 16, 2023

Now how do I ignore this lint? Because when I put ignore = ["PLR2004"] in pyproject.toml it says that it is not a valid option.

Edit: nvm, it does have an effect, Better TOML VSCode extension just says that it is not a valid option, I don't know if the problem is on my end

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

@charliermarsh
Copy link
Member

(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.)

@ofek
Copy link
Contributor

ofek commented Jan 18, 2023

Feature request: #1949

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants