Skip to content

Join of actual Class with Type[Class] results in object #3976

Open
@carljm

Description

@carljm

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].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions