Argument type `Union[T, List[T]]` fails to infer constaints
オープン
まだ誰も着手していません。
bug
topic-union-types
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
説明
While working on #11128 I got hit by this.
This code does not type check:
from typing import TypeVar, Union, List
T1 = TypeVar("T1")
def promote_list(item: Union[T1, List[T1]]) -> List[T1]:
...
exprs: List[int]
reveal_type(promote_list(exprs)) # error
# ex.py:9: note: Revealed type is "builtins.list[<nothing>]"
# ex.py:9: error: Argument 1 to "promote_list" has incompatible type "List[int]"; expected "List[<nothing>]"
reveal_type(promote_list(1)) # ok
# Revealed type is "builtins.list[builtins.int*]"
Which is not what I expect. For example, TypeScript solves this properly:
function protomoteList<T>(input: T | T[]): T[] {
return []
}
function test(input: number[]) {}
test(protomoteList(1)) # ok
Cause
It happens somewhere here: https://github.com/python/mypy/blob/b3ff2a683bd41a176c597d3c3ba65f6a31f805c5/mypy/constraints.py#L168-L171
I will try to solve this, but I know that this is going to be complicated.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue の再現から始め、報告された原因が発生している mypy/constraints.py の 168〜171 行付近を調べてください。提供されている reveal_type 呼び出しを使って Union[T1, List[T1]] の現在の動作を確認してください。List[int] 型の引数が型チェックを通過し、結果が List[int] と推論されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers, tooling
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100