From e309e5e298aaa5ff9c60ac56fe6b25b6d9555046 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Thu, 4 Jun 2026 10:31:16 +0000 Subject: [PATCH 1/3] Improve SyntaxError message for and operators --- Lib/test/test_syntax.py | 12 ++++++++++++ Parser/pegen_errors.c | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index a3d485c998ac915..1457dfd06a4c809 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -3505,6 +3505,18 @@ def test_ifexp_body_stmt_else_stmt(self): ]: self._check_error(f"x = {lhs_stmt} if 1 else {rhs_stmt}", msg) + def test_double_ampersand(self): + self._check_error( + "a && b", + "Maybe you meant 'and' or '&' instead of '&&'", + ) + + def test_double_pipe(self): + self._check_error( + "a || b", + "Maybe you meant 'or' or '|' instead of '||'", + ) + class LazyImportRestrictionTestCase(SyntaxErrorTestCase): """Test syntax restrictions for lazy imports.""" diff --git a/Parser/pegen_errors.c b/Parser/pegen_errors.c index 312699415efd9af..11e51346031b811 100644 --- a/Parser/pegen_errors.c +++ b/Parser/pegen_errors.c @@ -442,6 +442,16 @@ _Pypegen_set_syntax_error(Parser* p, Token* last_token) { RAISE_INDENTATION_ERROR(last_token->type == INDENT ? "unexpected indent" : "unexpected unindent"); return; } + if (last_token->type == AMPER && p->tokens[p->fill - 2]->type == AMPER) { + RAISE_SYNTAX_ERROR_KNOWN_RANGE(p->tokens[p->fill - 2], last_token, + "invalid syntax. Maybe you meant 'and' or '&' instead of '&&'"); + return; + } + if (last_token->type == VBAR && p->tokens[p->fill - 2]->type == VBAR) { + RAISE_SYNTAX_ERROR_KNOWN_RANGE(p->tokens[p->fill - 2], last_token, + "invalid syntax. Maybe you meant 'or' or '|' instead of '||'"); + return; + } // Unknown error (generic case) // Use the last token we found on the first pass to avoid reporting From 02f650adaa81f85acad72acc829b6f4b688d3efc Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Thu, 4 Jun 2026 14:55:56 +0000 Subject: [PATCH 2/3] Add error location assertions for && and || --- Lib/test/test_syntax.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 1457dfd06a4c809..c80332321697944 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -3509,12 +3509,20 @@ def test_double_ampersand(self): self._check_error( "a && b", "Maybe you meant 'and' or '&' instead of '&&'", + lineno=1, + end_lineno=1, + offset=3, + end_offset=5, ) def test_double_pipe(self): self._check_error( "a || b", "Maybe you meant 'or' or '|' instead of '||'", + lineno=1, + end_lineno=1, + offset=3, + end_offset=5, ) From 3b0f068641f8cf11de0069a8cec50058cb837736 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 4 Jun 2026 15:02:37 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2026-06-04-15-02-35.gh-issue-146495.wWRRvx.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-06-04-15-02-35.gh-issue-146495.wWRRvx.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-06-04-15-02-35.gh-issue-146495.wWRRvx.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-04-15-02-35.gh-issue-146495.wWRRvx.rst new file mode 100644 index 000000000000000..d9ca936165db1ab --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-06-04-15-02-35.gh-issue-146495.wWRRvx.rst @@ -0,0 +1 @@ +Improve SyntaxError message for ``&&`` and ``||`` operators, suggesting ``and``/``&`` and ``or``/``|`` respectively.