from abc import ABC
class A(ABC):
...
class B(A):
...
def foo(bar: A): ...
foo(B)
Mypy error: Argument 1 to "foo" has incompatible type "Type[B]"; expected "A"
Am I doing something wrong or it's just mypy bug?
CodePudding user response:
B
is a type. bar: A
means an object of type A
or anything inherited from A
(in this case A()
or B()
).
So either:
foo(B())
if you want to pass instances of classes, or
from typing import Type
def foo(bar: Type[A]): ...
foo(B)
if you want to pass classes themselves.
For python 3.9 :
def foo(bar: type[A]): ...
foo(B)
though mypy might have some issues with that.