flake8-bugbear icon indicating copy to clipboard operation
flake8-bugbear copied to clipboard

Loop variable use in closure not considered

Open inducer opened this issue 4 years ago • 1 comments

Running

flake8==3.9.2
flake8-bugbear==21.4.3
flake8-polyfill==1.0.2
flake8-quotes==3.2.0

on

def main():
    def f(x):
        print(i, x)

    for i in [1, 2, 3]:
        f('HI')

gives me

bugbearbug.py:5:9: B007 Loop control variable 'i' not used within the loop body. If this is intended, start the name with an underscore.

Clearly, i is being used (within the nested function), but bugbear doesn't seem to see it.

inducer avatar Jul 11 '21 06:07 inducer

Putting my dislike for this usage aside, if someone can make the AST parser work this logic out and can show that in unittests I'll accept this. This, of course, unless someone else speaks up and gives reasons not to. I feel this will just reduce false positives.

My personal though is one should always use a function the way functions are designed to be used and not abuse scope. For example the code could be:

def main():
    def f(x, main_i):
        print(main_i, x)

    for i in [1, 2, 3]:
        f('HI', i)

I feel this has readability advantages and is harder to miss where i comes from. It also would no longer trigger bugbear B007.

cooperlees avatar Jul 12 '21 16:07 cooperlees