Skip to content

[pylint] Add a configurable allowlist for magic-value-comparison (PLR2004) - #18961

Merged
ntBre merged 27 commits into
astral-sh:mainfrom
DaniBodor:allow_magic_values
Oct 16, 2026
Merged

ntBre merged 27 commits into
astral-sh:mainfrom
DaniBodor:allow_magic_values

Conversation

@DaniBodor

@DaniBodor DaniBodor commented Jun 26, 2025 •

Copy link
Copy Markdown
Contributor

Summary

This PR allows users to specify a custom set of whitelisted values for magic value comparisons in PLR2004. The setting is named pylint.allow_magic_values, which is consistent with a similar setting pylint.allow_magic_value_types
The previous hard-coded allow-list was moved to the default for pylint.allow_magic_values, giving users full control over what is and what is not a whitelisted value. This setting is added in addition to the pylint.allow_magic_value_types and does not replace it. Any value that is whitelisted due to either of these settings is not considered a linting violation.

Note that as far as I could tell, no other setting allows floats or complex numbers as inputs, so I created a few helper methods to deal with these properly (mainly relating to floating point variation).

Test Plan

I have tested the settings with a number of cases manually in a repository I work on regularly. I have not added any extensive (combinatorial) tests to the test suite.

Note

I don't know whether you have any policy on using or disclosing the use of code generators, but I definitely relied heavily on this to solve some of the issues I ran into during development. Ultimately, all code was reviewed by me before pushing.

fix: #10009

@DaniBodor
DaniBodor force-pushed the allow_magic_values branch 2 times, most recently from 9ec2b5a to 3664fae Compare June 26, 2025 15:53
@github-actions

github-actions Bot commented Jun 26, 2025 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Linter (stable)

βœ… ecosystem check detected no linter changes.

Linter (preview)

βœ… ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the configuration Related to settings and configuration of Ruff or ty label Jun 27, 2025
@MichaReiser

Copy link
Copy Markdown
Member

@DaniBodor any chance you could look at the failing tests so that we could move this PR across the line?

@DaniBodor

Copy link
Copy Markdown
Contributor Author

Yes.. I'm sorry, I haven't had much time recently. Will pick it up within the next couple of weeks.

@marciska

marciska commented Dec 5, 2025

Copy link
Copy Markdown

Is there any update on this? πŸ™‚

@DaniBodor

Copy link
Copy Markdown
Contributor Author

Hi. I'm sorry for the radio silence.
Due to life events, I have not been able to work on this in quite a while.

I now have some limited availability again, so can start picking this up again, but am hesitant to bite off more than I can chew atm, so I may need some help moving this forward.

@DaniBodor

Copy link
Copy Markdown
Contributor Author

Apologies again for leaving this open for so long.
Given the time delay, some conflicts with main appeared. I've now rebased this off the new main and addressed two out of three points raised by @MichaReiser, and resolved all the issues with failing checks.
I can now start looking into the remaining comment as well.

@ntBre

ntBre commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

No problem at all! Thanks for rebasing and for your continued work on this. Will you re-request my review when you're ready? Micha is out on vacation, but I'm happy to review :)

@carljm
carljm removed their request for review January 28, 2026 16:51
@ntBre

ntBre commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

I think something may have gone slightly awry with a merge or rebase. I'm seeing some modified ty files, which also prompted the code owner review requests. Let me know if you need any help sorting that out!

@DaniBodor

Copy link
Copy Markdown
Contributor Author

I think something may have gone slightly awry with a merge or rebase. I'm seeing some modified ty files, which also prompted the code owner review requests. Let me know if you need any help sorting that out!

Help would be appreciated.
The files in the last commit (which added the ty files) got auto-generated/-edited by running the test suite, as instructed to do in the contribution instructions. Without that, the tests fail locally for me.

@ntBre

ntBre commented Jan 28, 2026

Copy link
Copy Markdown
Contributor

Huh, that's strange. It doesn't look like a merge issue to me. Are you running the tests inside of a virtual environment by any chance? Maybe even a conda environment? I wonder if that is leaking into the tests. I'm mostly guessing since it looks like additional LSP diagnostics are showing up offering to add imports like import anaconda_cli_base.deprecations.deprecated.

