-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
B023
False positive
#380
Comments
Would accept a PR that somehow dives into the function scope to see i is use within the loop. Just out of interest, does passing |
@cooperlees It does not, this code doesn't get a warning:
However this approach isn't feasible for our actual usecase. |
The same problem in for i in range(5):
def foo(a):
return i + a # B023
print(foo(10)) Even with def bar():
for i in range(5):
def foo(a):
nonlocal i
return i + a # B023
print(foo(10)) I've to assign i to avoid def bar():
for i in range(5):
def foo(a):
nonlocal i
i = i
return i + a
print(foo(10)) |
This is somewhat tricky to check for. I think the only reasonable way to go about this would be to save
until the end of the parent scope where we clear it as a potential B023. Glancing at check_for_b023 it's already quite complex though |
This example is obviously too simple to be realistic - our actual code has more logic in
foo()
and passes parameters to it, but is equivalent in terms of everything the rule cares about. We get an error ofFunction definition does not bind loop variable 'i'.Flake8(B023)
. This should not happen becausefoo
is not a closure. It's never called outside the loop, and execution is never delayed.i
should always have the correct value.The reason we aren't immediately doing
mydict[arr[i]] = 0
is because we have other logic that determines the parameters passed tofoo()
.The text was updated successfully, but these errors were encountered: