Irrefutable pattern before the last case in a match statement passes type checking but is a SyntaxError at runtime
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Bắt đầu với các ví dụ trong repro.py và so sánh mypy repro.py với quá trình biên dịch bằng Python 3.13, bao gồm các capture có guard và các or-pattern lồng nhau. Truy vết phần xác thực or-pattern hiện có và thêm kiểm tra pattern không thể đạt tới còn thiếu; hoàn thành khi mypy báo cáo các pattern irrefutable không phải final không hợp lệ, đồng thời chấp nhận các trường hợp có guard hoặc final hợp lệ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug Report
mypy reports nothing for match statements that CPython rejects at compile time with SyntaxError: name capture 'y' makes remaining patterns unreachable. An unguarded capture or wildcard pattern in any case except the last one makes the remaining cases unreachable, so the compiler refuses the file, but mypy checks it clean. That means a file that cannot even be imported passes type checking.
The same applies to an irrefutable alternative in a non-final position of an or pattern, e.g. case _ | 1:, which CPython rejects wherever the or pattern appears, including nested inside sequence, mapping and class patterns.
To Reproduce
def f(x: int) -> str:
match x:
case y:
return "capture"
case _:
return "wildcard"
Expected Behavior
An error on case y:, matching the runtime behavior:
$ python repro.py
File "repro.py", line 3
case y:
^
SyntaxError: name capture 'y' makes remaining patterns unreachable
For reference, a case is fine if it has a guard (case y if y > 0:), and an irrefutable pattern in the last case is fine. Inside an or pattern an irrefutable alternative is only allowed in the last position, regardless of guards or which case it is in.
Actual Behavior
$ mypy repro.py
Success: no issues found in 1 source file
mypy already implements the closely related compile time check for or patterns ("Alternative patterns bind different names"), so I think this one is missing rather than intentionally skipped. I have a fix ready and will put up a PR shortly.
Your Environment
- Mypy version used: mypy 2.4.0+dev.3ad6157f9b66e71738bf39cae939aef7c3fd7ea8 (current master, also with
--native-parser) - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.13.13
- Ngôn ngữ chính
- Python
- Star
- 20.6k
- Fork
- 3.3k
- Merge trung bình
- 1 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 56
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của python/mypy
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
bug topic-configuration topic-error-reporting
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Issue tương tự
-
sponsored
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
opensubtitlescom: moviehash never sent when opensubtitles (.org) is not in the provider list Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
Diaoul/subliminal#1382 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
triage/confirmed
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
agentscope-ai/agentscope#2775 ·
-
worker.gpuVendors silently accepts unsupported/misspelled vendor names — no validation guard Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100