I can certainly update the snapshots on my computer to pass CI, but I expect you'll run into the same problem on future iterations, so it may be worth tracking down.

@DaniBodor

Copy link
Copy Markdown
Contributor Author

Are you running the tests inside of a virtual environment by any chance? Maybe even a conda environment? I wonder if that is leaking into the tests. I'm mostly guessing since it looks like additional LSP diagnostics are showing up offering to add imports like import anaconda_cli_base.deprecations.deprecated.

Yup, that was the issue. When leaving the conda env the tests passed fine without updating ty snapshots.

@DaniBodor

DaniBodor commented Jan 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Getting back to the content of this PR:

I've addressed all the issues raised in the previous review. I've also added 2 additional features to this setting, that were not part of the original PR.
First, separate options an option to extend the default allow list or to create a fully custom allow list (or just use the default, obviously) - 072f99b.
Second (this one I am not positive is actually wanted, so please tell me your thoughts), cross recognizing of whole-number floats and integers. By this I mean that if for example the float 2.0 is on the allow list then the int 2 will also be accepted, and vice versa - 82daee0.

@DaniBodor
DaniBodor requested a review from ntBre January 28, 2026 22:09
@DaniBodor
DaniBodor force-pushed the allow_magic_values branch from 8e6b935 to 67983b3 Compare August 6, 2026 09:41
@DaniBodor
DaniBodor requested review from a team and dhruvmanila as code owners August 6, 2026 13:31
@astral-sh-bot

astral-sh-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected βœ…

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 96.96%. The percentage of expected errors that received a diagnostic held steady at 92.96%. The number of fully passing files held steady at 106/133.

@astral-sh-bot

astral-sh-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged βœ…

@astral-sh-bot

astral-sh-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected βœ…

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@DaniBodor

DaniBodor commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm leaning toward avoiding the complexity of the numeric comparisons for now. See my inline comment for a little more detail.

Hi @ntBre ,
I finally had some time on my hands once again to look back into this. Given that there's been quite some changes to ruff since my last edits, I decided to rebase the entire branch off of the most recent main ruff branch. I had to do some manual conflict fixes, but am reasonably confident it all went ok.

I agree that cross-checking ints against floats and vice versa probably adds excessive complexity for how useful it is. In fact, I could even imagine cases where a user does want to white-list the one but not the other. So indeed probably best to remove it. I reverted the the code that does the cross-checking in the last few commits.

When pushing the newest changes, I keep running into failed CI checks for which I can't figure out what is wrong. Locally, when I run any of the diagnostics listed in the dev documentation it passes just fine.
Any chance you can help me diagnose this? FYI, 588afd4 was added in response to this failed check on the previous commit, but seems to have auto-triggered additional reviewers to this PR (once again).

@DaniBodor
DaniBodor requested a review from ntBre August 6, 2026 13:46
@ntBre

ntBre commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Thanks for the update!

The CI failure I'm seeing is for ruff_dev::bin/ruff_dev generate_json_schema::tests::test_generate_json_schema, which means you need to run the cargo dev generate-all command suggested (way earlier πŸ˜…) in the output. The suggestion to run cargo insta test --force-update-snapshots is to fix a warning that won't break CI, so we should drop that commit for now.

The contributing docs you linked to are for ty. This is probably the better reference for Ruff, including the mention of cargo dev generate-all: https://github.com/astral-sh/ruff/blob/main/CONTRIBUTING.md#example-adding-a-new-configuration-option.

@dhruvmanila
dhruvmanila removed their request for review August 11, 2026 09:05
@DaniBodor

Copy link
Copy Markdown
Contributor Author

Thanks for the update!

The CI failure I'm seeing is for ruff_dev::bin/ruff_dev generate_json_schema::tests::test_generate_json_schema, which means you need to run the cargo dev generate-all command suggested (way earlier πŸ˜…) in the output. The suggestion to run cargo insta test --force-update-snapshots is to fix a warning that won't break CI, so we should drop that commit for now.

The contributing docs you linked to are for ty. This is probably the better reference for Ruff, including the mention of cargo dev generate-all: https://github.com/astral-sh/ruff/blob/main/CONTRIBUTING.md#example-adding-a-new-configuration-option.

