Skip to content

Reject _ as a match-pattern target - #25977

Merged
charliermarsh merged 2 commits into
mainfrom
charlie/reject-underscore-pattern-bindings
Jun 15, 2026
Merged

Reject _ as a match-pattern target#25977
charliermarsh merged 2 commits into
mainfrom
charlie/reject-underscore-pattern-bindings

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 14, 2026

Copy link
Copy Markdown
Member

Summary

Python reserves _ for wildcard patterns and does not allow it as the binding target in either of these forms:

match value:
    case 1 as _:
        pass
    case {**_}:
        pass

We currently recover an AST for both forms without reporting a parser error. Both are grammar errors in CPython, so this change validates the capture target while parsing as and mapping patterns and reports cannot use '_' as a target on the underscore.

We previously tried to address the mapping-pattern case in #6838 by representing the rest target as a general PatternMatchAs node. This keeps the existing Identifier representation and recovered AST, including statements after the invalid match, while rejecting the invalid target at the parser boundary.

@astral-sh-bot

astral-sh-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot

astral-sh-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Full report with detailed diff (timing results)

@astral-sh-bot

astral-sh-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh added the parser Related to the parser label Jun 14, 2026
@charliermarsh
charliermarsh requested a review from ntBre June 14, 2026 14:03
@charliermarsh
charliermarsh marked this pull request as ready for review June 14, 2026 14:03
@charliermarsh
charliermarsh force-pushed the charlie/reject-underscore-pattern-bindings branch from 376c134 to 2a6ce00 Compare June 14, 2026 22:59
@MichaReiser

Copy link
Copy Markdown
Member

I'm not sure using semantic syntax errors is appropriate here, given that ast.parse rejects these patterns too. We primarily use semantic syntax errors for what are compile errors in CPython.

@charliermarsh
charliermarsh force-pushed the charlie/reject-underscore-pattern-bindings branch from 2a6ce00 to c23c3c2 Compare June 15, 2026 13:23
@charliermarsh
charliermarsh marked this pull request as draft June 15, 2026 13:30
@charliermarsh
charliermarsh force-pushed the charlie/reject-underscore-pattern-bindings branch from c23c3c2 to 70fb60f Compare June 15, 2026 13:35
@codspeed-hq

codspeed-hq Bot commented Jun 15, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.52%

❌ 1 regressed benchmark
✅ 130 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime ty_module_resolver[25] 1.4 ms 1.4 ms -4.52%

Tip

Investigate this regression with the CodSpeed MCP and your agent.


Comparing charlie/reject-underscore-pattern-bindings (53bd691) with main (cfa4d72)

Open in CodSpeed

@charliermarsh
charliermarsh force-pushed the charlie/reject-underscore-pattern-bindings branch from 70fb60f to 53bd691 Compare June 15, 2026 13:40
@charliermarsh
charliermarsh marked this pull request as ready for review June 15, 2026 13:40

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks. Would you mind updating the summary before landing

@charliermarsh
charliermarsh enabled auto-merge (squash) June 15, 2026 13:45
@charliermarsh
charliermarsh merged commit b6ab2b3 into main Jun 15, 2026
58 checks passed
@charliermarsh
charliermarsh deleted the charlie/reject-underscore-pattern-bindings branch June 15, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

parser Related to the parser

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants