Skip to content

gh-146495: Improve SyntaxError message for && and || operators#150906

Open
Aniketsy wants to merge 3 commits into
python:mainfrom
Aniketsy:fix-146495
Open

gh-146495: Improve SyntaxError message for && and || operators#150906
Aniketsy wants to merge 3 commits into
python:mainfrom
Aniketsy:fix-146495

Conversation

@Aniketsy
Copy link
Copy Markdown
Contributor

@Aniketsy Aniketsy commented Jun 4, 2026

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 !

Comment thread Parser/pegen_errors.c Fixed
Comment thread Parser/pegen_errors.c
"invalid syntax. Maybe you meant 'and' or '&' instead of '&&'");
return;
}
if (last_token->type == VBAR && p->tokens[p->fill - 2]->type == VBAR) {
Copy link
Copy Markdown
Member

@sobolevn sobolevn left a 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:

Image

Comment thread Lib/test/test_syntax.py
def test_double_pipe(self):
self._check_error(
"a || b",
"Maybe you meant 'or' or '|' instead of '||'",
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.

Can you please also assert the error location, it should be the second | or & char.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review, i've updated with error location, please let me know if we need any further improvements

Copy link
Copy Markdown
Member

@pablogsal pablogsal left a 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

Comment thread Parser/pegen_errors.c
RAISE_SYNTAX_ERROR_KNOWN_RANGE(p->tokens[p->fill - 2], last_token,
"invalid syntax. Maybe you meant 'or' or '|' instead of '||'");
return;
}
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.

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

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.

@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.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented Jun 4, 2026

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider specialized syntax error for && and ||

5 participants