Author xtreak
Recipients emilyemorehouse, eric.smith, gvanrossum, steven.daprano, tim.peters, vstinner, xtreak
Date 2019-01-25.06:18:34
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1548397114.32.0.0165205859503.issue35224@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-01-25 06:18:35xtreaksetrecipients: + xtreak, gvanrossum, tim.peters, vstinner, eric.smith, steven.daprano, emilyemorehouse
2019-01-25 06:18:34xtreaksetmessageid: <1548397114.32.0.0165205859503.issue35224@roundup.psfhosted.org>
2019-01-25 06:18:34xtreaklinkissue35224 messages
2019-01-25 06:18:34xtreakcreate