PyCQA / PyCQA/flake8-bugbear

Proposed Check: variable used outside of for-loop scope

Open
#79 2 comments 13 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Hi,

Python doesn't scope variables from a for loop, which I don't think I've ever intentionally used as a language feature, and has only been a source of bugs for me.

for x in range(5):
    print(x)
print(x)  # conspicuous usage

I think this would be the same for scopes like context managers; using the file pointer after exiting the open context manager.

I dunno if this is something that would maybe be considered for bugbear to identify.

I was thinking that when it's intentional usage could maybe be identified with something like:

x = None
for x in range(5):
    print(x)
print(x)

# OR

for x in range(5):
    print(x)
last_x = x  #noqa

Thanks for considering this.

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

The issue proposes a new check for Python variables used after for-loop or context-manager scopes, but it does not name an implementation file, test, or entry point. Start by reviewing the existing flake8-bugbear checks and discuss the intended scope and exemptions. Done requires an agreed behavior and tests covering intentional and unintended uses.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.