-
Notifications
You must be signed in to change notification settings - Fork 961
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
Consider splitting PLR2004
(MagicValueComparison
) into different rules
#1949
Comments
For example, Hatch which is a moderately sized project with high code coverage has almost 600 instances of this and none of them are numeric https://github.com/pypa/hatch/actions/runs/3946429939/jobs/6754217718 😅 |
Yeah this rule doesn't make sense to me for single occurrence inline strings. Inline string values most of the time are self explanatory because, well, they are strings. And in 90% of cases the constant variable that would replace them would have the same exact name as the string value. |
In this case, it's Ruff's goal for (Though it's outside the scope of this issue: eventually, we want to get to that point that the flake8 and pylint terminology and mappings are viewed as more of a compatibility layer. E.g., we'll likely recategorize the rules into a first-class taxonomy; we may migrate from numeric to human-readable codes, and so on. Should we make these changes, the intent is for it be done in a way that preserves existing compatibility and even allows for "automatic" configuration migrations -- but the idea is that we'd start viewing Ruff's taxonomy as its own standalone thing rather than a reimplementation, while retaining all the compatibility benefits we get today.) A few comments (even if not a prescriptive solution, yet -- to be clear, I definitely want to figure out something that works for @ofek here):
|
Specifically, a configuration option would look something like: [tool.ruff.pylint]
allow-magic-value-comparisons = ["str"] # Or "string"? Or "String"? |
Yeah that sounds like a reasonable option. In the future scenario in which Ruff has its own rule taxonomy it would be nice if the default behavior excluded strings since almost everyone will add that to their config, or simply not use PL* |
Yeah, I would likely either exclude strings or even omit this rule altogether from the default set. (I personally don't find it useful, but I try to avoid injecting too much of my own opinion when porting third-party rules.) |
I do think it'd be reasonable for you to omit the |
I think the rule for numeric types is quite useful and is actually something that I will probably always comment on in a code review.
I hadn't considered doing that but maybe that's a good idea. 2004 is the only thing that I think would be disruptive though and I want beginners to get as much benefit as possible out of this in order to learn best practices. |
Added said option in #1987. I actually went ahead and ignored string comparisons by default per this thread. It deviates from Pylint, but users can always re-enable string comparisons via the setting, so that's fine with me. |
Thank you!!! |
Of course, thanks as always for being such a great supporter of the project @ofek! |
I would suggest one rule for numeric types and another that would be the current behavior. Checking string values inline is extremely common and as it is now when I have time I'm going to disable this for new projects generated by Hatch.
The text was updated successfully, but these errors were encountered: