-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
Conversation
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 |
d9ec470
to
f4600c3
Compare
Grammar/python.gram
Outdated
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 } |
There was a problem hiding this comment.
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?
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 } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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") }
Gotcha. So then |
Yup |
There was a problem hiding this 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!
Thanks @pablogsal for the PR 🌮🎉.. I'm working now to backport this PR to: 3.10. |
…l patterns (pythonGH-26793) (cherry picked from commit 0acc258) Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
GH-26900 is a backport of this pull request to the 3.10 branch. |
https://bugs.python.org/issue44456