Metaclass confusion with properties
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
I'm using python 3.8 and mypy 0.740. The script below prints "21" and "42" when run, but fails to type check with mypy:
class meta (type):
pass
class value (metaclass=meta):
pass
class submeta (meta):
@property
def attr(cls) -> int:
return 42
class subvalue (metaclass=submeta):
@property
def attr(self) -> int:
return 21
o = subvalue()
a = o.attr
print(a)
a = subvalue.attr
print(a)
The mypy diagnostics say:
meta-test.py:24: error: Incompatible types in assignment (expression has type "Callable[[subvalue], int]", variable has type "int")
Found 1 error in 1 file (checked 1 source file)
The error is incorrect, "subvalue.attr" has type "int", as it resolves to the property defined on the metaclass, not to the property defined on the class.
This is fairly esoteric as written, but is the structure used by PyObjC to expose Objective-C classes. PyObjC uses metaclasses to expose class methods because ObjC classes can have instance- and class-methods with the same name.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
issue で説明されている meta-test.py の例を使い、Python 3.8 と mypy 0.740 で診断を再現します。まず、mypy が subvalue とその submeta メタクラスに対してプロパティをどのように解決するかを追跡します。完了の条件は、報告されている incompatible-assignment エラーなしに subvalue.attr が int と推論され、インスタンスアクセスは引き続き正しく動作することです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 38/100