Irrefutable pattern before the last case in a match statement passes type checking but is a SyntaxError at runtime
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 repro.py 中的示例开始,将 mypy repro.py 与 Python 3.13 的编译结果进行比较,包括带 guard 的捕获以及嵌套的 or-pattern。跟踪现有的 or-pattern 验证,并添加缺失的不可达 pattern 检查;完成的标准是 mypy 报告无效的非 final irrefutable pattern,同时接受有效的带 guard 或 final 情况。
由索引模型根据 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 天 1 小时
- 30 天内合并 PR
- 61
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python/mypy 的其他 Issue
-
bug documentation
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
documentation
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
bug topic-configuration topic-error-reporting
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
相似的 Issue
-
docs pydanty:is-working
难度 2/5 1-3 小时 新手友好度 75/100
pydantic/pydantic-ai#8863 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
run-llama/llama_index#23278 ·
维护者通常 2 天内回复
-
documentation from-review-extraction github-actions priority: low severity:nit
难度 1/5 1 小时以内 新手友好度 92/100
LearningCircuit/local-deep-research#6946 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
oracle/langchain-oracle#323 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
tenstorrent/tt-metal#58057 · 1 条评论 ·
维护者通常 1 天内回复