-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Proposed check: reuse of loop variable in nested loop #2972
Comments
I have a working draft of this and will submit a PR in the next few days... |
Sounds good. This looks reasonable to me, especially if it mirrors an existing PyCharm inspection. If bugbear adds it, we can use their rule code; otherwise, we can put it in RUF. |
Great! We may even want to create a new category for PyCharm inspections - I looked through them all, filtered them by suitability for implementation in a linter with no type awareness, then filtered out those for which Ruff already had counterparts, and was left with no fewer than 19 total including this one:
|
I have this tested, with snapshots, and ready to review (thanks to your excellent contribution documentation!). Just need a decision from you on whether to put this under RUF or a create new category for PyCharm (PC? PYC? CH? CHA? CHM?). |
Great work @matthewlloyd! Regarding to where they should be placed, it depends on the rule. See also: pylint-dev/pylint#5608 Pylint
Let's see if we can find corresponding rules to the remaining ones in the overview of rules in pylint. |
Sounds reasonable to me! I wasn't aware of #970, and agree there is a lot of overlap. I went through all the PyCharm inspections in my list above and categorized them by whether they have PyLint counterparts, mostly by running PyLint directly on the examples provided in the PyCharm Settings panel. The inspection in this issue is indeed partially covered by PyLint's new PLW2901 redefined-loop-name inspection, though it does not seem to be detected at all by PLW0621 redefined-outer-name. However, the semantics are slightly different between the PyCharm and PyLint versions:
Given that, it might make sense to add this as PLW2901, and either roll in with statements (my vote), or pull out the with variant into a separate RUF. I'll need to extend my code a little to detect all nested assignments. Already covered by PyLint
PyCharm inspections not covered by PyLint in my tests(note: this list includes only those I deemed implementable in a non-type-aware linter)
The rule "Old-style class contains new-style class features (slots, getattribute, super)" is no longer relevant since Python 3. |
Slightly broadens W2901 to cover `with` statements too. Closes #2972.
I was just bitten by a bug in production where I accidentally reused an outer loop variable in a nested loop. Example:
Proposed warning:
R???: "Variable 'i' is already declared in 'for' loop or 'with' statement above"
Credit for the wording: this is one of PyCharm's built-in inspections.
Note that although I am personally using only Ruff, I have opened a similar enhancement request in the
flake8-bugbear
repo here: PyCQA/flake8-bugbear#360.The text was updated successfully, but these errors were encountered: