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

Feature Request: Custom Whitelist for Magic Numbers (PLR2004) #10009

Open
Spectre5 opened this issue Feb 16, 2024 · 5 comments
Open

Feature Request: Custom Whitelist for Magic Numbers (PLR2004) #10009

Spectre5 opened this issue Feb 16, 2024 · 5 comments
Labels
configuration Related to settings and configuration needs-decision Awaiting a decision from a maintainer

Comments

@Spectre5
Copy link

PLR2004 has a few whitelisted numbers including '', 0, and 1. #9964 has fixed a bug so that 0.0 and 1.0 are also whitelisted. There is already an option to ignore certain types of variables from the check. I'd like to request an option to extend the whitelist. For example, I have a library where 2 (and 2.0) are really common and I don't want to use a global variable for it. So I ether need many noqa comments or to disable the check entirely.

@AlexWaygood AlexWaygood added the configuration Related to settings and configuration label Feb 18, 2024
@branidal
Copy link

This would indeed be a very nice improvement. I think there are quite a few common magic variables that are commonly ok but inconvenient to define globally (in my case 0.5) and I can imagine users wanting to be able to add any number of additional options.

@charliermarsh
Copy link
Member

I think it's also worth asking whether we should just add 2 and 2.0 to the allowed list?

@dscorbett
Copy link

I hope this can be made configurable. I use the magic numbers 90, 180, 270, and 360 all over my code, but it wouldn’t make sense to add those to the allowed list for everyone.

@DaniBodor
Copy link
Contributor

To me it would make sense to both make it configurable, but also add 2/2.0 to the default list of allowed values.
I wonder whether there is a quick way to scrape github in a way to get the statistics of which values are most commonly used with noqa: PLR2004. I would guess that at least 2 and 2.0 are very common. Maybe some others as well.

@MichaReiser MichaReiser added the needs-decision Awaiting a decision from a maintainer label Aug 6, 2024
@RubenVanEldik
Copy link
Contributor

I agree with the idea that a whitelist for magic numbers should be customizable. The idea of not using magic numbers is because it's harder to infer the meaning of a line of code if random values are sprinkled throughout it. However, depending on the codebase some numbers might make total sense.

For example, I work on an energy related application. The number 3600 is commonly used to convert between Joule and kWh. Everyone in our team understands the meaning and context of these numbers and there is no value in making a variable for it first. As @dscorbett described, if you're working with angles (degrees) than 90, 180, 270, and 360 are completely self explanatory.

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 needs-decision Awaiting a decision from a maintainer
Projects
None yet
Development

No branches or pull requests

8 participants