GeezerVison is showing github.com — gh-146495: Improve `SyntaxError` message for `&&` and `||` operators by Aniketsy · Pull Request #150906 · python/cpython
Conversation
Aniketsy
commented
Jun 4, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
fixes #146495
@Aniketsy I would prefer that a parser expert takes care of that. This is likely a non-trivial change so to make it easier to review, it would be better if a core dev or someone familiar with the parser takes care of this.
As there was not any pr for this for long time, so i thought to give a try for this as @skirpichev have shared the patch towards fix. Hoping it does not create much pain to review for members. Thank you !
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
sobolevn
left a comment
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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 like the idea! Before it was:
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for the PR @Aniketsy! Unfortunately this is not the correct approach for such an error message. See my comment below
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with heart emoji
Uh oh!
There was an error while loading. Please reload this page.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.
Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
LGTM. 👍
As I proposed in #151129 (comment), we can also add suggestions for <> ("not equal" in Pascal and Python 2), "=<", "=>" and "=!" (typo in "<=", ">=" and "!="), "===" ("is" in JavaScript and PHP).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
skirpichev reacted with hooray emoji
LGTM. 👍
Thanks!
As I proposed in #151129 (comment), we can also add suggestions for <> ("not equal" in Pascal and Python 2), "=<", "=>" and "=!" (typo in "<=", ">=" and "!="), "===" ("is" in JavaScript and PHP).
I'd be happy to create a follow up PR or should i open issue and then link pr for this
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
"===" ("is" in JavaScript and PHP).
And Mathematica.
I'd be happy to create a follow up PR or should i open issue and then link pr for this
Yes, please open a separate issue.
|
Aniketsy reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with heart emoji
Uh oh!
There was an error while loading. Please reload this page.
I'd be happy to create a follow up PR or should i open issue and then link pr for this
Up to you. You can also include them in this PR.
|
Aniketsy reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
Sorry, this doesn't look acceptable for me:
>>> 1 & & 2
File "<python-input-1>", line 1
1 & & 2
^^^
SyntaxError: invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?
I think you should raise a different exception message only for cases, when '&&' or '||' could be interpreted as distinct tokens.
|
Aniketsy reacted with confused emoji
Aniketsy reacted with eyes emoji
All reactions
-
😕
1 reaction
-
👀
1 reaction
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, this doesn't look acceptable for me:
>>> 1 & & 2
File "<python-input-1>", line 1
1 & & 2
^^^
SyntaxError: invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?
I think you should raise a different exception message only for cases, when '&&' or '||' could be interpreted as distinct tokens.
should we fall back to generic error message if we have gap between them ?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with heart emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
with this patch
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ git diff Grammar/python.gram
diff --git a/Grammar/python.gram b/Grammar/python.gram
index c45e2f3a28..823af82397 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -1642,7 +1642,19 @@ invalid_type_params:
"Type parameter list cannot be empty")}
invalid_bitwise_and:
- | a=bitwise_and b='&' c='&' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?") }
+ | a=bitwise_and b='&' c='&' {
+ (b->end_lineno == c->lineno)
+ ? ((b->end_col_offset == c->col_offset)
+ ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?")
+ : NULL)
+ : NULL
+ }
invalid_bitwise_or:
- | a=bitwise_or b='|' c='|' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?") }
+ | a=bitwise_or b='|' c='|' {
+ (b->end_lineno == c->lineno)
+ ? ((b->end_col_offset == c->col_offset)
+ ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?")
+ : NULL)
+ : NULL
+ }
output :
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "1 && 2"
File "<string>", line 1
1 && 2
^^
SyntaxError: invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "1 & & 2"
File "<string>", line 1
1 & & 2
^
SyntaxError: invalid syntax
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "1 || 2"
File "<string>", line 1
1 || 2
^^
SyntaxError: invalid syntax. Maybe you meant 'or' or '|' instead of '||'?
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "1 | | 2"
File "<string>", line 1
1 | | 2
^
SyntaxError: invalid syntax
previously i tried something like this but it was throwing warning during compiling
invalid_bitwise_and:
| a=bitwise_and b='&' c='&' {
(b->end_lineno == c->lineno && b->end_col_offset == c->col_offset)
? RAISE_SYNTAX_ERROR_KNOWN_RANGE(b, c, "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?")
: NULL
}
these were the warning :--
lude -DPy_BUILD_CORE -o Parser/parser.o Parser/parser.c
Parser/parser.c: In function ‘invalid_bitwise_and_rule’:
Parser/parser.c:28511:77: warning: comparison between pointer and integer
28511 | _res = ( b -> end_lineno == c -> lineno & & b -> end_col_offset == c -> col_offset ) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE ( b , c , "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?" ) : NULL;
| ^~
Parser/parser.c:28511:38: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
28511 | _res = ( b -> end_lineno == c -> lineno & & b -> end_col_offset == c -> col_offset ) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE ( b , c , "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'?" ) : NULL;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
Parser/parser.c: In function ‘invalid_bitwise_or_rule’:
Parser/parser.c:28560:77: warning: comparison between pointer and integer
28560 | _res = ( b -> end_lineno == c -> lineno & & b -> end_col_offset == c -> col_offset ) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE ( b , c , "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?" ) : NULL;
| ^~
Parser/parser.c:28560:38: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]
28560 | _res = ( b -> end_lineno == c -> lineno & & b -> end_col_offset == c -> col_offset ) ? RAISE_SYNTAX_ERROR_KNOWN_RANGE ( b , c , "invalid syntax. Maybe you meant 'or' or '|' instead of '||'?" ) : NULL;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
with this patch
Yes, like that. Though, I think it could be factored to a helper function.
these were the warning
You should take into account operator precedence rules in C.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
The code was correct, but errorprone, thus the compiler warning. Simply add parentheses: ((b->end_lineno == c->lineno) && (b->end_col_offset == c->col_offset)).
|
Aniketsy reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Hmm, it seems there is a bug in the python.gram parser or the Parser/parser.c generator which parses && in C code as two tokens & and & and then inserts a space between them.
|
Aniketsy reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Anyway, if you add a helper that tests this condition in Parser/pegen.h, you can simply use it in python.gram.
|
Aniketsy reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
In my opinion && and & & are both incorrect, the error message is good enough.
|
Aniketsy reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
In my opinion && and & & are both incorrect, the error message is good enough.
+1 make sense
Hmm, it seems there is a bug in the python.gram parser or the Parser/parser.c generator which parses && in C code as two tokens & and & and then inserts a space between them.
should we take care of this in follow up
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Also please let me know your thoughts on this as well #151375
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
In my opinion && and & & are both incorrect, the error message is good enough.
I typed '& &', not '&&'. And maybe actually meant something like '& 7' (a typo).
|
serhiy-storchaka and Aniketsy reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Add tests with space between operators, e.g. a & & b.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
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 is actually a regexp. So escape ? (in other test too).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
gentle ping! should we move this forward so that we can also address the follow up PR. Thanks!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
LGTM! Thanks a lot @Aniketsy and thanks everyone for your great reviews ❤️
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Aniketsy reacted with heart emoji
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
sobolevn
sobolevn approved these changes
lysnikolaou
Awaiting requested review from lysnikolaou
lysnikolaou is a code owner
+1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|