MyPy treats initialization of a variable as an assignment for variance checks for complex dictionary.
未关闭
还没有人认领这个 Issue。
bug
topic-type-context
- 主要语言
- Python
- 星标
- 20.6k
- 派生
- 3.3k
- PR 合并指标
- PR 指标待抓取
描述
The problem is that when initializing a variable from a dictionary expression it treats the type as invariant, but in the case of initialialization, it is covariant.
Example:
CODES = {
"a": "b",
"c": "d",
"e": "f",
}
# MyPy rejects this
PATTERNS: dict[str, tuple[str | None, str | None]] = {
"1" + k: ((v + "1" if v else None), "x")
for (k, v) in CODES.items()
} | {
"2" + k: ((v + "2" if v else None), "x")
for (k, v) in CODES.items()
}
# But is OK with this:
X: str | None = "x"
PATTERNS2: dict[str, tuple[str | None, str | None]] = {
"1" + k: ((v + "1" if v else None), X)
for (k, v) in CODES.items()
} | {
"1" + k: ((v + "1" if v else None), X)
for (k, v) in CODES.items()
}
Both patterns should be OK.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先运行提供的 MyPy 示例,并比较 PATTERNS 和 PATTERNS2 的诊断结果。跟踪与带注解初始化和方差检查结合的字典表达式的类型检查路径。添加一个覆盖该示例的回归测试;完成的标准是两个模式都被接受,同时不削弱无关的方差检查。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 38/100