question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG/QUESTION: Not sure if B020 is expected to trigger here

See original GitHub issue

running flake8 with the latest version of bugbear on this snippet

for var in (var for var in range(10) if var % 2):
    print(var)

results in

t.py:1:5: B020 Found for loop that reassigns the iterable it is iterating with each iterable value.

Is this expected ? I don’t understand how the error message relates to what’s actually happening here, it seems to me that this could be a mistake.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jpy-gitcommented, Mar 22, 2022

I’ve made a PR to these false positives 👍

1reaction
jpy-gitcommented, Mar 21, 2022

It’s comprehensions in general not just generators that have this issue

for var in [var for var in range(10)]:
    print(var)

for var in (var for var in range(10)):
    print(var)

for k, v in {k: v for k, v in zip(range(10), range(10, 20))}.items():
    print(k, v)

N.B. we don’t want to exclude comprehensions from this check completely. This example is still bad and should be flagged:

vars = range(10)
for vars in [i for i in vars]:
    print(vars)

So I think the rule needs to account for the fact that variables declared in the for clause of a comprehension are local to the scope of that comprehension and should be excluded from the check.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vscode todo - Bildgewalt - Production
Following the instructions here, press Ctrl+Shift+P, type Run Tasks, select it and ... When the build is finished, restart VSCode to complete the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found