PyCQA / PyCQA/flake8-bugbear

Loop variable use in closure not considered

Open
#175 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement help wanted
Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported B007 warning with the provided nested-function example and the listed flake8-bugbear versions. Trace how B007 determines whether a loop variable is used, then add coverage showing that use within the nested function is recognized and confirm the warning is no longer emitted for this example.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.