Calling a method of Union of two classes with class's attribute results in incompatible type error
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Honestly didn't know how to name this issue...
Issue type: Bug
Problematic code:
from typing import Union
class UserTypeA: ...
class UserTypeB: ...
class A:
def permissions_for(self, user: UserTypeA) -> bool: ...
@property
def me(self) -> UserTypeA: ...
class B:
def permissions_for(self, user: UserTypeB) -> bool: ...
@property
def me(self) -> UserTypeB: ...
def func(x: Union[A, B]) -> bool:
return x.permissions_for(x.me)
Error message:
main.py:20: error: Argument 1 to "permissions_for" of "A" has incompatible type "Union[UserTypeA, UserTypeB]"; expected "UserTypeA"
main.py:20: error: Argument 1 to "permissions_for" of "B" has incompatible type "Union[UserTypeA, UserTypeB]"; expected "UserTypeB"
Expected behaviour:
No error as x.me matches the type expected by x.permissions_for - if instance of A is passed x.me will have type UserTypeA and x.permissions_for will expect UserTypeA and same for instance of B.
Also, if func looks like this, the errors are gone so if mypy could infer this without isinstance it would be great:
def func(x: Union[A, B]) -> bool:
if isinstance(x, A):
return x.permissions_for(x.me)
else:
return x.permissions_for(x.me)
Python version:: 3.8.1
Mypy version: 0.761, occurs on master as well
Mypy flags: No flags
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
mypy 0.761 または現在の master を使って main.py の最小例を再現し、x.permissions_for(x.me) を通した Union[A, B] の呼び出しに重点を置きます。各クラスのプロパティがそのメソッドのパラメーターに一致する場合に、func が isinstance なしで型チェックを通過し、実際に互換性のない呼び出しは引き続きエラーになることが完了条件です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 35/100