gh-146495: Improve SyntaxError message for && and || operators#150906
gh-146495: Improve SyntaxError message for && and || operators#150906Aniketsy wants to merge 4 commits into
SyntaxError message for && and || operators#150906Conversation
| def test_double_pipe(self): | ||
| self._check_error( | ||
| "a || b", | ||
| "Maybe you meant 'or' or '|' instead of '||'", |
There was a problem hiding this comment.
Can you please also assert the error location, it should be the second | or & char.
There was a problem hiding this comment.
thanks for the review, i've updated with error location, please let me know if we need any further improvements
| RAISE_SYNTAX_ERROR_KNOWN_RANGE(p->tokens[p->fill - 2], last_token, | ||
| "invalid syntax. Maybe you meant 'or' or '|' instead of '||'"); | ||
| return; | ||
| } |
There was a problem hiding this comment.
This is not a good approach for this: _Pypegen_set_syntax_error is a generic functions to set a syntax erroor it should absolutely not know about generic error conditions that are derived from thr Grammar semantics, only the tokenizer middle language (that's why out checks for INDENT/DEDENT). The correct way to raise custom syntax errors is custom grammar rules that trigger on the second pass via invalid_ rules
There was a problem hiding this comment.
@Aniketsy, I think you can address this as:
diff --git a/Grammar/python.gram b/Grammar/python.gram
index 9bf3a67939f..406201e12ae 100644
--- a/Grammar/python.gram
+++ b/Grammar/python.gram
@@ -827,6 +827,7 @@ bitwise_xor[expr_ty]:
bitwise_and[expr_ty]:
| a=bitwise_and '&' b=shift_expr { _PyAST_BinOp(a, BitAnd, b, EXTRA) }
+ | invalid_bitwise_and
| shift_expr
shift_expr[expr_ty]:
@@ -1638,3 +1639,6 @@ invalid_type_params:
RAISE_SYNTAX_ERROR_STARTING_FROM(
token,
"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 '&&'") }(ditto for bitwise_or).
This works for me:
Python 3.16.0a0 (heads/main-dirty:e28a2f49304, Jun 5 2026, 04:45:37) [GCC 14.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 1, 2
>>> a && b
File "<python-input-1>", line 1
a && b
^^
SyntaxError: invalid syntax. Maybe you meant 'and' or '&' instead of '&&'See InternalDocs/changing_grammar.md on changing grammar.
There was a problem hiding this comment.
@pablogsal @skirpichev thanks for the review and the suggestion, i've updated
|
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 |

fixes #146495
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 !
&&and||#146495