Type of argument is not inferred for a TypeVar in a Generic type
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Mypy needs a type annotation for a variable that it could easily infer from the constructor argument annotations. This works perfectly with normal types. But using a TypeVar in a Generic type, somehow it doesn't.
from typing import Generic, TypeVar
class A:
pass
class B:
pass
T_Value = TypeVar("T_Value", A, B)
class Guess(Generic[T_Value]):
def __init__(self, value: T_Value, confidence: float) -> None:
self.value = value # <-- line 17
self.confidence = confidence
Mypy then complains it needs a type annotation
foo.py:17: error: Need type annotation for 'value'
This of course is no big deal since I can just add that annotation:
class Guess(Generic[T_Value]):
def __init__(self, value: T_Value, confidence: float) -> None:
self.value: T_Value = value
self.confidence = confidence
But shouldn't mypy infer the type from the type annotations in the constructor?
If I'm doing something wrong please let me now.
I didn't find a similar issue but if this is a duplicate feel free to close.
I'm using:
Python 3.7.0, mypy 0.610, macOS 10.13.5
Same result with the current master (2018-07-05):
mypy 0.620+dev-f5058bcb269a55b33a02c3e3fd345541839bf06e
Flags I'm using:
strict_optional = true
ignore_missing_imports = true
follow_imports = silent
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
報告された strict_optional、ignore_missing_imports、follow_imports の設定で inline Python の例を再現します。まず、Generic TypeVar 属性の代入に対する mypy の推論を追跡します。例で明示的なアノテーションが不要になり、その動作が回帰テストでカバーされれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100