python / python/cpython

Improve SyntaxError message for inconsistent name binding in OR-patterns

Aperta
#145,019 5 commenti 0 reazioni 0 assegnatari Vedi su GitHub

@picnixz ci sta già lavorando.

Dal 14/3/2026.

interpreter-core type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Feature or enhancement

Proposal:

Title

compiler: improve SyntaxError message for inconsistent name binding in OR-patterns

Summary

When using structural pattern matching (PEP 634), all alternatives of an OR-pattern (|) are required to bind the same set of names.

Currently, if this rule is violated, CPython raises a SyntaxError with the message:

SyntaxError: alternative patterns bind different names

While technically correct, this message does not indicate which names differ or how the bindings are inconsistent.

This issue proposes enhancing the diagnostic to explicitly report the mismatched bindings.

Reproducer

match value:
    case 1 | x:
        pass

Output:

SyntaxError: alternative patterns bind different names

The message does not clarify:

  • Which variable(s) are inconsistently bound
  • Which alternative introduces additional bindings
  • What the required constraint is

Expected Behavior

The error message should clearly indicate the binding mismatch. For example:

SyntaxError: OR-pattern alternatives must bind the same names;
left alternative binds no names, right alternative binds {'x'}

or:

SyntaxError: inconsistent variable binding in OR-pattern: 'x' is not bound in all alternatives

The exact wording can be adjusted, but the diagnostic should identify the differing name(s).

Rationale

PEP 634 specifies that all OR-pattern alternatives must bind identical sets of names.

Providing more detailed diagnostics would:

  • Improve developer experience
  • Make the rule easier to understand
  • Align with recent improvements in CPython error reporting
  • Maintain consistency with Python’s philosophy of helpful error messages

Scope

  • No syntax changes
  • No semantic changes
  • No AST changes
  • Diagnostic enhancement only

Implementation Notes

The relevant validation logic appears to occur during semantic analysis of pattern matching (likely in Python/ast.c or related compiler validation code).

The change would involve augmenting the existing name-set comparison logic to include information about the differing bindings in the raised SyntaxError.

Backward Compatibility

This change affects only the wording of an error message and does not alter runtime behavior.

Tests

Regression tests can be added to Lib/test/test_patma.py to assert that the improved diagnostic includes information about mismatched variable names.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-145939

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando il PR #145939 collegato, quindi analizza la logica di validazione degli OR-patterns in Python/ast.c e il codice correlato del compilatore. Esegui i test pertinenti di pattern-matching in Lib/test/test_patma.py. Il lavoro è completato quando la copertura di regressione verifica che i bindings incoerenti identifichino i nomi non corrispondenti senza modificare la sintassi o il comportamento a runtime.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.