confusing behavior with TypeVars and multiple inheritance checks in 0.770
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Hi, I was just testing out 0.770 and I'm getting a surprising result:
from typing import TypeVar
class Foo(object):
pass
FooOrStr = TypeVar('FooOrStr', Foo, str)
def doit(reset: bool, arg: FooOrStr) -> FooOrStr:
if reset:
if isinstance(arg, Foo):
reveal_type(arg)
arg = Foo()
elif isinstance(arg, str):
reveal_type(arg)
arg = ''
else:
raise TypeError()
return arg
test.py:12: note: Revealed type is 'test.Foo*'
test.py:12: note: Revealed type is 'test.<subclass of "str" and "Foo">'
test.py:13: error: Incompatible types in assignment (expression has type "Foo", variable has type "str") [assignment]
test.py:15: note: Revealed type is 'builtins.str*'
Based on the latest blog post about 0.770 I suspect this is by design, but what can I do to work around it (short of adding some artificial method to make Foo incompatible with str)?
It would be good to add something to the docs about this, because I think it could be a fairly common trap. Also, I think adjusting the error message to clarify the relationship to multiple inheritance would help, e.g. variable has type Foo and 'test.<subclass of "str" and "Foo">'.
Thanks!
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず mypy 0.770 を実行するか、issue にある TypeVar と多重継承の例を実行して、明らかになった型と代入エラーを再現してください。次に、関連する TypeVar と型の絞り込みに関するドキュメントおよび診断テストを読み、そのうえで挙動、回避策、Foo と推論されたサブクラス型の関係をより明確に表現する文言を文書化してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools, tooling
- issue の種類
- ドキュメント
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 30/100