From b507144ce036c15f36530cee13e442bc603aa120 Mon Sep 17 00:00:00 2001 From: Robert Joseph George Date: Fri, 4 Dec 2026 03:14:15 +0000 Subject: [PATCH] gh-163121: Fix `_=` in a class pattern after a positional pattern --- Grammar/python.gram | 2 +- Lib/test/test_patma.py | 25 +++++++++++++++++++ Lib/test/test_syntax.py | 6 +++++ ...-12-04-03-13-04.gh-issue-163121.SfPhTN.rst | 2 ++ Parser/parser.c | 12 +++++---- 5 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-03-13-04.gh-issue-163121.SfPhTN.rst diff --git a/Grammar/python.gram b/Grammar/python.gram index a3312753f5bde8..281d9645d2114a 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -580,7 +580,7 @@ pattern_capture_target[expr_ty]: _PyPegen_set_expr_context(p, name, Store) } wildcard_pattern[pattern_ty]: - | "_" { _PyAST_MatchAs(NULL, NULL, EXTRA) } + | "_" !'=' { _PyAST_MatchAs(NULL, NULL, EXTRA) } value_pattern[pattern_ty]: | attr=attr !('.' | '(' | '=') { _PyAST_MatchValue(attr, EXTRA) } diff --git a/Lib/test/test_patma.py b/Lib/test/test_patma.py index 0825d9d980c5e7..1522d0cd4d8096 100644 --- a/Lib/test/test_patma.py +++ b/Lib/test/test_patma.py @@ -2979,6 +2979,31 @@ class B(A): ... h = 1 self.assertEqual(h, 1) + def test_patma_underscore_keyword_after_positional(self): + # gh-163121: "_" right after a positional pattern was parsed as a + # wildcard, so "_=" there was a SyntaxError. + class C: + __match_args__ = ("a",) + def __init__(self): + self.a = 1 + self._ = 2 + self.b = 3 + + match C(): + case C(p, _=x, b=y): + z = 0 + self.assertEqual((p, x, y, z), (1, 2, 3, 0)) + match C(): + case C(_, _=x): + z = 1 + self.assertEqual((x, z), (2, 1)) + match C(): + case C(1, _=3): + z = 2 + case C(1, _=2): + z = 3 + self.assertEqual(z, 3) + class TestSyntaxErrors(unittest.TestCase): diff --git a/Lib/test/test_syntax.py b/Lib/test/test_syntax.py index 04c759310d3d95..323e258b7ce1e3 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2433,6 +2433,12 @@ Traceback (most recent call last): SyntaxError: positional patterns follow keyword patterns + >>> match ...: + ... case Foo(a, _=1, x): + ... ... + Traceback (most recent call last): + SyntaxError: positional patterns follow keyword patterns + >>> match ...: ... case Foo(z=1, x, y=2): ... ... diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-03-13-04.gh-issue-163121.SfPhTN.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-03-13-04.gh-issue-163121.SfPhTN.rst new file mode 100644 index 00000000000000..4f4de1ccdcfd39 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-04-03-13-04.gh-issue-163121.SfPhTN.rst @@ -0,0 +1,2 @@ +Fix a :exc:`SyntaxError` when ``_`` is used as a keyword name in a class +pattern right after a positional pattern, as in ``case C(p, _=x):``. diff --git a/Parser/parser.c b/Parser/parser.c index 2300314a77888f..1b0722fe126db9 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -9549,7 +9549,7 @@ pattern_capture_target_rule(Parser *p) return _res; } -// wildcard_pattern: "_" +// wildcard_pattern: "_" !'=' static pattern_ty wildcard_pattern_rule(Parser *p) { @@ -9571,18 +9571,20 @@ wildcard_pattern_rule(Parser *p) UNUSED(_start_lineno); // Only used by EXTRA macro int _start_col_offset = p->tokens[_mark]->col_offset; UNUSED(_start_col_offset); // Only used by EXTRA macro - { // "_" + { // "_" !'=' if (p->error_indicator) { p->level--; return NULL; } - D(fprintf(stderr, "%*c> wildcard_pattern[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "\"_\"")); + D(fprintf(stderr, "%*c> wildcard_pattern[%d-%d]: %s\n", p->level, ' ', _mark, p->mark, "\"_\" !'='")); expr_ty _keyword; if ( (_keyword = _PyPegen_expect_soft_keyword(p, "_")) // soft_keyword='"_"' + && + _PyPegen_lookahead_with_int(0, _PyPegen_expect_token, p, 22) // token='=' ) { - D(fprintf(stderr, "%*c+ wildcard_pattern[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "\"_\"")); + D(fprintf(stderr, "%*c+ wildcard_pattern[%d-%d]: %s succeeded!\n", p->level, ' ', _mark, p->mark, "\"_\" !'='")); Token *_token = _PyPegen_get_last_nonnwhitespace_token(p); if (_token == NULL) { p->level--; @@ -9602,7 +9604,7 @@ wildcard_pattern_rule(Parser *p) } p->mark = _mark; D(fprintf(stderr, "%*c%s wildcard_pattern[%d-%d]: %s failed!\n", p->level, ' ', - p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "\"_\"")); + p->error_indicator ? "ERROR!" : "-", _mark, p->mark, "\"_\" !'='")); } _res = NULL; done: