classification
Title: PEP 572: Assignment Expressions
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: emilyemorehouse Nosy List: emilyemorehouse, eric.smith, gvanrossum, hroncok, rhettinger, steven.daprano, tim.peters, vstinner, xtreak
Priority: normal Keywords: patch, patch

Created on 2018-11-13 00:52 by emilyemorehouse, last changed 2019-02-13 15:48 by vstinner.

Pull Requests
URL Status Linked Edit
PR 10497 merged emilyemorehouse, 2018-11-13 01:08
PR 10497 merged emilyemorehouse, 2018-11-13 01:08
PR 11670 merged vstinner, 2019-01-25 00:27
PR 11670 merged vstinner, 2019-01-25 00:27
PR 11670 merged vstinner, 2019-01-25 00:27
Messages (15)
msg329781 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2018-11-13 00:52
This issue will serve to track development and PRs for the implementation of PEP 572: Assignment Expressions.
msg334325 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-01-24 23:50
New changeset 8f59ee01be3d83d5513a9a3f654a237d77d80d9a by Emily Morehouse in branch 'master':
bpo-35224: PEP 572 Implementation (#10497)
https://github.com/python/cpython/commit/8f59ee01be3d83d5513a9a3f654a237d77d80d9a
msg334328 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-01-25 00:01
This is huge!

I do recall there are some minor edge cases where the implementation currently doesn't match the PEP. Could you summarize those here, and add your recommendation (e.g. change the PEP, fix the code, wait and see) with motivation?
msg334330 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-25 00:29
The change broke most buildbots: congrats Emily, each core dev has to do their as part of their training ;-) Don't worry, it's fine.

I wrote PR #11670 which should fix test_tools.
msg334331 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-01-25 00:36
@vstinner Is there something I could/should have checked other than the CI displayed in GitHub before merging? Let me know if I can help.



Here's a brief summary of the differences between the PEP spec and implementation:

From the "Scope of the target" section of the PEP, there are two cases that should raise a TargetScopeError: when an assignment expression is used in a comprehension inside a class body or for special cases in comprehensions.

Invalid examples for the latter include:

    [i := i+1 for i in range(5)]
    [[(j := j) for i in range(5)] for j in range(5)]
    [i := 0 for i, j in stuff]
    [i+1 for i in i := stuff]

However, the following work in the implementation,though the PEP states they should be invalid:

    >>> [i := i+1 for i in range(5)]
    [1, 2, 3, 4, 5]
    >>> i
    5

    >>> [i := 0 for i, j in [(1, 2)]]
    [0]

The following does not work in the implementation (as desired), but does not throw a TargetScopeError as defined in the PEP:

    >>> [i+1 for i in i := range(5)]
    File "<stdin>", line 1
        [i+1 for i in i := range(5)]
                        ^
    SyntaxError: invalid syntax


IMO, I was leaning towards advocating for changing the PEP to match the implementation. I think the error messages are clear and expected, and restricting what already works would require significant special cases. I'm open to discussion though.

There's also documentation that should certainly be added (and I believe a spot where assignment expressions are explicitly mentioned as not being included in the language, which is no longer the case)
msg334332 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-25 00:39
> @vstinner Is there something I could/should have checked other than the CI displayed in GitHub before merging? Let me know if I can help.

It wasn't your fault. Our pre-commit checks on pull requests is incomplete on purpose: it has to be fast. It's fine to break buildbots sometimes. It's a tradeoff.

If you want to help, please merge https://github.com/python/cpython/pull/11670 as soon as the CI test pass since I'm going to bed :-)

You are the victim of a very very specific annoying test, test_unparse with its annoying "randomly pick 10 files from the stdlib" feature...
msg334334 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-25 00:50
New changeset 1396d8fab4d0ae830d45f4937322bbb43ce0c30e by Victor Stinner in branch 'master':
bpo-35224: Add support for NamedExpr to unparse.py (GH-11670)
https://github.com/python/cpython/commit/1396d8fab4d0ae830d45f4937322bbb43ce0c30e
msg334341 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python triager) Date: 2019-01-25 06:18
I don't know if this is the correct issue for questions/clarifications but it seems parens are mandatory while using named expressions in while statement which makes some of the examples invalid like https://www.python.org/dev/peps/pep-0572/#sysconfig-py . From my limited knowledge while statement Grammar was not modified at https://github.com/python/cpython/pull/10497/files#diff-cb0b9d6312c0d67f6d4aa1966766ceddR73 and no tests for while statement which made me assume it's intentional. I haven't followed the full discussion about PEP 572 so feel free to correct me if it's a conscious decision and in that case the PEP 572 can be updated.

# python info

