-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
PEP 572 : assignment expression to a global variable in a comprehension #82650
Comments
PEP-572 says that "an assignment expression occurring in a (...) comprehension (...) binds the target in the containing scope, honoring a nonlocal or global declaration for the target in that scope, if one exists." In Appendix B, the PEP shows this example : def f():
global TARGET
a = [TARGET := EXPR for VAR in ITERABLE] So I don't understand why this fails: Python 3.8.0rc1 (tags/v3.8.0rc1:34214de, Oct 1 2019, 18:42:37) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 0
>>> def f():
... global x
... [x := i for i in range(5)]
...
File "<stdin>", line 3
SyntaxError: no binding for nonlocal 'x' found
>>> Is this a bug or am I missing something ? |
I think this is a bug in the symtable_extend_namedexpr_scope function because when visiting the stack in reverse order and finding a function block is always assigning the scope as DEF_NONLOCAL. Marking also as a release blocker unless Nick, Guido or Emily confirm otherwise. |
It's a bug, but it's honestly such an odd corner case that I don't think this should hold up the 3.8.0 release. Let's fix it in 3.9 and backport it to 3.8.1. |
I am working on a fix now, se we can merge it before the final release |
Thanks for working on a fix. But is it really important enough to violate the desired "rc1 == release" invariant? It's up to the release manager. |
I created the fix because many other bug fixes have been merged since rc1 already so apologies if it seemed a bit precipitate. Let's see what the release manager thinks (in case the bugfix is correct) :) |
That was a quick fix, thanks ! |
(FTR the fix is part of Python 3.8.0) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: