Refactor parial types to avoid code duplication
未关闭
还没有人认领这个 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 incheck_assignment()forNonepartial types. These two probably can be refactored to the same method if we also updatehandle_partial_var_type()to not special-case partialNonetypes, and instead consistently return a partial type in lvalue context. - Logic in
try_infer_partial_type_from_indexed_assignment()duplicates that intry_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 tocheck_call()), or by generating a syntheticCallExprwith__setitem__and passing it totry_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?
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 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