This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow unparenthesized walrus in set comprehensions
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: gvanrossum, lys.nikolaou, miss-islington, pablogsal
Priority: normal Keywords: patch

Created on 2020-11-16 22:42 by lys.nikolaou, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23332 merged pablogsal, 2020-11-16 23:47
PR 23333 merged pablogsal, 2020-11-17 01:22
PR 23382 merged lys.nikolaou, 2020-11-18 23:31
Messages (13)
msg381173 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-11-16 22:42
Since unparenthesized walruses are allowed in list comprehensions and in generator expressions, should we maybe allow them in set comprehensions as well?
msg381177 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-16 23:06
Yeah, I suppose the comprehensions should all have the same syntax inside their various brackets.
msg381187 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-11-16 23:49
Apparently we currently don't allow walruses in set literals either:

>>> {y := 4, 4**2, 3**3}
  File "<stdin>", line 1
    {y := 4, 4**2, 3**3}
       ^
SyntaxError: invalid syntax

But they should be allowed as well per PEP 572 (as the pep mentions all comprehensions):

There is one special case: an assignment expression occurring in a list, set or dict comprehension or in a generator expression (below collectively referred to as "comprehensions") binds the target in ...
msg381188 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-11-16 23:51
The only question left is that if we should allow this on dict literals and dict comprehensions as well (because these constructs have a ":" in the middle that can be noisy if there are walrus, the answer isn't immediately obvious to me).
msg381190 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-11-16 23:54
> dict comprehension

FWIW, unparenthesized walruses are still disallowed in dictcomps and to be honest, I'm not sure if we should allow them or not. I feel that allowing too many colons there would be too noisy.

{a:='a': b:=1} doesn't really look too beautiful.
msg381191 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-16 23:57
Not in anything dict please.

Nagging question — is there sufficient difference between {x := y} and {x: y} ?
msg381192 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-11-16 23:57
While {(a:='a'): (b:=1)} looks somewhat cleaner to me.
msg381194 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-11-16 23:58
> Nagging question — is there sufficient difference between {x := y} and {x: y} ?

I think there is enough distinction, yes. It will also be a bit odd if is allowed in list and list comps but not set and set comps
msg381195 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-16 23:59
Okay.
msg381201 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-11-17 01:17
New changeset b0aba1fcdc3da952698d99aec2334faa79a8b68c by Pablo Galindo in branch 'master':
bpo-42381: Allow walrus in set literals and set comprehensions (GH-23332)
https://github.com/python/cpython/commit/b0aba1fcdc3da952698d99aec2334faa79a8b68c
msg381203 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2020-11-17 02:00
Let’s make sure this is all written up in whatsnew.
msg381383 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-11-18 23:44
New changeset 87c87b5bd6f6a5924b485398f353308410f9d8c1 by Pablo Galindo in branch '3.9':
[3.9] bpo-42381: Allow walrus in set literals and set comprehensions (GH-23332) (GH-23333)
https://github.com/python/cpython/commit/87c87b5bd6f6a5924b485398f353308410f9d8c1
msg381384 - (view) Author: miss-islington (miss-islington) Date: 2020-11-18 23:49
New changeset a85fefe71b3bf56b1b3e72e752e0f962c3b0b487 by Lysandros Nikolaou in branch 'master':
bpo-42381: Document walrus-related syntax changes in whatsnew (GH-23382)
https://github.com/python/cpython/commit/a85fefe71b3bf56b1b3e72e752e0f962c3b0b487
History
Date User Action Args
2022-04-11 14:59:38adminsetgithub: 86547
2020-11-18 23:50:58lys.nikolaousetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-11-18 23:49:36miss-islingtonsetnosy: + miss-islington
messages: + msg381384
2020-11-18 23:44:41pablogsalsetmessages: + msg381383
2020-11-18 23:31:42lys.nikolaousetpull_requests: + pull_request22274
2020-11-17 02:00:35gvanrossumsetmessages: + msg381203
2020-11-17 01:22:00pablogsalsetpull_requests: + pull_request22221
2020-11-17 01:17:20pablogsalsetmessages: + msg381201
2020-11-16 23:59:17gvanrossumsetmessages: + msg381195
2020-11-16 23:58:37pablogsalsetmessages: + msg381194
2020-11-16 23:57:21lys.nikolaousetmessages: + msg381192
2020-11-16 23:57:20gvanrossumsetmessages: + msg381191
2020-11-16 23:54:33lys.nikolaousetmessages: + msg381190
2020-11-16 23:51:45pablogsalsetmessages: + msg381188
2020-11-16 23:49:33pablogsalsetmessages: + msg381187
2020-11-16 23:47:14pablogsalsetkeywords: + patch
stage: patch review
pull_requests: + pull_request22220
2020-11-16 23:06:46gvanrossumsetmessages: + msg381177
2020-11-16 22:42:21lys.nikolaoucreate