python / python/cpython

Improve SyntaxError message for inconsistent name binding in OR-patterns

Đang mở
#145,019 5 bình luận 0 reaction 0 người được giao Xem trên GitHub

@picnixz đang làm issue này rồi.

Từ ngày 14/3/2026.

  • #145939 của @picnixz — đang mở
interpreter-core type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc xem xét PR #145939 được liên kết, sau đó kiểm tra logic xác thực OR-patterns trong Python/ast.c và mã trình biên dịch liên quan. Chạy các bài kiểm thử pattern-matching phù hợp trong Lib/test/test_patma.py. Công việc được xem là hoàn tất khi phạm vi kiểm thử hồi quy xác minh rằng các bindings không nhất quán xác định được những tên không khớp mà không thay đổi cú pháp hoặc hành vi lúc chạy.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.