B031: don't count store-context references as generator uses#558
Open
StressTestor wants to merge 1 commit into
Open
B031: don't count store-context references as generator uses#558StressTestor wants to merge 1 commit into
StressTestor wants to merge 1 commit into
Conversation
A reference to the groupby loop variable in store context (an annotation target like `group: T`, or an assignment target) was counted as a use of the generator, so a single real use plus an annotation tripped a false-positive B031. Count only loads. Addresses PyCQA#465 (the annotation/store-context case; the if/else case is handled separately by PyCQA#557).
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.
what
fixes a B031 false positive: type-annotating (or assigning to) the
groupbyloop variable was counted as a "use", so a single real use plus an annotation tripped "used more than once".before: B031 fires. after: clean.
why
the B031 usage-counter walked the loop body and counted every
Namematching the group variable regardless of context. an annotation target (group: T) or an assignment target is aStore-contextName, not a use of the generator, so it inflated the count. count onlyLoad-context references.scope
this is the type-annotation / store-context case raised in the comments on #465. the original if/else case is handled separately by #557, and the two are orthogonal: if/else uses are both loads, untouched by this change, so they don't collide. real double-uses still fire B031.
tests
a case in
tests/eval_files/b031.pythat annotates the loop variable then uses it once, expecting no B031. it fails before the fix and passes after. full suite stays green.Addresses #465.