gh-151303 : Improve SyntaxError suggestions for common operator typos and cross-language mistakes#151375
Open
Aniketsy wants to merge 6 commits into
Open
gh-151303 : Improve SyntaxError suggestions for common operator typos and cross-language mistakes#151375Aniketsy wants to merge 6 commits into
SyntaxError suggestions for common operator typos and cross-language mistakes#151375Aniketsy wants to merge 6 commits into
Conversation
Member
|
Please add tests and news entry. |
|
|
||
| eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) } | ||
| invalid_eqeqeq: | ||
| | a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='") } |
Member
There was a problem hiding this comment.
The suggestion ("Maybe you meant ... ?") should be a question, same for the rest.
Contributor
Author
There was a problem hiding this comment.
thanks for the improvement
Aniketsy
commented
Jun 13, 2026
…e-151303.mzlJxi.rst
| | invalid_eqeqeq | ||
|
|
||
| invalid_diamond_op: | ||
| | a='<' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'?") } |
Member
There was a problem hiding this comment.
Won't this break when __future__.CO_FUTURE_BARRY_AS_BDFL is true?
Contributor
Author
There was a problem hiding this comment.
hmm , yes
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "
__futu> from __future__ import barry_as_FLUFL
> 1 < > 2
> "
File "<string>", line 3
1 < > 2
^^^
SyntaxError: invalid syntax. Maybe you meant '!=' instead of '<>'?
please let me know your thoughts on this ...
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ git diff Grammar/python.gram
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 61a6d5ea90..19ef406437 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -805,7 +805,11 @@ compare_op_bitwise_or_pair[CmpopExprPair*]:
| invalid_eqeqeq
invalid_diamond_op:
- | a='<' b='>' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'?") }
+ | a='<' b='>' {
+ (p->flags & PyPARSE_BARRY_AS_BDFL)
+ ? RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '<>' instead of '< >'?")
+ : RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '!=' instead of '<>'?")
+ }
eq_bitwise_or[CmpopExprPair*]: '==' a=bitwise_or { _PyPegen_cmpop_expr_pair(p, Eq, a) }
invalid_eqeqeq:
| a='==' b='=' { RAISE_SYNTAX_ERROR_KNOWN_RANGE(a, b, "invalid syntax. Maybe you meant '==' instead of '==='?") }
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 '!=' instead of '<>'?
aniket@DESKTOP-074O80J:/mnt/d/cpython/cpython$ ./python.exe -c "
__futu> from __future__ import barry_as_FLUFL
> 1 < > 2
> "
File "<string>", line 3
1 < > 2
^^^
SyntaxError: invalid syntax. Maybe you meant '<>' instead of '< >'?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fixes #151303
SyntaxErrorsuggestions for common operator typos and cross-language mistakes #151303