Skip to content

Proposed Check: variable used outside of for-loop scope #79

@jamesstidard

Description

@jamesstidard

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions