PyCQA/flake8-bugbear

B023: false positive for nested function inside loop

Open

#468 opened on Apr 25, 2024

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Python (116 forks)github user discovery
bugenhancementhelp wanted

Repository metrics

Stars
 (1,116 stars)
PR merge metrics
 (Avg merge 4h 17m) (3 merged PRs in 30d)

Description

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

Contributor guide