python / python/mypy

Refactor parial types to avoid code duplication

オープン
#8,043 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。