Skip to content

B031: don't count store-context references as generator uses#558

Open
StressTestor wants to merge 1 commit into
PyCQA:mainfrom
StressTestor:fix/b031-store-context-fp
Open

B031: don't count store-context references as generator uses#558
StressTestor wants to merge 1 commit into
PyCQA:mainfrom
StressTestor:fix/b031-store-context-fp

Conversation

@StressTestor

Copy link
Copy Markdown

what

fixes a B031 false positive: type-annotating (or assigning to) the groupby loop variable was counted as a "use", so a single real use plus an annotation tripped "used more than once".

for _, group in groupby(items):
    group: SomeType        # annotation, not a use
    do_something(group)    # the only real use

before: B031 fires. after: clean.

why

the B031 usage-counter walked the loop body and counted every Name matching the group variable regardless of context. an annotation target (group: T) or an assignment target is a Store-context Name, not a use of the generator, so it inflated the count. count only Load-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.py that 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.

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant