Fix B020 false positive with attribute and subscript access#539
Closed
Fridayai700 wants to merge 2 commits intoPyCQA:mainfrom
Closed
Fix B020 false positive with attribute and subscript access#539Fridayai700 wants to merge 2 commits intoPyCQA:mainfrom
Fridayai700 wants to merge 2 commits intoPyCQA:mainfrom
Conversation
B020 was flagging `for x in x.attr` and `for x in x[key]` as reassigning the iterable, but these patterns are safe because: 1. The iterable expression is evaluated once before the loop starts 2. The name is used as an attribute/subscript base, producing a different object — it's not the direct iterable Override visit_Attribute and visit_Subscript in B020NameFinder to stop recording names that appear as attribute bases or subscript bases. Direct name references (for x in x) and names inside function calls (for x in f(x)) are still flagged. This also means `for key, values in values.items()` is no longer flagged, which is correct — values.items() is a method call on an attribute, not a direct reference to the iterable. Fixes PyCQA#521 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The visit_Attribute and visit_Subscript methods in B020NameFinder are intentionally empty to suppress name recording for attribute/subscript bases. Add noqa comments so flake8-bugbear's own B906 check doesn't flag them. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #521 — B020 was incorrectly flagging patterns like:
These are safe because the iterable expression (
smoother.smoothers,data["a"]) is evaluated once before the loop starts. The loop variable name appearing as an attribute/subscript base is not the same as the loop variable being the direct iterable.Changes
visit_Attributeandvisit_SubscriptinB020NameFinderto stop descending into attribute bases and subscript basesfor x in x) and names inside function calls (for x in f(x)) are still correctly flaggedfor key, values in values.items()is no longer flagged (the name appears inside an attribute access)Behavior change
The existing test expected
for key, values in values.items()to be flagged. This PR changes that — sincevaluesis used as an attribute base (values.items()), not as the direct iterable, it should not trigger B020. The pattern is common, idiomatic Python.Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com