@property returning descriptor instance triggers false positive
オープン
まだ誰も着手していません。
bug
topic-descriptors
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
🐛 Bug Report
Accessing a field implementing the descriptor protocol via a property method causes MyPy to complain that there is no matching overload for __get__().
To Reproduce
Run MyPy with all default settings on the following code:
from typing import Optional
from typing import overload
from typing import Type
class A:
pass
class Field:
def __init__(self, attr: Optional["Field"] = None):
self.attr = attr
@property
def crashing_property(self) -> Optional["Field"]:
return self.attr
@overload
def __get__(self, instance: None, owner: Type["A"]) -> "Field":
...
@overload
def __get__(self, instance: "A", owner: Type["A"]) -> Optional["Field"]:
...
def __get__(self, instance, owner):
if not instance:
return self
return self.attr
f = Field() # type: Field
f.attr # Works fine
f.crashing_property # Triggers "no overload variant"
n.b. adding __set__() makes no difference
Expected Behavior
Accessing crashing_property shouldn't cause an error.
Actual Behavior
The last line in the example throws the following error:
test.py:39: error: No overload variant of "__get__" of "Field" matches argument types "Field", "Type[Field]"
test.py:39: note: Possible overload variants:
test.py:39: note: def __get__(self, instance: None, owner: Type[A]) -> Field
test.py:39: note: def __get__(self, instance: A, owner: Type[A]) -> Optional[Field]
If you add this the code validates fine:
@overload
def __get__(self, instance: "Field", owner: Type["Field"]):
...
Your Environment
- Mypy version used: 0.782, 0.800, 0.812, 0.910
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None, using all defaults - Python version used: CPython 3.7.7, 3.8.6, 3.9.1
- Operating system and version: Ubuntu 20.04, MacOS 11.1 (Big Sur)
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供されている Python 再現コードを MyPy のデフォルト設定で実行し、プロパティアクセスで誤検出される overload エラーを確認します。Field インスタンスに追加の get overload を要求せずにそのアクセスが検証され、既存のディスクリプタの動作も引き続き有効であれば、作業は完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100