Unions with generic parameter reduces parameter too far
オープン
まだ誰も着手していません。
bug
topic-join-v-union
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When creating a method that takes a union of a type with a generic parameter it seems like the generic parameter is reduced to object instead of its original value.
To Reproduce
import typing as t
T = t.TypeVar('T', covariant=True)
Y = t.TypeVar('Y')
class Nothing(t.Generic[T]):
def chain_none(self: 'Nothing[t.Optional[Y]]') -> 'Nothing[Y]':
return self # type: ignore
class Just(t.Generic[T]):
def __init__(self, val: T) -> None:
self._value = val
def chain_none(
self: 'Just[t.Optional[Y]]') -> t.Union[Nothing[Y], 'Just[Y]']:
if self._value is None:
return Nothing()
return Just(self._value)
val: t.Union[Nothing[t.Union[None, int, str]], Just[t.Union[None, int, str]]]
reveal_type(val.chain_none())
This reveals 'Union[mypy_test.Nothing[builtins.object], mypy_test.Just[builtins.object]]' while I would expect 'Union[mypy_test.Nothing[Union[int, str]], mypy_test.Just[Union[int, str]]].
Please note that the example does work correctly when using Union[None, int] for val.
Your Environment
- Mypy version used: 0.790
- Python version used: 3.8.5
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された Python 3.8.5 の再現コードを mypy 0.790 で実行し、reveal_type の出力を期待される Union と比較してください。Union のケースにおけるジェネリックパラメーターの縮約を追跡し、その後、結果が Union[int, str] を保持し、object に縮約されないことを示すカバレッジを追加してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100