PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
MIT License
1.06k stars 107 forks source link

B023: false positive for nested function inside loop #468

Open fwinkl opened 6 months ago

fwinkl commented 6 months ago

The following (admittedly bad) code produces what I think must be a false positive:

for _ in range(10):
   foo = []
   def bar():
      foo.append(42)

   bar()
test.py:4:7: B023 Function definition does not bind loop variable 'foo'.

At the very least the warning is confusing because foo is not actually a loop variable.

Using:

flake8 --version
7.0.0 (flake8-bugbear: 24.2.6, flake8-builtins: 2.5.0, mccabe: 0.7.0, pycodestyle: 2.11.1, pyflakes: 3.2.0) CPython 3.10.12 on Linux
cooperlees commented 6 months ago

Agree. I think we should ignore this, even tho I'd never want it in my code bases. I don't think this is particularly worth a dedicated check here, but could be convinced.