You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
case ...: generates unclear SyntaxError message #138864
Is there any indication that this is actually a problem for users? Is this a common error? There are a huge number of possible errors: I’m opposed to adding every one of them as a special case with a unique error message.
Whatever we need do needs to be weighed against how common the particular error is. Ultimately, “Syntax error” is correct, and the users will have to know how to handle them.
Is there any indication that this is actually a problem for users?
Yes, I think so. case has its own micro-language that is unique. You can use list-looking and tuple-looking expressions, but not set-looking expressions.
Regular SyntaxError requires user to understand the reason behind this, which is not really clear.
You can write dict patterns, but not dict unpacking or comprehensions. The line between last 3 grammar pieces is very thin. We have real users reported this as a problem somewhere in the past.
matchsome_arg:
caseNone:
print('ok')
case ...:
print('...')
-> You will get a SyntaxError: invalid syntax.
Is it easy to fix? Yes. Does it require you to know the little quirks of PM? Yes. Is it user friendly? No.
I think that we should care about users and show them the best diagnostics we can.
Especially, when it costs us little to nothing: invalid_ rules do not slow down initial successful parsing and only are run when the code is broken.
Many languages like Rust, Gleam, etc are exceptional in these diagnostics. Basically, allowing users to copy-paste suggestions from the error messages. Which is super cool for beginners (which is one of our target audiences).
I’m opposed to adding every one of them as a special case with a unique error message.
Can you please specify - why? What do we lose when doing so?
I can see several potential problems:
Potential errors while parsing, it is critical. We need to test this very well
Support cost, we have to maintain good standard of good error messages
Is there something I am missing here? :)
I would love to hear your opinion and your technical concerns.
Is this a common error?
One last thing: users don't report these problems usually, because technically all is correct. This is an issue with the syntax. But, they don't report their frustrations when they spend X minutes understanding the problem and fixing it, when there can be a better way :)
Reacted by Kirill Podoprigora, Brian Schubert, Bartosz Sławecki, Sergey Miryanov, trag1c, M. Kocher and Mikhail Efimov
Reacted by Bartosz Sławecki and Golov Dmitriy Evgenyevich
Many new programming tools provide extensive diagnostics and for good reason :)
If clarifying an error doesn’t hurt performance, what’s the downside?
The only aspect I don’t particularly like in this case is that python.gram keeps growing with each invalid rule. I’d prefer to keep invalid rules in a separate file, but that’s a different discussion.
My point is that I think there need to be some guidelines, otherwise the grammar will grow and grow, and become harder to maintain over time. What is the bar that a given specific error condition needs to clear before it's added as a special case? Can it be made an objective test? If there is no standard to meet, then the temptation is to accept every such special case, which I think is a mistake.
Activity
pythongh-138864: Improve `SyntaxError` message for `case` with expres…
ericvsmith commentedon Sep 13, 2025
Is there any indication that this is actually a problem for users? Is this a common error? There are a huge number of possible errors: I’m opposed to adding every one of them as a special case with a unique error message.
Whatever we need do needs to be weighed against how common the particular error is. Ultimately, “Syntax error” is correct, and the users will have to know how to handle them.
sobolevn commentedon Sep 13, 2025
Yes, I think so.
casehas its own micro-language that is unique. You can use list-looking and tuple-looking expressions, but not set-looking expressions.Regular
SyntaxErrorrequires user to understand the reason behind this, which is not really clear.You can write
dictpatterns, but not dict unpacking or comprehensions. The line between last 3 grammar pieces is very thin. We have real users reported this as a problem somewhere in the past.And tests for these cases:
cpython/Lib/test/test_syntax.py
Lines 373 to 383 in 7168e98
And my proposal improves these cases as well.
I don't agree with this, sorry. Let's say you have this code:
and then you refactor it to:
-> You will get a
SyntaxError: invalid syntax.Is it easy to fix? Yes. Does it require you to know the little quirks of PM? Yes. Is it user friendly? No.
I think that we should care about users and show them the best diagnostics we can.
Especially, when it costs us little to nothing:
invalid_rules do not slow down initial successful parsing and only are run when the code is broken.Many languages like
Rust,Gleam, etc are exceptional in these diagnostics. Basically, allowing users to copy-paste suggestions from the error messages. Which is super cool for beginners (which is one of our target audiences).Can you please specify - why? What do we lose when doing so?
I can see several potential problems:
Is there something I am missing here? :)
I would love to hear your opinion and your technical concerns.
One last thing: users don't report these problems usually, because technically all is correct. This is an issue with the syntax. But, they don't report their frustrations when they spend X minutes understanding the problem and fixing it, when there can be a better way :)
Eclips4 commentedon Sep 14, 2025
Many new programming tools provide extensive diagnostics and for good reason :)
If clarifying an error doesn’t hurt performance, what’s the downside?
The only aspect I don’t particularly like in this case is that
python.gramkeeps growing with each invalid rule. I’d prefer to keep invalid rules in a separate file, but that’s a different discussion.ericvsmith commentedon Sep 15, 2025
My point is that I think there need to be some guidelines, otherwise the grammar will grow and grow, and become harder to maintain over time. What is the bar that a given specific error condition needs to clear before it's added as a special case? Can it be made an objective test? If there is no standard to meet, then the temptation is to accept every such special case, which I think is a mistake.
efimov-mikhail commentedon Oct 4, 2025
IMO, this change worth it. It may not be obvious just for Ellipsis, but adding general rule for "broken match" looks good to me.