8000 bpo-44456: Improve the syntax error when mixing keyword and positional patterns by pablogsal · Pull Request #26793 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44456: Improve the syntax error when mixing keyword and positional patterns #26793

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

Merged
merged 1 commit into from
Jun 24, 2021

Conversation

pablogsal
Copy link
Member
@pablogsal pablogsal commented Jun 18, 2021

@brandtbucher
Copy link
Member

Hm, maybe we should loosen this rule to catch any time a positional pattern follows a keyword one. We also want to catch cases like C(a, b=c, d) and C(a=b, c, d=e) too, right?

@pablogsal pablogsal force-pushed the bpo-44456 branch 2 times, most recently from d9ec470 to f4600c3 Compare June 20, 2021 19:08
@pablogsal pablogsal requested a review from brandtbucher June 20, 2021 19:28
8000
Comment on lines 987 to 990
invalid_class_argument_pattern[asdl_pattern_seq*]:
| positional_patterns ',' keyword_patterns ',' a=positional_patterns { a }
| keyword_patterns ',' a=positional_patterns ',' keyword_patterns { a }
| keyword_patterns ',' a=positional_patterns { a }
Copy link
Member
@brandtbucher brandtbucher Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Would something like this (untested) catch all invalid cases where a positional pattern follows a keyword one?

Suggested change
invalid_class_argument_pattern[asdl_pattern_seq*]:
| positional_patterns ',' keyword_patterns ',' a=positional_patterns { a }
| keyword_patterns ',' a=positional_patterns ',' keyword_patterns { a }
| keyword_patterns ',' a=positional_patterns { a }
invalid_class_argument_pattern[asdl_pattern_seq*]:
| [positional_patterns ','] keyword_patterns ',' a=positional_patterns (',' keyword_patterns ',' positional_patterns)* [',' keyword_patterns] { a }

Copy link
Member
@brandtbucher brandtbucher Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm imagining an arbitrarily-long pathological case like C(a=b, c, d=e, f, g=h, i, j=k, ...), for example. Maybe it's not worth catching, but it seems like it would be easy enough to do.

Copy link
Member Author
@pablogsal pablogsal Jun 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can cover that if we rely on eager parsing on the rule above:

>>> match x:
...    case C(a=b, c, d=e, f, g=h, i, j=k, ...):
  File "<stdin>", line 2
    case C(a=b, c, d=e, f, g=h, i, j=k, ...):
                ^
SyntaxError: positional patterns follow keyword patterns

with

diff --git a/Grammar/python.gram b/Grammar/python.gram
index df49d5de63..8f0bb70fc3 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -980,7 +980,7 @@ invalid_as_pattern:
     | or_pattern 'as' a="_" { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "cannot use '_' as a target") }
     | or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }
 invalid_class_pattern:
-    | name_or_attr '(' a=invalid_class_argument_pattern ','? ')' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
+    | name_or_attr '(' a=invalid_class_argument_pattern  { RAISE_SYNTAX_ERROR_KNOWN_RANGE(
         PyPegen_first_item(a, pattern_ty),
         PyPegen_last_item(a, pattern_ty),
         "positional patterns follow keyword patterns") }

@brandtbucher
Copy link
Member
brandtbucher commented Jun 21, 2021

Gotcha. So then invalid_class_argument_pattern just becomes [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }?

@pablogsal
Copy link
Member Author

Gotcha. So then invalid_class_argument_pattern just becomes [positional_patterns ','] keyword_patterns ',' a=positional_patterns { a }?

Yup

Copy link
Member
@brandtbucher brandtbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes we discussed are great. Thanks @pablogsal!

@pablogsal pablogsal merged commit 0acc258 into python:main Jun 24, 2021
@pablogsal pablogsal deleted the bpo-44456 branch June 24, 2021 15:09
@miss-islington
Copy link
Contributor

Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 24, 2021
…l patterns (pythonGH-26793)

(cherry picked from commit 0acc258)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
@bedevere-bot
Copy link

GH-26900 is a backport of this pull request to the 3.10 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.10 only security fixes label Jun 24, 2021
miss-islington added a commit that referenced this pull request Jun 24, 2021
…l patterns (GH-26793)

(cherry picked from commit 0acc258)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0