Join of actual Class with Type[Class] results in `object`

オープン
#3,976 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python
領域
compilers

調査の方向性

Start with the provided Python reproducer and trace mypy's handling of the conditional-expression type join involving the actual Foo class and Type[Foo]. Run the example to confirm the revealed type and call error; done means the expression is inferred as Type[Foo], Foo can be called, and the existing reversed-order and cls or Foo cases remain covered.

索引モデルが issue の本文から書いたものです。

説明

bug priority-1-normal

This file:

from typing import Type, Optional

class Foo:
    pass

def make_foo(cls: Optional[Type[Foo]] = None) -> Foo:
    x = Foo if cls is None else cls
    reveal_type(x)
    return x()

gives the following:

typeofclass.py:10: error: Revealed type is 'builtins.object' 
typeofclass.py:11: error: "object" not callable 

This example is superficially related to #3487, in that it uses an if/else expression to make mypy perform a type join, but the distinct bug here is that joining Foo (the actual class object) with Type[Foo] should result in Type[Foo], but instead results in object.

This is related to the fact that class objects are typed as callables instead of as Type[Class].

Oddly reversing Foo if cls is None else cls to cls if cls is not None else Foo results in builtin.type instead of object, which is still wrong, but differently wrong.

Using cls or Foo results in Type[Foo].

主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/mypy のほかの issue

python/mypy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。