classification
Title: PEP 572 : assignment expression to a global variable in a comprehension
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: emilyemorehouse, gvanrossum, lukasz.langa, ncoghlan, pablogsal, quentel
Priority: normal Keywords: patch

Created on 2019-10-13 20:54 by quentel, last changed 2019-10-14 21:42 by lukasz.langa. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16755 merged pablogsal, 2019-10-13 22:40
PR 16760 merged miss-islington, 2019-10-14 04:18
Messages (10)
msg354601 - (view) Author: Pierre Quentel (quentel) * Date: 2019-10-13 20:54
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 ?
msg354603 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-10-13 21:58
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.
msg354604 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-10-13 22:07
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.
msg354606 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-10-13 22:20
I am working on a fix now, se we can merge it before the final release
msg354607 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-10-13 22:27
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.
msg354608 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-10-13 22:44
> But is it really important enough to violate the desired "rc1 == release" invariant?

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) :)
msg354615 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2019-10-14 04:18
New changeset fd5c414880b2e05720b9cf14ab0b0d7ae2b7d925 by Pablo Galindo in branch 'master':
bpo-38469: Handle named expression scope with global/nonlocal keywords (GH-16755)
https://github.com/python/cpython/commit/fd5c414880b2e05720b9cf14ab0b0d7ae2b7d925
msg354628 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2019-10-14 11:40
New changeset 927f07c816aad9f8ed961d7ace6a367837f7fa8f by Łukasz Langa (Miss Islington (bot)) in branch '3.8':
bpo-38469: Handle named expression scope with global/nonlocal keywords (GH-16755) (#16760)
https://github.com/python/cpython/commit/927f07c816aad9f8ed961d7ace6a367837f7fa8f
msg354644 - (view) Author: Pierre Quentel (quentel) * Date: 2019-10-14 19:16
That was a quick fix, thanks !
msg354664 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2019-10-14 21:42
(FTR the fix is part of Python 3.8.0)
History
Date User Action Args
2019-10-14 21:42:18lukasz.langasetmessages: + msg354664
2019-10-14 19:16:10quentelsetmessages: + msg354644
2019-10-14 18:34:35pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-10-14 11:40:18lukasz.langasetmessages: + msg354628
2019-10-14 04:18:20miss-islingtonsetpull_requests: + pull_request16331
2019-10-14 04:18:07pablogsalsetmessages: + msg354615
2019-10-13 22:44:21pablogsalsetmessages: + msg354608
2019-10-13 22:40:12pablogsalsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request16328
2019-10-13 22:27:16gvanrossumsetmessages: + msg354607
2019-10-13 22:20:03pablogsalsetmessages: + msg354606
2019-10-13 22:07:00gvanrossumsetpriority: release blocker -> normal

messages: + msg354604
stage: needs patch
2019-10-13 21:58:22pablogsalsetversions: + Python 3.9
2019-10-13 21:58:18pablogsalsetpriority: normal -> release blocker
nosy: + lukasz.langa
messages: + msg354603

2019-10-13 21:30:54pablogsalsetnosy: + gvanrossum, ncoghlan, emilyemorehouse, pablogsal
2019-10-13 20:54:11quentelcreate