➜  cpython git:(master) ./python.exe
Python 3.8.0a0 (heads/bpo35113-dirty:49329a217e, Jan 25 2019, 09:57:53)
[Clang 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

# Example as in PEP 572 to create a simple file that reads itself and prints lines that matches "foo"

➜  cpython git:(master) cat /tmp/foo.py
import re

with open("/tmp/foo.py") as f:
    while line := f.readline():
        if match := re.search(r"foo", line):
            print(match.string.strip("\n"))
➜  cpython git:(master) ./python.exe /tmp/foo.py
  File "/tmp/foo.py", line 4
    while line := f.readline():
               ^
SyntaxError: invalid syntax

# Wrapping named expression with parens for while makes this valid

➜  cpython git:(master) cat /tmp/foo.py
import re

with open("/tmp/foo.py") as f:
    while (line := f.readline()):
        if match := re.search(r"foo", line):
            print(match.string.strip("\n"))
➜  cpython git:(master) ./python.exe /tmp/foo.py
with open("/tmp/foo.py") as f:
        if match := re.search(r"foo", line):


As a user I think parens shouldn't be mandatory in while statement since if statement works fine. Parens can cause while statement to be superfluous in some cases and an extra case to remember while teaching.
msg334358 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-01-25 12:36
Note: I checked and 3.x buildbots are back to green (ignoring the ones which already failed previously). Good.
msg334839 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2019-02-04 20:08
FYI, we need a prominent Whatsnew entry for this.
msg334840 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-02-04 20:42
@rhettinger absolutely, I'm going to include that in my documentation PR which is currently in progress. :)
msg335437 - (view) Author: Miro Hrončok (hroncok) * Date: 2019-02-13 13:24
PEP 572 is nowhere to be found in https://docs.python.org/3.8/whatsnew/3.8.html

Should I open a separate issue for that?
msg335438 - (view) Author: Miro Hrončok (hroncok) * Date: 2019-02-13 13:32
(I've somehow missed the previous comments about the same, sorry about that.)
msg335452 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-02-13 15:29
I have a work-in-progress (WIP) documentation branch I've been working on that I'll push up this week to address the following:

- Add summary to What's New in Doc/whatsnew/3.8.rst
- Add to list of delimiters in Doc/reference/lexical_analysis.rst
- Add usage documentation in Doc/reference/expressions.rst
- Update FAQ in Doc/faq/design.rst (https://bugs.python.org/issue35666)

If anyone has another area they think the documentation should be updated, please let me know!
msg335458 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-02-13 15:48
> If anyone has another area they think the documentation should be updated, please let me know!

If we forget something, it's not an issue: it can be added later!
History
Date User Action Args
2019-02-13 15:48:34vstinnersetkeywords: patch, patch

messages: + msg335458
2019-02-13 15:29:17emilyemorehousesetkeywords: patch, patch

messages: + msg335452
2019-02-13 13:32:41hroncoksetmessages: + msg335438
2019-02-13 13:24:31hroncoksetnosy: + hroncok
messages: + msg335437
2019-02-04 20:42:34emilyemorehousesetkeywords: patch, patch

messages: + msg334840
2019-02-04 20:08:01rhettingersetkeywords: patch, patch
nosy: + rhettinger
messages: + msg334839

2019-01-25 12:36:00vstinnersetkeywords: patch, patch

messages: + msg334358
2019-01-25 06:18:34xtreaksetkeywords: patch, patch

messages: + msg334341
2019-01-25 00:50:09vstinnersetmessages: + msg334334
2019-01-25 00:39:46vstinnersetkeywords: patch, patch

messages: + msg334332
2019-01-25 00:36:34emilyemorehousesetkeywords: patch, patch

messages: + msg334331
2019-01-25 00:29:27vstinnersetkeywords: patch, patch
nosy: + vstinner
messages: + msg334330

2019-01-25 00:27:54vstinnersetpull_requests: + pull_request11483
2019-01-25 00:27:47vstinnersetpull_requests: + pull_request11482
2019-01-25 00:27:38vstinnersetpull_requests: + pull_request11481
2019-01-25 00:01:04gvanrossumsetkeywords: patch, patch

messages: + msg334328
2019-01-24 23:50:00emilyemorehousesetmessages: + msg334325
2019-01-05 16:29:59serhiy.storchakalinkissue35666 dependencies
2018-11-13 10:15:10steven.dapranosetkeywords: patch, patch
nosy: + steven.daprano
2018-11-13 05:43:59xtreaksetkeywords: patch, patch
nosy: + xtreak
2018-11-13 01:32:02eric.smithsetkeywords: patch, patch
nosy: + eric.smith
2018-11-13 01:08:12emilyemorehousesetkeywords: + patch
stage: patch review
pull_requests: + pull_request9759
2018-11-13 01:08:10emilyemorehousesetkeywords: + patch
stage: (no value)
pull_requests: + pull_request9758
2018-11-13 00:52:17emilyemorehousecreate