Proposed Check: variable used outside of for-loop scope
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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