Thanks for pointing me to the correct contribution guidelines, not sure how I ended up on the ty guide, it's been the one I was using from the start, which explains why similar things happened a few time 🀦.

I now dumped the force-update-snapshots commit and instead ran the json schema update and that was indeed all that was required to make the tests pass.
This PR is now ready for review again

@DaniBodor

DaniBodor commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor Author

There was a comment on the original issue regarding the name of these rules that hasn't been addressed in this PR or the discussion yet.

#10009 (comment) says:

If the rule's named magic-value-comparison (PLR2004) then an option lint.pylint.allow-magic-values seems appropriate.
There's already lint.pylint.allow-magic-value-types, so "allow" rather than "allowed" and "values" rather than "value" would keep a similar pattern.

Although that does bring into question whether lint.pylint.allow-magic-value-types and lint.pylint.allow-dunder-method-names should have been called allowed-* because it's a list of options, not a boolean. As the pattern other rule options seem to follow.

I agree that using allowed-* for all these settings is semantically better, but I guess the question is whether it is worth creating a breaking change with the previous existing rule-name.

EDIT:
Another point that hasn't been addressed (yet) from the issue that this is based off, is whether 2 and 3 should be part of the default allowed list. This was discussed prior to proposing to create an adaptable setting. I think that it would make sense to add 2, but not 3. I won't touch the PR now, in case that would make it move down your to do list, but if you agree I'll add it in the next round of changes.

@DaniBodor

Copy link
Copy Markdown
Contributor Author

@ntBre , just checking in as it's been 2 months since the last updates to this PR and I'm waiting for a review. I have the feeling this PR is close to complete, but new conflicts keep popping up so it would be nice to try and finalize it :)

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you, and sorry for the delay! This looks good to me. I pushed one commit adding a few tests and making a couple of minor simplifications.

@ntBre ntBre changed the title [magic-value-comparison] Custom allowlist for Magic Numbers (PLR2004) [pylint] Add a configurable allowlist for magic-value-comparison (PLR2004) Oct 16, 2026
@ntBre
ntBre merged commit 16bc1bc into astral-sh:main Oct 16, 2026
115 of 118 checks passed
rlichtenwalter added a commit to rlichtenwalter/dir2text that referenced this pull request Oct 30, 2026
This PR contains the following updates:

