-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
False positive for cell-var-from-loop / W0640 #7100
Comments
I think our example is wrong in the documentation because we wanted to make it small but in the process we made it meaningless. It works fine because you're using the function directly in the loop and you could simply do: def foo(numbers):
for i in numbers:
print(i) But https://stackoverflow.com/questions/25314547/cell-var-from-loop-warning-from-pylint explains why the check is useful. I'd welcome a less naive example for the documentation :) |
Documentation aside, it's still a false positive for this simple case: def foo(numbers):
for num in numbers:
print(list(filter(lambda x: x != num, numbers))) Do you think it's safe to not raise this issue for these cases? |
I think I get it now. def get_functions(numbers):
for num in numbers:
function = lambda: print(num)
yield function
for func in get_functions():
func() This one works: 1
2
3 This one also works: def get_functions(numbers):
for num in numbers:
function = lambda: print(num)
yield function
functions = get_functions([1, 2, 3])
for func in functions:
func() 1
2
3 BUT, this one doesn't work: def get_functions(numbers):
for num in numbers:
function = lambda: print(num)
yield function
functions = list(get_functions([1, 2, 3]))
for func in functions:
func() 3
3
3 It's because |
#5263 is a near duplicate. |
Sounds crazy, but I think
W0640
(cell-var-from-loop
) is a lint for an issue that is non-existent.Here's the code snippet from the example, which should supposedly cause
i
to be assigned to the last value in the list:Except, it works fine:
Same is true for lambdas:
Here's pylint output:
asd.py:3:41: W0640: Cell variable num defined in loop (cell-var-from-loop)
Pylint version
The text was updated successfully, but these errors were encountered: