python / python/mypy

Refactor parial types to avoid code duplication

未关闭
#8,043 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

priority-1-normal refactoring
主要语言
Python
星标
20.6k
派生
3.3k
PR 合并指标
PR 指标待抓取

描述

Currently there is some duplication around partial types:

  • Logic in try_infer_partial_generic_type_from_assignment() essentially duplicates the logic inlined in check_assignment() for None partial types. These two probably can be refactored to the same method if we also update handle_partial_var_type() to not special-case partial None types, and instead consistently return a partial type in lvalue context.
  • Logic in try_infer_partial_type_from_indexed_assignment() duplicates that in try_infer_partial_type() (there is already a TODO item). This one can be refactored by either pushing the latter a bit down the call stack (closer to check_call()), or by generating a synthetic CallExpr with __setitem__ and passing it to try_infer_partial_type().

The second item may be something to watch out when implementing support for these:

a = defaultdict(list)
a[0].append('yes')

b = {}
b.setdefault(0, []).append('yes')

c = defaultdict(set)
c[0].add('yes')

d = {}
d.setdefault(0, set()).add('yes')

@JukkaL this is probably not something important, but maybe it makes sense to fix this while we are at it?

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先定位 try_infer_partial_generic_type_from_assignment()、check_assignment()、handle_partial_var_type()、try_infer_partial_type_from_indexed_assignment() 和 try_infer_partial_type()。比较它们对部分类型的处理,然后将 defaultdict 和 setdefault 示例作为回归用例进行评估;完成的标准是统一重复逻辑,同时不破坏这些推断场景。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
compilers, devtools
Issue 类型
重构
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。