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.

Title: := in comprehensions does not work
Type: Stage: resolved
Components: Parser, Interpreter Core Versions: Python 3.11, Python 3.10, Python 3.9
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, lys.nikolaou, pablogsal, serhiy.storchaka, socketpair
Priority: normal Keywords:

Created on 2021-05-24 07:11 by socketpair, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg394233 - (view) Author: Марк Коренберг (socketpair) * Date: 2021-05-24 07:11
    for item in collection
    if xxx := mutator(item) is not None
msg394234 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-05-24 07:26
"Does not work" is not informative. What did you get and what did you expect to get? Please proved a complete reproducible example.
msg394235 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-05-24 07:27
The parser rejects this ambiguity and requires parentheses:
    for item in collection
    if (xxx := mutator(item)) is not None

Example ambiguity:

>>> [x for item in "abcdefabc" if x := item.upper() not in "ABC"]
SyntaxError: invalid syntax
>>> [x for item in "abcdefabc" if (x := item.upper()) not in "ABC"]
['D', 'E', 'F']
>>> [x for item in "abcdefabc" if (x := item.upper() not in "ABC")]
[True, True, True]
msg394237 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-05-24 07:46
Well, it works, but the priority of := is lower than priority of other operators. (x := item.upper() not in "ABC") is interpreted as (x := (item.upper() not in "ABC")).
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88389
2021-05-24 07:46:22serhiy.storchakasetstatus: open -> closed
resolution: not a bug
messages: + msg394237

stage: resolved
2021-05-24 07:27:35Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg394235
2021-05-24 07:26:10serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg394234
2021-05-24 07:11:46socketpaircreate