Skip to content

gh-145019: improve SyntaxError when match patterns bind different names - #145939

Merged
picnixz merged 11 commits into
python:mainfrom
picnixz:feat/parser/syntax-error-match-145019
Oct 19, 2026
Merged

picnixz merged 11 commits into
python:mainfrom
picnixz:feat/parser/syntax-error-match-145019

Conversation

@picnixz

@picnixz picnixz commented Mar 14, 2026 •

Copy link
Copy Markdown
Member

Comment thread Lib/test/test_syntax.py Outdated
Comment thread Python/codegen.c Outdated
Co-authored-by: AN Long <aisk@users.noreply.github.com>
Comment thread Python/codegen.c Outdated
Comment thread Lib/test/test_syntax.py Outdated
@picnixz

picnixz commented Apr 3, 2026

Copy link
Copy Markdown
Member Author

Thanks Hugo for the review! I am unavailable this week-end so I will address this next week. If you want to apply your changes, feel free to do so! Otherwise I will do it once I have my laptop.

Comment thread Python/codegen.c Outdated
Comment thread Python/codegen.c Outdated
Comment thread Python/codegen.c Outdated
@picnixz

picnixz commented Apr 11, 2026

Copy link
Copy Markdown
Member Author

@hugovk @pablogsal Thanks for the comments! I decided not to compute the exclusive variable names because it's a bit annoying to do so. In pure Python it would be easy as I just need to do x_control = [c for c in control if c not in mismatch_names] and vice-versa. But then we need to distinguish when control is empty to print "nothing" or when x_control is empty (in which case "nothing" isn't the appropriate word so yet another message). For instance, ideally, we want to have:

>>> match 1:
...     case (y, z) | (x, y, z): pass
SyntaxError: pattern 1 does not bind "x" but pattern 3 does

instead of

>>> match 1:
...     case (y, z) | (x, y, z): pass
SyntaxError: pattern 1 binds ['y', 'z'] but pattern 3 binds ['x', 'y', 'z']

It would be annoying to do so and unledss there are hundreds of variables, I think it's ok to read it from the list. Please tell me whether you want a more precise solution though.

@picnixz
picnixz requested review from hugovk and pablogsal April 11, 2026 10:40
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 17, 2026
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 23, 2026

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

I left a nitpick in picnixz#3; other than that this looks good to me!

Use static constant (_Py_ID) for the string "nothing"
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 17, 2026
@picnixz
picnixz marked this pull request as draft October 19, 2026 17:25
@picnixz
picnixz marked this pull request as ready for review October 19, 2026 17:32
@picnixz picnixz self-assigned this Oct 19, 2026
@picnixz
picnixz merged commit 7641c5d into python:main Oct 19, 2026
60 of 62 checks passed
@picnixz
picnixz deleted the feat/parser/syntax-error-match-145019 branch October 19, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve SyntaxError message for inconsistent name binding in OR-patterns

5 participants