Skip to content

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

Open
picnixz wants to merge 5 commits intopython:mainfrom
picnixz:feat/parser/syntax-error-match-145019
Open

gh-145019: improve SyntaxError when match patterns bind different names#145939
picnixz wants to merge 5 commits intopython:mainfrom
picnixz:feat/parser/syntax-error-match-145019

Conversation

@picnixz
Copy link
Copy Markdown
Member

@picnixz picnixz commented Mar 14, 2026

Co-authored-by: AN Long <aisk@users.noreply.github.com>
@picnixz
Copy link
Copy Markdown
Member Author

picnixz commented Apr 3, 2026

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.

@picnixz
Copy link
Copy Markdown
Member Author

picnixz commented Apr 11, 2026

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

Improve SyntaxError message for inconsistent name binding in OR-patterns

4 participants