Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parenthesis is mandatory for named expressions in while statement #80058

Closed
tirkarthi opened this issue Feb 1, 2019 · 9 comments
Closed

parenthesis is mandatory for named expressions in while statement #80058

tirkarthi opened this issue Feb 1, 2019 · 9 comments
Assignees
Labels
3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@tirkarthi
Copy link
Member

BPO 35877
Nosy @gvanrossum, @emilyemorehouse, @tirkarthi
PRs
  • bpo-35877: Make parenthesis optional for named expression in while statement #11724
  • bpo-35877: Make parenthesis optional for named expression in while statement #11724
  • bpo-35877: Make parenthesis optional for named expression in while statement #11724
  • bpo-35877: Add test for while loop named expression without parentheses #11726
  • bpo-35877: Add test for while loop named expression without parentheses #11726
  • bpo-35877: Add test for while loop named expression without parentheses #11726
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/emilyemorehouse'
    closed_at = <Date 2019-02-01.22:28:36.404>
    created_at = <Date 2019-02-01.10:55:55.277>
    labels = ['interpreter-core', 'type-bug', '3.8']
    title = 'parenthesis is mandatory for named expressions in while statement'
    updated_at = <Date 2019-02-01.22:39:54.852>
    user = 'https://github.com/tirkarthi'

    bugs.python.org fields:

    activity = <Date 2019-02-01.22:39:54.852>
    actor = 'xtreak'
    assignee = 'emilyemorehouse'
    closed = True
    closed_date = <Date 2019-02-01.22:28:36.404>
    closer = 'emilyemorehouse'
    components = ['Interpreter Core']
    creation = <Date 2019-02-01.10:55:55.277>
    creator = 'xtreak'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35877
    keywords = ['patch', 'patch', 'patch']
    message_count = 9.0
    messages = ['334665', '334688', '334692', '334699', '334704', '334724', '334725', '334726', '334727']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'emilyemorehouse', 'xtreak']
    pr_nums = ['11724', '11724', '11724', '11726', '11726', '11726']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35877'
    versions = ['Python 3.8']

    @tirkarthi
    Copy link
    Member Author

    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.

    @tirkarthi tirkarthi added 3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 1, 2019
    @gvanrossum
    Copy link
    Member

    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?

    @tirkarthi
    Copy link
    Member Author

    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},

    @gvanrossum
    Copy link
    Member

    Thanks, Karthikeyan! Can you submit that as a PR?

    @tirkarthi
    Copy link
    Member Author

    Thanks, Karthikeyan! Can you submit that as a PR?

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

    Thanks

    @emilyemorehouse
    Copy link
    Member

    New changeset d4fceaa by Emily Morehouse (Xtreak) in branch 'master':
    bpo-35877: Make parenthesis optional for named expression in while statement (GH-11724)
    d4fceaa

    @emilyemorehouse
    Copy link
    Member

    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.

    @emilyemorehouse
    Copy link
    Member

    New changeset ac19081 by Emily Morehouse in branch 'master':
    bpo-35877: Add test for while loop named expression without parentheses (GH-11726)
    ac19081

    @tirkarthi
    Copy link
    Member Author

    Thanks a lot Guido and Emily for guidance on this issue. Happy to see this for alpha release :)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants