classification
Title: Walrus Operator in list index
Type: Stage: patch review
Components: Documentation, Interpreter Core Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Brando753, docs@python, lys.nikolaou, terry.reedy
Priority: normal Keywords: patch

Created on 2020-11-10 17:28 by Brando753, last changed 2020-11-16 23:09 by lys.nikolaou.

Pull Requests
URL Status Linked Edit
PR 23291 open terry.reedy, 2020-11-15 07:44
PR 23317 merged lys.nikolaou, 2020-11-16 17:28
Messages (3)
msg380691 - (view) Author: Brandon (Brando753) Date: 2020-11-10 17:28
Reading the PEP 572 document I don't see anything stating that Walrus operator in list indexes must be enclosed in parenthesis. 

Minimal Example: 
'''
In [1]: a = list(range(10))

In [2]: idx = -1

In [3]: a[idx := idx +1]
  File "<ipython-input-3-65fc931e77dd>", line 1
    a[idx := idx +1]
          ^
SyntaxError: invalid syntax

'''

If you enclose in parenthesis it works as expected: 
'''
In [4]: a[(idx := idx +1)]
Out[4]: 0

In [5]: a[(idx := idx +1)]
Out[5]: 1

In [6]: a[(idx := idx +1)]
Out[6]: 2
'''

Is this a bug or am I misreading the PEP 572 document somewhere? It's not a top-level assignment nor is it a list comprehension so I would expect it to work in the first example.
msg380970 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-11-14 10:54
PEP 572 does not saw much of anything about when parens are needed.  Nor does the low priority itself.  Looking through the grammar of expressions, an assignment_expression is also a starred_expression, a positional_item (in calls), and the first part of a comprehension. It is not itself an plain expression unless and until wrapped in parentheses.  Subscription requires an expression list, which may be a single expression.

To put it another way: an expression is an assignment expression but an assignment expression with a "name :=" prefix is not an expression, so parens are required anywhere an expression is required.  That includes subscripts, slicings, displays, call items other than positional items, comprehension parts other than the initial expression (maybe, check), parts of conditional expressions (maybe, check), and lambda expression bodies.  After checking, I would like a sentence added to the doc before the 'go see the PEP' bit.

This has nothing to do with asyncio.
msg381179 - (view) Author: Lysandros Nikolaou (lys.nikolaou) * (Python committer) Date: 2020-11-16 23:09
New changeset cae60187cf7a7b26281d012e1952fafe4e2e97e9 by Lysandros Nikolaou in branch 'master':
bpo-42316: Allow unparenthesized walrus operator in indexes (GH-23317)
https://github.com/python/cpython/commit/cae60187cf7a7b26281d012e1952fafe4e2e97e9
History
Date User Action Args
2020-11-16 23:09:41lys.nikolaousetmessages: + msg381179
2020-11-16 17:28:11lys.nikolaousetnosy: + lys.nikolaou
pull_requests: + pull_request22208
2020-11-15 07:44:57terry.reedysetkeywords: + patch
stage: patch review
pull_requests: + pull_request22182
2020-11-14 10:54:58terry.reedysetversions: + Python 3.10, - Python 3.8
nosy: + docs@python, terry.reedy, - asvetlov, yselivanov
messages: + msg380970

assignee: docs@python
components: + Documentation, Interpreter Core, - asyncio
2020-11-10 17:28:35Brando753create