Checking a match on an enum becomes quadratically slow after the variable was narrowed by an == check
还没有人认领这个 Issue。
评估
调研方向
从链接的 Playground 复现开始,比较有无提前进行 x == E.M0 检查时的类型检查时间。追踪 mypy 中的枚举缩窄和 match 检查路径,然后验证大型枚举示例不再出现二次方级别的变慢,同时保留报告中的缩窄行为。
由索引模型根据 Issue 内容生成。
描述
Bug Report
Type-checking a match on a large enum becomes very slow when the variable was first narrowed by an == check with an early exit:
from enum import StrEnum
class E(StrEnum):
M0 = "m0"
M1 = "m1"
# ... 1,200 members
def f(x: E) -> None:
if x == E.M0:
return
match x:
case E.M1:
pass
# ... through case E.M7
Removing the x == E.M0 block makes this much faster (~1.5s vs 22s locally).
To Reproduce
Your Environment
- Mypy version used:
2.3.1,2.4.0+dev.4d195a2 - Python version used: 3.12
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- 平均合并
- 1 天 12 小时
- 30 天内合并 PR
- 58
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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
-
难度 2/5 1-3 小时 新手友好度 75/100
anthropics/skills#1811 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
speaches-ai/speaches#678 ·
-
bug
难度 2/5 1-3 小时 新手友好度 75/100
datalayer/mcp-compose#42 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
conda-forge/spacy-feedstock#177 ·
-
难度 2/5 1-3 小时 新手友好度 70/100
UKGovernmentBEIS/inspect_evals#2523 ·