| Package | Type | Update | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|---|---|
| [DavidAnson/markdownlint-cli2](https://github.com/DavidAnson/markdownlint-cli2) | repository | minor | `v0.23.3` β†’ `v0.24.0` | ![age](https://developer.mend.io/api/mc/badges/age/github-tags/DavidAnson%2fmarkdownlint-cli2/v0.24.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/github-tags/DavidAnson%2fmarkdownlint-cli2/v0.23.3/v0.24.0?slim=true) |
| [ruff](https://docs.astral.sh/ruff) ([source](https://github.com/astral-sh/ruff), [changelog](https://github.com/astral-sh/ruff/blob/main/CHANGELOG.md)) | dependency-groups | patch | `==0.17.1` β†’ `==0.17.3` | ![age](https://developer.mend.io/api/mc/badges/age/pypi/ruff/0.17.3?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/ruff/0.17.1/0.17.3?slim=true) |

Note: The `pre-commit` manager in Renovate is not supported by the `pre-commit` maintainers or community. Please do not report any problems there, instead [create a Discussion in the Renovate repository](https://github.com/renovatebot/renovate/discussions/new) if you have any questions.

---

### Release Notes

<details>
<summary>DavidAnson/markdownlint-cli2</summary>

### [`v0.24.0`](DavidAnson/markdownlint-cli2@v0.23.3...v0.24.0)

[Compare Source](DavidAnson/markdownlint-cli2@v0.23.3...v0.24.0)

</details>

<details>
<summary>astral-sh/ruff (ruff)</summary>

### [`v0.17.3`](https://github.com/astral-sh/ruff/blob/HEAD/CHANGELOG.md#0173)

[Compare Source](astral-sh/ruff@0.17.2...0.17.3)

Released on 2026-10-26.

##### Preview features

- \[`suspicious`] Add `shared-clock-mock` ([#&#8203;29074](astral-sh/ruff#29074))

##### Bug fixes

- \[`pyupgrade`] Avoid incorrect conversions to f-strings (`UP032`) ([#&#8203;25485](astral-sh/ruff#25485))

##### Performance

- Buffer stdout in `ruff format` ([#&#8203;29631](astral-sh/ruff#29631))
- Specialize ASCII identifier lexing ([#&#8203;29503](astral-sh/ruff#29503))
- Speed up identifier and keyword lexing ([#&#8203;29463](astral-sh/ruff#29463))

##### Documentation

- \[`flake8-bandit`] Describe what the rule actually checks for `httpx` (`S113`) ([#&#8203;27936](astral-sh/ruff#27936))

##### Contributors

- [@&#8203;itsvedantkumar](https://github.com/itsvedantkumar)
- [@&#8203;charliermarsh](https://github.com/charliermarsh)
- [@&#8203;anishgirianish](https://github.com/anishgirianish)

### [`v0.17.2`](https://github.com/astral-sh/ruff/blob/HEAD/CHANGELOG.md#0172)

[Compare Source](astral-sh/ruff@0.17.1...0.17.2)

Released on 2026-10-22.

##### Preview features

- \[`complexity`] Add `unnecessary-regular-expression-compile` ([#&#8203;25533](astral-sh/ruff#25533))

##### Bug fixes

- Avoid syntax errors for type variables with starred constraints (`UP040`, `UP046`, `UP047`, `RUF053`) ([#&#8203;28362](astral-sh/ruff#28362))
- \[`pydocstyle`] Treat backslash-escaped `*args` and `**kwargs` as documented (`D417`) ([#&#8203;29536](astral-sh/ruff#29536))
- \[`pylint`] Avoid false positive on starred elements (`PLE0643`) ([#&#8203;29483](astral-sh/ruff#29483))
- \[`pylint`] Mark fixes unsafe when deleting comments (`PLR5501`, `PLW0120`) ([#&#8203;29414](astral-sh/ruff#29414))
- \[`pyupgrade`] Mark fix unsafe when deleting comments (`UP035`) ([#&#8203;29413](astral-sh/ruff#29413))

##### Performance

- Avoid intermediate names in lint name resolution ([#&#8203;29506](astral-sh/ruff#29506))
- Prefilter bidirectional Unicode checks by UTF-8 leading bytes ([#&#8203;29561](astral-sh/ruff#29561))
- Share postfix parsing and reduce expression copies ([#&#8203;29480](astral-sh/ruff#29480))

##### Configuration

- \[`pylint`] Add a configurable allowlist for `magic-value-comparison` (`PLR2004`) ([#&#8203;18961](astral-sh/ruff#18961))

##### Documentation

- Clarify documentation and correct stale guidance ([#&#8203;29374](astral-sh/ruff#29374))
- \[`pyupgrade`] Remove mentions of variance related to fix safety (`UP040`, `UP047`) ([#&#8203;29592](astral-sh/ruff#29592))

##### Contributors

- [@&#8203;fixfon](https://github.com/fixfon)
- [@&#8203;MichaReiser](https://github.com/MichaReiser)
- [@&#8203;zaniebot](https://github.com/zaniebot)
- [@&#8203;FrancescoPezzella](https://github.com/FrancescoPezzella)
- [@&#8203;anishgirianish](https://github.com/anishgirianish)
- [@&#8203;rajveer3x](https://github.com/rajveer3x)
- [@&#8203;charliermarsh](https://github.com/charliermarsh)
- [@&#8203;LckyLike](https://github.com/LckyLke)
- [@&#8203;DaniBodor](https://github.com/DaniBodor)

</details>

---

### Configuration

πŸ“… **Schedule**: (in timezone America/Denver)

- Branch creation
  - Between 02:00 AM and 06:59 AM, only on Tuesday (`* 2-6 * * 2`)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

β™» **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

πŸ‘» **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://github.com/renovatebot/renovate/discussions) if that's undesired.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4xNDguMyIsInVwZGF0ZWRJblZlciI6IjQ0LjE0OC4zIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJyZW5vdmF0ZSJdfQ==-->

Reviewed-on: https://git.lichtenwalter.site/rlichten/dir2text/pulls/100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

configuration Related to settings and configuration of Ruff or ty

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Custom allowlist for Magic Numbers (PLR2004)

5 participants