Skip to content

gh-163121: Fix _= in a class pattern after a positional pattern - #163126

Open
Robertboy18 wants to merge 1 commit into
python:mainfrom
Robertboy18:gh-163121-wildcard-keyword
Open

Robertboy18 wants to merge 1 commit into
python:mainfrom
Robertboy18:gh-163121-wildcard-keyword

Conversation

@Robertboy18

@Robertboy18 Robertboy18 commented Dec 4, 2026 •

Copy link
Copy Markdown

@gvanrossum here's the fix for #163121 :)

The problem was that wildcard_pattern is just "_", while pattern_capture_target and value_pattern both have a !('.' | '(' | '=') lookahead. So in case C(p, _=x):, positional_patterns took the _ as a wildcard and then got stuck at the =. This adds !'=' to wildcard_pattern (and regenerates Parser/parser.c), so _=x is parsed as a keyword pattern like any other name. I only added the = part, so case _.x: and case _(a): stay syntax errors, as they are now.

Since a pattern is never followed by = in valid code, nothing that compiled before changes. A few errors for invalid class patterns do change, and they now match what any other keyword name gives. E.g. case C(p, _=x, q): now says "positional patterns follow keyword patterns", like case C(p, a=x, q):, instead of "invalid syntax".

The new tests in test_patma and test_syntax both fail on main.

I used AI tools to help write and review the patch, and checked it myself.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants