GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146495: Improve `SyntaxError` message for `&&` and `||` operators by Aniketsy · Pull Request #150906 · python/cpython

Conversation

@Aniketsy

@Aniketsy Aniketsy commented Jun 4, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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 !

  • Issue: Consider specialized syntax error for && and || #146495

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Aniketsy
Improve SyntaxError message for and operators
e309e5e
@Aniketsy
Aniketsy requested review from lysnikolaou and pablogsal as code owners June 4, 2026 10:41
@bedevere-app bedevere-app Bot added the awaiting review label Jun 4, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jun 4, 2026
Consider specialized syntax error for && and || #146495
Closed
github-advanced-security[bot]
github-advanced-security Bot found potential problems Jun 4, 2026
View reviewed changes
Comment thread Parser/pegen_errors.c Fixed

Uh oh!

There was an error while loading. Please reload this page.

github-advanced-security[bot]
github-advanced-security Bot found potential problems Jun 4, 2026
View reviewed changes
Comment thread Parser/pegen_errors.c Fixed

Uh oh!

There was an error while loading. Please reload this page.

sobolevn
sobolevn reviewed Jun 4, 2026
View reviewed changes

@sobolevn sobolevn left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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:

Image

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Aniketsy reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Lib/test/test_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Aniketsy and others added 2 commits June 4, 2026 14:55
@Aniketsy
Add error location assertions for && and ||
02f650a
@blurb-it
📜🤖 Added by blurb_it.
3b0f068
pablogsal
pablogsal requested changes Jun 4, 2026
View reviewed changes

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

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
All reactions
  • ❤️ 1 reaction
Comment thread Parser/pegen_errors.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting review label Jun 4, 2026
@bedevere-app

bedevere-app Bot commented Jun 4, 2026

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting changes label Jun 4, 2026
@Aniketsy
Fix && and || syntax error using invalid grammar rules
f527441
@skirpichev
skirpichev self-requested a review June 5, 2026 04:14
skirpichev
skirpichev approved these changes Jun 5, 2026
View reviewed changes
@skirpichev
skirpichev requested review from pablogsal and sobolevn June 5, 2026 04:39
@IvyXu420 IvyXu420 mentioned this pull request Jun 11, 2026
gh-151128: Improve SyntaxError message for cross language keywords #151129
Merged
serhiy-storchaka
serhiy-storchaka approved these changes Jun 11, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

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
All reactions
  • 🎉 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting changes labels Jun 11, 2026
@Aniketsy

Aniketsy commented Jun 11, 2026

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 11, 2026

Copy link
Copy Markdown
Member

"===" ("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
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy Aniketsy mentioned this pull request Jun 11, 2026
Improve SyntaxError suggestions for common operator typos and cross-language mistakes #151303
Open
sobolevn
sobolevn approved these changes Jun 11, 2026
View reviewed changes

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

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.

Thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Aniketsy reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-06-04-15-02-35.gh-issue-146495.wWRRvx.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy @sobolevn
Update Misc/NEWS.d/next/Core_and_Builtins/2026-06-04-15-02-35.gh-issu…
4d8f024
…e-146495.wWRRvx.rst

Co-authored-by: sobolevn <[email protected]>
@serhiy-storchaka

serhiy-storchaka commented Jun 11, 2026

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev
skirpichev requested changes Jun 12, 2026
View reviewed changes
Comment thread Grammar/python.gram Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev
skirpichev previously approved these changes Jun 12, 2026
View reviewed changes
Comment thread Lib/test/test_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Aniketsy and others added 2 commits June 12, 2026 09:01
@Aniketsy @skirpichev
Update Lib/test/test_syntax.py
5aad775
Co-authored-by: Sergey B Kirpichev <[email protected]>
@Aniketsy @skirpichev
Update Lib/test/test_syntax.py
9e22cfa
Co-authored-by: Sergey B Kirpichev <[email protected]>
@skirpichev
skirpichev dismissed their stale review June 14, 2026 02:17

Second look.

@skirpichev

skirpichev commented Jun 14, 2026

Copy link
Copy Markdown
Member

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.

@skirpichev skirpichev mentioned this pull request Jun 14, 2026
gh-151303 : Improve SyntaxError suggestions for common operator typos and cross-language mistakes #151375
Open
@Aniketsy

Aniketsy commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

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 ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 14, 2026

Copy link
Copy Markdown
Member

should we fall back to generic error message if we have gap between them ?

Yes, I think so.

@serhiy-storchaka, @sobolevn ?

Aniketsy reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy

Aniketsy commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

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;
      |                      ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 14, 2026

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jun 14, 2026

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jun 14, 2026

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Jun 14, 2026

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn

sobolevn commented Jun 14, 2026

Copy link
Copy Markdown
Member

In my opinion && and & & are both incorrect, the error message is good enough.

Aniketsy reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy

Aniketsy commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy

Aniketsy commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Also please let me know your thoughts on this as well #151375

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 14, 2026

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy
add helper function in parser
92e502a
skirpichev
skirpichev reviewed Jun 14, 2026
View reviewed changes

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction
@Aniketsy
add tests with space
476243d
skirpichev
skirpichev reviewed Jun 14, 2026
View reviewed changes
Comment thread Lib/test/test_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy
improve tests
7f86edd
skirpichev
skirpichev approved these changes Jun 14, 2026
View reviewed changes
serhiy-storchaka
serhiy-storchaka reviewed Jun 14, 2026
View reviewed changes
Comment thread Lib/test/test_syntax.py Outdated

@serhiy-storchaka serhiy-storchaka Jun 14, 2026

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction
Comment thread Lib/test/test_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_syntax.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@Aniketsy
improve tests
1415a98
@Aniketsy

Aniketsy commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

gentle ping! should we move this forward so that we can also address the follow up PR. Thanks!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Jun 24, 2026

Copy link
Copy Markdown
Member

@pablogsal, could you review the current version?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pablogsal
pablogsal approved these changes Jun 27, 2026
View reviewed changes

@pablogsal pablogsal left a comment

Copy link
Copy Markdown
Member

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
All reactions
  • ❤️ 1 reaction
@pablogsal
pablogsal merged commit 0f501b9 into python:main Jun 27, 2026
54 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
@skirpichev skirpichev skirpichev approved these changes
@sobolevn sobolevn sobolevn approved these changes
@pablogsal pablogsal pablogsal approved these changes
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
+1 more reviewer
@github-advanced-security github-advanced-security[bot] github-advanced-security[bot] left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Consider specialized syntax error for && and ||

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@Aniketsy @skirpichev @serhiy-storchaka @sobolevn @pablogsal @github-advanced-security
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.