Irrefutable pattern before the last case in a match statement passes type checking but is a SyntaxError at runtime
まだ誰も着手していません。
評価
調査の方向性
repro.py の例から始め、guarded capture とネストされた or-pattern を含めて、mypy repro.py と Python 3.13 でのコンパイル結果を比較します。既存の or-pattern の検証を追跡し、不足している到達不能パターンのチェックを追加します。mypy が無効な非 final の irrefutable pattern を報告し、有効な guarded case または final case を受け入れれば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 3時間
- マージ済み PR(30日)
- 59
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
bug topic-attrs
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
似ている issue
-
area: harness bug status: needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
Human-Agent-Society/reef#625 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 80/100
learningequality/kolibri#15351 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
Name consistency オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
eellak/triplestore#65 · コメント 1 件 ·