-
-
Notifications
You must be signed in to change notification settings - Fork 33.1k
gh-138864: Improve SyntaxError
message for case
with expression
#138865
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
base: main
Are you sure you want to change the base?
Conversation
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 some people may be using case ...:
to write case _:
Do we want to add this suggestion to the error message?
Nope, sorry :) |
Misc/NEWS.d/next/Core_and_Builtins/2025-09-13-12-51-34.gh-issue-138864.Otm5li.rst
Outdated
Show resolved
Hide resolved
| a="case" patterns guard? ':' NEWLINE !INDENT { | ||
RAISE_INDENTATION_ERROR("expected an indented block after 'case' statement on line %d", a->lineno) } | ||
invalid_case_pattern: | ||
| "case" a=expression guard? ':' block { |
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.
Maybe it would be better to use this variant?
"case" a=expression 'as'? expression? guard? ':' block
For cases like this:
match ...:
case T[1] as E:
print(1)
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.
This will be slower and a bit more unreadable I think and also allows to match "case" a=expression 'as' guard ':' block
which makes no sense.
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.
We can run for ('as' expression)?
. But "slower" sounds ugly, maybe such a little change doesn't worth it.
In my opinion this makes already awesome python error messages (thanks, @pablogsal!) even cooler.
Maybe there are some hidden things that I've missed? I would be happy to fix them :)
In case this complicates our parser quite a bit - feel free to close.
case ...:
generates unclearSyntaxError
message #138864