Skip to content

[magic-value-comparison] Custom allowlist for Magic Numbers (PLR2004)#18961

Draft
DaniBodor wants to merge 18 commits intoastral-sh:mainfrom
DaniBodor:allow_magic_values
Draft

[magic-value-comparison] Custom allowlist for Magic Numbers (PLR2004)#18961
DaniBodor wants to merge 18 commits intoastral-sh:mainfrom
DaniBodor:allow_magic_values

Conversation

@DaniBodor
Copy link
Contributor

@DaniBodor DaniBodor commented Jun 26, 2025

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 does 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 during development, but I definitely relied heavily on this to solve some of the issues I ran into during development.

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
Copy link
Contributor

github-actions bot commented Jun 26, 2025

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 label Jun 27, 2025
@MichaReiser
Copy link
Member

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

@DaniBodor
Copy link
Contributor Author

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

@marciska
Copy link

marciska commented Dec 5, 2025

Is there any update on this? 🙂

@DaniBodor
Copy link
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
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
Copy link
Contributor

ntBre commented Jan 28, 2026

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

instead of try_from_literal_expr
i.e. listing an allowed magic value of the float `1.0` will also accept the int `1` (and vice versa)
@carljm carljm removed their request for review January 28, 2026 16:51
@ntBre ntBre removed the request for review from sharkdp January 28, 2026 17:50
@ntBre ntBre removed request for Gankra and dcreager January 28, 2026 17:50
@ntBre
Copy link
Contributor

ntBre commented Jan 28, 2026

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
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
Copy link
Contributor

ntBre commented Jan 28, 2026

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
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
Copy link
Contributor Author

DaniBodor commented Jan 28, 2026

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
in addition to defining custom list (which replaces the defaults)
@DaniBodor
Copy link
Contributor Author

@ntBre just checking whether this is still on your radar or whether it fell through the cracks since resolving the ty issues.

@ntBre
Copy link
Contributor

ntBre commented Feb 9, 2026

This is still on my list to review!

@DaniBodor
Copy link
Contributor Author

This is still on my list to review!

just a friendly ping because it's been a few weeks now

@DaniBodor DaniBodor requested a review from MichaReiser March 2, 2026 10:19
Copy link
Contributor

@ntBre ntBre left a comment

Choose a reason for hiding this comment

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

Thanks, this looks pretty reasonable to me at a high level. I had a few suggestions for simplifying a bit further, and I think we should add some tests. At a minimum we should test the somewhat tricky logic in AllowedValue::matches with tests in crates/ruff_linter/src/rules/pylint/mod.rs, but we may also want to include a CLI test or two for parsing from an actual TOML file.

@DaniBodor DaniBodor marked this pull request as draft March 3, 2026 09:43
Copy link
Contributor Author

@DaniBodor DaniBodor left a comment

Choose a reason for hiding this comment

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

Thanks for the review!
Sorry for initially commenting on individual points, rather than batching them in one review.

I think tests make a lot of sense, I use a dummy toml file for one of the repos I used to work on as a test. In doing so, I actually discovered that there's a bug with negative values, which I will need to look into.
I'll also look into creating tests, but may need some help if I get stuck.

In the meantime, I think I've addressed all your other comments already. I will wait with re-opening this PR until I have finished some tests and fixing that negative value bug, but in principle am not working anymore your other comments.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Custom allowlist for Magic Numbers (PLR2004)

4 participants