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: Allowing conditions with assignment expressions in comprehensions without parantheses
Type: enhancement Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: arne, emilyemorehouse, gvanrossum, xtreak
Priority: normal Keywords:

Created on 2019-09-25 14:20 by arne, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (8)
msg353203 - (view) Author: Arne Recknagel (arne) Date: 2019-09-25 14:20
All code is run on python build from the current 3.8 branch.

The following doesn't work:

  >>> [x for x in 'foo' if y := True]
    File "<stdin>", line 1
      [x for x in 'foo' if y := True]
                             ^
  SyntaxError: invalid syntax

While this does:

  >>> [x for x in 'foo' if (y := True)]
  ['f', 'o', 'o']

Since assignment expressions work without parentheses in normal if-clauses and there being no mention on it explicitly being disallowed, this seems to either be a bug or an oversight in the documentation (with my own opinion, for what it's worth, being that it's a bug).
msg353223 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-09-25 18:33
We're intentionally holding back on the places where we allow unparenthesized walrus operators.

There are some tricky issues in this particular bit of syntax -- look at this line in the grammar:
https://github.com/python/cpython/blob/bfd0c963d88f3df69489ee250655e2b8f3d235bd/Grammar/Grammar#L190

Because of that I would like to hold back on supporting this.

Is there a specific part of the docs that made you think this should be allowed?
msg353239 - (view) Author: Arne Recknagel (arne) Date: 2019-09-25 21:58
Nothing in particular, only that I expected it to follow the same rules as regular if-blocks. It would be nice to have, since it'd be one less thing to keep in mind.

> There are some tricky issues in this particular bit of syntax [...]

It looks like there are good reasons to keep it this way or wait with potentially misleading docu updates. Then this issue can just serve to point out that it might be something to fix in either way, since I can see people stumbling over it.
msg353240 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-09-25 23:33
@xtreak do you think we need to keep this issue open then?
msg353257 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-09-26 02:57
IIRC there was a debate over allowing top level assignment expression without parentheses in original PEP 572 issue and most of the docs and examples follow using parentheses to avoid this confusion. I just noticed while reading the PEP that list comprehension case is not covered in the exceptional cases section that focused mostly on unparenthesized assignment expressions. If this will not be supported maybe this also could be added to PEP there since PEP serves as reference docs in most cases. The restriction can be lifted in future version if there is a simple way to fix this where this can be reopened.

https://www.python.org/dev/peps/pep-0572/#exceptional-cases
msg353268 - (view) Author: Arne Recknagel (arne) Date: 2019-09-26 06:33
I'll keep shouting from the sidelines, if it's ok.

Is the following behavior still desired

>>> [z := x for x in 'foo']  # valid

over forcing parentheses here as well?

>>> [(z := x) for x in 'foo']  # also valid, but redundant parentheses

The section on when to add parentheses in comprehensions would be a lot simpler if the answer was 'always', with an asterisk saying that the restriction might be lifted for some cases if there is a demand and a good-enough implementation.
msg353270 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-09-26 07:01
I beg to differ. The rule I use is: If it doesn't work without parentheses,
try adding them. That's simple rnough.
msg353293 - (view) Author: Arne Recknagel (arne) Date: 2019-09-26 11:05
Alright, fair enough
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82458
2019-09-27 20:39:12gvanrossumsetstatus: open -> closed
resolution: wont fix
stage: resolved
2019-09-27 20:13:08terry.reedysettype: enhancement
versions: + Python 3.9, - Python 3.8
2019-09-26 11:05:58arnesetmessages: + msg353293
2019-09-26 07:01:57gvanrossumsetmessages: + msg353270
2019-09-26 06:33:28arnesetmessages: + msg353268
2019-09-26 02:57:05xtreaksetnosy: + xtreak
messages: + msg353257
2019-09-25 23:33:21gvanrossumsetmessages: + msg353240
2019-09-25 21:58:30arnesetmessages: + msg353239
2019-09-25 18:33:45gvanrossumsetmessages: + msg353223
2019-09-25 18:00:41xtreaksetnosy: + gvanrossum
2019-09-25 14:20:15arnecreate