Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
carljm opened this issue Sep 21, 2017 · 2 comments
Open

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

carljm opened this issue Sep 21, 2017 · 2 comments
Labels
bug mypy got something wrong priority-1-normal

Comments

@carljm
Copy link
Member

carljm commented Sep 21, 2017

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

@ilevkivskyi
Copy link
Member

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

It looks like this will be easy to fix, just by adding a special case for callable types with .is_type_obj() returning True.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-1-normal labels Sep 21, 2017
@ilevkivskyi
Copy link
Member

Although original example now passes, this is because now we infer a (simplified) union from the ternary expression. I will keep this open for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong priority-1-normal
Projects
None yet
Development

No branches or pull requests

2 participants