Checking a match on an enum becomes quadratically slow after the variable was narrowed by an == check
未关闭
还没有人认领这个 Issue。
performance
topic-enum
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从链接的 Playground 复现开始,比较有无提前进行 x == E.M0 检查时的类型检查时间。追踪 mypy 中的枚举缩窄和 match 检查路径,然后验证大型枚举示例不再出现二次方级别的变慢,同时保留报告中的缩窄行为。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers, devtools
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 活跃
- 描述清晰度
- 基本清楚
- 新手友好度
- 55/100