Skip to content
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 #3107

Closed
dpinol opened this issue Sep 16, 2019 · 4 comments · Fixed by #4827
Closed

False positive for cell-var-from-loop #3107

dpinol opened this issue Sep 16, 2019 · 4 comments · Fixed by #4827
Labels
Bug 🪲 Checkers Related to a checker
Milestone

Comments

@dpinol
Copy link

dpinol commented Sep 16, 2019

Steps to reproduce

for i in [[2], [3]]:
    next(filter(lambda j, ix=i[0]: j == ix, [1, 3]))

Current behavior

W0640: Cell variable i defined in loop (cell-var-from-loop)

Expected behavior

I would expect to avoid the warning because the "i" is passed as default argument to the lambda. I get the false positive also if I loop along objects instead of list.
But I don't get it for the code below (looping along ints)

for i in [2, 3]:
    next(filter(lambda j, ix=i: j == ix, [1, 3]))

pylint --version output

pylint 2.3.1
astroid 2.2.5
Python 3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)

thanks

@PCManticore
Copy link
Contributor

Thanks for the report.

@PCManticore PCManticore added Bug 🪲 Checkers Related to a checker labels Sep 23, 2019
@PCManticore PCManticore changed the title false positive cell-var-from-loop even with trick False positive for cell-var-from-loop Sep 23, 2019
@SamyCookie
Copy link

SamyCookie commented Sep 4, 2020

same issue for me, here a functional reduced test case:

handlers = []
for i in range(10):
    handlers.append(lambda: i)

@greschd
Copy link

greschd commented Oct 2, 2020

same issue for me, here a functional reduced test case:

handlers = []
for i in range(10):
    handlers.append(lambda: i)

This isn't a false positive, since i is only resolved when the handlers are called:

In [1]: handlers = []

In [2]: for i in range(10):
   ...:     handlers.append(lambda: i)
   ...:

In [3]: [h() for h in handlers]
Out[3]: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

@SamyCookie
Copy link

same issue for me, here a functional reduced test case:

handlers = []
for i in range(10):
    handlers.append(lambda: i)

This isn't a false positive, since i is only resolved when the handlers are called:

In [1]: handlers = []

In [2]: for i in range(10):
   ...:     handlers.append(lambda: i)
   ...:

In [3]: [h() for h in handlers]
Out[3]: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

Ouch, you're so right. My bad, bad example !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🪲 Checkers Related to a checker
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants