Skip to content

gh-138864: Improve SyntaxError message for case with expression#138865

Open
sobolevn wants to merge 6 commits intopython:mainfrom
sobolevn:issue-138864
Open

gh-138864: Improve SyntaxError message for case with expression#138865
sobolevn wants to merge 6 commits intopython:mainfrom
sobolevn:issue-138864

Conversation

@sobolevn
Copy link
Copy Markdown
Member

@sobolevn sobolevn commented Sep 13, 2025

Снимок экрана 2025-09-13 в 12 53 17

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.

Comment thread Grammar/python.gram Outdated
Copy link
Copy Markdown
Contributor

@ilovelinux ilovelinux left a 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?

@sobolevn
Copy link
Copy Markdown
Member Author

sobolevn commented Sep 15, 2025

Nope, sorry :)
It is not really 100% expected, people might also mean case Ellipsis: :)

Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-09-13-12-51-34.gh-issue-138864.Otm5li.rst Outdated
Comment thread Grammar/python.gram
| 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 {
Copy link
Copy Markdown
Member

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)

Copy link
Copy Markdown
Member

@pablogsal pablogsal Oct 6, 2025

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.

Copy link
Copy Markdown
Member

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.

@sergey-miryanov
Copy link
Copy Markdown
Contributor

Is there anything else that needs to be done?

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants