-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 comprehension escape in nested comprehensions #3487
Comments
Hey @noloerino Thanks for opening an issue. I'm afraid I can't really reproduce it with
Also tried with 3.8.1 and couldn't get the same error as you. Let me know if you can still reproduce it using your reproduction case or if there is an additional step that is missing. |
Hm, I'm not entirely sure what's going wrong - I'm able to reproduce it from within the pylint codebase as well by adding the following test to method to def test_comprehension_escape_nested_scope(self):
nested_for = astroid.extract_node(
"""
c = [
i for i in range(10)
if any([j == i for j in range(i)])
]
"""
)
nested_lambda = astroid.extract_node(
"""
c = [
i for i in range(10)
if (lambda j: j == i)(0)
]
"""
)
with self.assertNoMessages():
self.checker.visit_listcomp(nested_for.value)
self.checker.visit_listcomp(nested_lambda.value) (you can find this in my fork at https://github.com/noloerino/pylint) I'm reasonably certain that the problem comes from |
@noloerino Ohh now it makes sense. This can be reproduced when passing |
Oh, interesting - it looks like I had This might have gotten lost in the density of the original bug, but were you able to reproduce the lack of message for the second line? |
Yes, I also managed to reproduce the false negative for the second one. Ideally though these should be two separate issues, unless the root cause is the same and they can be fixed by the same changes. |
My initial impression was that they'd both be the same because they both occurred in list comprehensions, but after looking further it seems like they do have different causes. I can open a separate issue if you like. |
Nice, let's go with another issue for now! Thanks a lot for looking into it. |
Won't do, we removed the python3 porting mode in 2.11. |
Steps to reproduce
Current behavior
test.py:3:17: W1662: Using a variable that was bound inside a comprehension (comprehension-escape)
Expected behavior
In the assignment to
a
, the usage ofi
within the if clause is still valid, so the message is a false positive. This error is reported only if the comprehension and if clause are split across multiple lines, which leads me to think it might be an issue with astroid.EDIT: After some investigation, it seems like pylint is using line numbers to determine whether a variable is in scope for list comprehensions. I'm working on a fix.
pylint --version output
The text was updated successfully, but these errors were encountered: