-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-121377: Fix closure with intervening comprehension #124696
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
base: main
Are you sure you want to change the base?
Conversation
|
Found this failure case: |
|
Simpler repro: |
Lib/test/test_listcomps.py
Outdated
| code = """ | ||
| x = 1 | ||
| def f(): | ||
| [lambda: x for x in [1]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| [lambda: x for x in [1]] | |
| [lambda: x for x in [2]] |
As discussed:
- Use a different value (the test ran correctly but used the wrong
1) - Should change it to execute the lambda so we ensure the value of
xinside the lambda is right
|
@carljm I think the tests are not testing the exact situation in my original example. They already pass in Python 3.13. The intervening list comprehension needs to be in the innermost scope like my original example, which still fails. |
|
oh, sorry ignore me, I was confused. apparently the tests passed because I ran it at module scope. they are also tested in function scope, so that is ok, I think |
|
but maybe you should also add a test to also test a local in f() to be sure. (I keep getting confused by the change of the function names between our examples) |
Fix the case where a comprehension iteration variable is inlined into a function that otherwise does not have a variable by that name, but the name should be a free variable from an enclosing scope passed through to other enclosed scopes.