-
Notifications
You must be signed in to change notification settings - Fork 109
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: Why flake8 returns error only for a temporal variable for for loop? #396
Comments
I think the above code has the same structure as the following code.
|
Hi, the difference is that in the first example you reference the loop variable inside a lambda which could in theory lead to issues (not in your case though since it's not called outside the loop). |
I really thank you for your quick and proper reply!! > @tomasr8
I understand the situation. Due to another flake8 error( Thank you. |
Is it a correct or ideal output for flake8 developers? |
Yes it should ideally not be raised in this case, but distinguishing the false positives from true positives is not that straightforward (see the linked issue for more details) |
I really thank you for your proper comment.
I understand it. With your comment, I can avoid B023 error changing the code to |
Would accept making the check handle lambdas better if it's possible. |
I don't think this can/should be fixed - we have no guarantees that the lambda will only be executed inside the loop when passing it to some unknown 3rd-party function: lambdas_to_run_later = []
for i in range(10):
lambdas_to_run_later.append(lambda: print(i))
for l in lambdas_to_run_later:
l() # oops, always prints 9 |
flake8 causes error:
B023 Function definition does not bind loop variable 'chk_key'
for the following code.I cannot understand the reason.
chk_key
is only a temporal variable for for loop. How should I fix it?I'd like to fix it as soon as possible.
I set a value to
chk_key
at the top of the function for trial. However, flake8 returns the same message.The text was updated successfully, but these errors were encountered: