Erronous 'Incompatible type' when using descriptor in metaclass
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When using descriptors in a metaclass, mypy will errounously throw 'Incompatible types in assignment' when accessing the described attribute in the derived class.
To Reproduce
` from typing import Generic, TypeVar, Any
from dataclasses import dataclass
@dataclass
class DUT:
Description: str = ""
ProductNumber: int = 0
T = TypeVar('T')
class DataDescriptor(Generic[T]):
def __init__(self, value: T) -> None:
self.value = value
def __set_name__(self, owner: Any, name: str) -> None:
self.name = name
def __get__(self, instance: Any, cls: Any) -> T:
return self.value
def __set__(self, obj: Any, value: T) -> None:
self.value = value
class DataModelMeta(type):
current_dut = DataDescriptor(DUT())
class DataModel(object, metaclass=DataModelMeta):
pass
dut: DUT = DUT("hello", 1234)
reveal_type(DataModel.current_dut)
reveal_type(dut)
DataModel.current_dut = dut`
# main.py:32: note: Revealed type is 'main.DUT*'
# main.py:33: note: Revealed type is 'main.DUT'
# main.py:34: error: Incompatible types in assignment (expression has type "DUT", variable has type "DataDescriptor[DUT]")
# Found 1 error in 1 file (checked 1 source file)
Expected Behavior
Assignment to DataModel.current_dut should expect the DUT type. mypy was able to infer this, as shown by reveal_type(DataModel.current_dut).
Actual Behavior
mypy throws an 'Incompatible type' error at the assignment
Your Environment
- Mypy version used: 0.790
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.8.6:db45529
- Operating system and version: Windows 7
This can also be reproduced on the latest Python / mypy at https://mypy-play.net
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
提供された descriptor と metaclass の再現コードから始め、示されている Python の例で mypy を実行し、metaclass を介した descriptor への代入の扱いを確認します。DataModel.current_dut = dut が受け入れられ、同時に reveal_type(DataModel.current_dut) が引き続き DUT であれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100