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 this! 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

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

3 participants