Skip to content

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

Merged
gvanrossum merged 1 commit into
python:mainfrom
Robertboy18:gh-163121-wildcard-keyword
Dec 4, 2026
Merged

gvanrossum merged 1 commit into
python:mainfrom
Robertboy18:gh-163121-wildcard-keyword

Conversation

@Robertboy18

@Robertboy18 Robertboy18 commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

@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.

@gvanrossum gvanrossum added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Dec 4, 2026
@gvanrossum
gvanrossum merged commit 81d9755 into python:main Dec 4, 2026
67 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @Robertboy18 for the PR, and @gvanrossum for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

GH-163164 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Dec 4, 2026
@bedevere-app

bedevere-app Bot commented Dec 4, 2026

Copy link
Copy Markdown

GH-163165 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants