B023: Functions defined inside a loop must not use variables redefined in the loop, because late-binding closures are a classic gotcha.
Here's some sample code approximating the poetry code:
```python
#!/usr/bin/env python3
def one() -> int:
return 1
def two() -> int:
return 2
def three() -> int:
return 3
factories = [one, two, three]
registered = []
for factory in factories:
registered.append(lambda: factory())
for registered_factory in registered:
print(f"result is {registered_factory()}")
```
output is
```
result is 3
result is 3
result is 3
```
which is exactly the gotcha that flake8-bugbear's B023 was trying to warn about.
I've applied one of the workarounds that various parts of the internet recommend, and you can verify for yourself if you're so inclined that doing the same in the toy script gives the expected output.
| Name |
Last commit
|
Last Update |
|---|---|---|
| .github | Loading commit data... | |
| assets | Loading commit data... | |
| docs | Loading commit data... | |
| src/poetry | Loading commit data... | |
| tests | Loading commit data... | |
| .cirrus.yml | Loading commit data... | |
| .flake8 | Loading commit data... | |
| .gitignore | Loading commit data... | |
| .pre-commit-config.yaml | Loading commit data... | |
| .pre-commit-hooks.yaml | Loading commit data... | |
| CHANGELOG.md | Loading commit data... | |
| CODE_OF_CONDUCT.md | Loading commit data... | |
| CONTRIBUTING.md | Loading commit data... | |
| LICENSE | Loading commit data... | |
| README.md | Loading commit data... | |
| get-poetry.py | Loading commit data... | |
| install-poetry.py | Loading commit data... | |
| poetry.lock | Loading commit data... | |
| pyproject.toml | Loading commit data... |