From da6a1009954f8fbdf653a8a49597e28d6cb43965 Mon Sep 17 00:00:00 2001 From: Robert Joseph Date: Fri, 4 Dec 2026 12:03:42 -0800 Subject: [PATCH] gh-163121: Fix `_=` in a class pattern after a positional pattern (GH-163126) (cherry picked from commit 81d97554fb130c280f5ee505a1071bc435c0c618) Co-authored-by: Robert Joseph --- 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 a8ac90d8b23c467..9d4df2c0182cf9e 100644 --- a/Grammar/python.gram +++ b/Grammar/python.gram @@ -587,7 +587,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 0825d9d980c5e7f..1522d0cd4d80965 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 f381bb3c117af58..f4cf3a38e9759ce 100644 --- a/Lib/test/test_syntax.py +++ b/Lib/test/test_syntax.py @@ -2373,6 +2373,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 000000000000000..4f4de1ccdcfd392 --- /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 14e7f8774859f97..5340cbccb655683 100644 --- a/Parser/parser.c +++ b/Parser/parser.c @@ -9647,7 +9647,7 @@ pattern_capture_target_rule(Parser *p) return _res; } -// wildcard_pattern: "_" +// wildcard_pattern: "_" !'=' static pattern_ty wildcard_pattern_rule(Parser *p) { @@ -9669,18 +9669,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--; @@ -9700,7 +9702,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: