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: parenthesis is mandatory for named expressions in while statement
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: emilyemorehouse Nosy List: emilyemorehouse, gvanrossum, xtreak
Priority: normal Keywords: patch, patch, patch

Created on 2019-02-01 10:55 by xtreak, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11724 merged xtreak, 2019-02-01 17:20
PR 11724 merged xtreak, 2019-02-01 17:20
PR 11724 merged xtreak, 2019-02-01 17:20
PR 11726 merged emilyemorehouse, 2019-02-01 21:49
PR 11726 merged emilyemorehouse, 2019-02-01 21:49
PR 11726 merged emilyemorehouse, 2019-02-01 21:49
Messages (9)
msg334665 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-01 10:55
I thought to open a separate report itself in order to keep the original issue not cluttered with questions since it could be used for other docs. Sorry for my report there. Original report as per msg334341 .

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.
msg334688 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-02-01 15:28
Emily, I think this would be as simple as making a tiny change to Grammar/Grammar and running make regen-grammar. Can you take care of that?
msg334692 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-01 15:49
> Emily, I think this would be as simple as making a tiny change to Grammar/Grammar and running make regen-grammar. Can you take care of that?

Thanks, can confirm that this fixes the issue. I changed test to namedexpr_test for while statement in grammar and ran `make regen-grammar` and `make`. The reported program runs fine and Lib/test/test_named_expressions.py also passes.

➜  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):


Patch : 

➜  cpython git:(master) ✗ git diff | cat
diff --git a/Grammar/Grammar b/Grammar/Grammar
index e65a688e4c..a425978059 100644
--- a/Grammar/Grammar
+++ b/Grammar/Grammar
@@ -72,7 +72,7 @@ assert_stmt: 'assert' test [',' test]
 compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | with_stmt | funcdef | classdef | decorated | async_stmt
 async_stmt: 'async' (funcdef | with_stmt | for_stmt)
 if_stmt: 'if' namedexpr_test ':' suite ('elif' namedexpr_test ':' suite)* ['else' ':' suite]
-while_stmt: 'while' test ':' suite ['else' ':' suite]
+while_stmt: 'while' namedexpr_test ':' suite ['else' ':' suite]
 for_stmt: 'for' exprlist 'in' testlist ':' [TYPE_COMMENT] suite ['else' ':' suite]
 try_stmt: ('try' ':' suite
            ((except_clause ':' suite)+
diff --git a/Python/graminit.c b/Python/graminit.c
index 6e0f19891b..5cdde2789c 100644
--- a/Python/graminit.c
+++ b/Python/graminit.c
@@ -971,7 +971,7 @@ static arc arcs_42_0[1] = {
     {103, 1},
 };
 static arc arcs_42_1[1] = {
-    {26, 2},
+    {99, 2},
 };
 static arc arcs_42_2[1] = {
     {27, 3},
msg334699 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2019-02-01 16:29
Thanks, Karthikeyan! Can you submit that as a PR?
msg334704 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-01 16:56
> Thanks, Karthikeyan! Can you submit that as a PR?

Sure, I will submit the patch with tests for the same.

Thanks
msg334724 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-02-01 21:40
New changeset d4fceaafb8e3f8700d9ec6ab37a51e903392f74f by Emily Morehouse (Xtreak) in branch 'master':
bpo-35877: Make parenthesis optional for named expression in while statement (GH-11724)
https://github.com/python/cpython/commit/d4fceaafb8e3f8700d9ec6ab37a51e903392f74f
msg334725 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-02-01 21:53
Thanks, Karthikeyan! I added an additional test to make sure this gets coverage. I'll close out this issue once tests pass and I can merge that.
msg334726 - (view) Author: Emily Morehouse (emilyemorehouse) * (Python committer) Date: 2019-02-01 22:27
New changeset ac19081c26eaa7de3e6aabeb789ddc2e7cdd5b24 by Emily Morehouse in branch 'master':
bpo-35877: Add test for while loop named expression without parentheses (GH-11726)
https://github.com/python/cpython/commit/ac19081c26eaa7de3e6aabeb789ddc2e7cdd5b24
msg334727 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-01 22:39
Thanks a lot Guido and Emily for guidance on this issue. Happy to see this for alpha release :)
History
Date User Action Args
2022-04-11 14:59:10adminsetgithub: 80058
2019-02-01 22:39:54xtreaksetkeywords: patch, patch, patch

messages: + msg334727
2019-02-01 22:28:36emilyemorehousesetkeywords: patch, patch, patch
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-02-01 22:27:45emilyemorehousesetmessages: + msg334726
2019-02-01 21:53:20emilyemorehousesetkeywords: patch, patch, patch

messages: + msg334725
2019-02-01 21:49:41emilyemorehousesetpull_requests: + pull_request11610
2019-02-01 21:49:35emilyemorehousesetpull_requests: + pull_request11609
2019-02-01 21:49:30emilyemorehousesetpull_requests: + pull_request11608
2019-02-01 21:40:26emilyemorehousesetmessages: + msg334724
2019-02-01 17:20:34xtreaksetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request11605
2019-02-01 17:20:26xtreaksetkeywords: + patch
stage: needs patch -> needs patch
pull_requests: + pull_request11604
2019-02-01 17:20:19xtreaksetkeywords: + patch
stage: needs patch -> needs patch
pull_requests: + pull_request11603
2019-02-01 16:56:25xtreaksetmessages: + msg334704
2019-02-01 16:29:47gvanrossumsetmessages: + msg334699
2019-02-01 15:49:32xtreaksetmessages: + msg334692
2019-02-01 15:28:25gvanrossumsetassignee: emilyemorehouse
messages: + msg334688
stage: needs patch
2019-02-01 10:55:55xtreakcreate