Skip to content

Cannot inference common base class when root of hierarchy is Any #5134

Description

@asottile

I believe this to be a bug

code

(this has been boiled down from a more complete example)

from typing import Type

from external import ExtBase


class Base(ExtBase):
    pass


class Concrete1(Base):
    pass


class Concrete2(Concrete1):
    pass


def f(x: Type[Base]) -> None:
    pass


def g(x: bool) -> None:
    #reveal_type(Base)
    #reveal_type(Concrete1)
    #reveal_type(Concrete2)
    #reveal_type(Concrete1 if x else Concrete2)
    f(Concrete1 if x else Concrete2)

output

$  mypy t2.py --ignore-missing-imports
t2.py:27: error: Argument 1 to "f" has incompatible type "Type[object]"; expected "Type[Base]"

output with types revealed

$ mypy t2.py --ignore-missing-imports
t2.py:23: error: Revealed type is 'def (*_args: Any, **_kwds: Any) -> t2.Base'
t2.py:24: error: Revealed type is 'def (*_args: Any, **_kwds: Any) -> t2.Concrete1'
t2.py:25: error: Revealed type is 'def (*_args: Any, **_kwds: Any) -> t2.Concrete2'
t2.py:26: error: Revealed type is 'def (*_args: Any, **_kwds: Any) -> builtins.object'
t2.py:27: error: Argument 1 to "f" has incompatible type "Type[object]"; expected "Type[Base]"

version information

$ mypy --version
mypy 0.600

against master

same error

peculiarly working

diff --git a/t2.py b/t2.py
index 91a8309..6f35532 100644
--- a/t2.py
+++ b/t2.py
@@ -24,4 +24,8 @@ def g(x: bool) -> None:
     #reveal_type(Concrete1)
     #reveal_type(Concrete2)
     #reveal_type(Concrete1 if x else Concrete2)
-    f(Concrete1 if x else Concrete2)
+    if x:
+        t = Concrete1
+    else:
+        t = Concrete2
+    f(t)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions