MCVE:
class A:
def __init__(self, num: int):
self.value = num
class B(A):
def __init__(self): # Mypy: function is missing a type annotation
A.__init__(self, 7) # Mypy: Expression has type "Any"
I would like Mypy to not force me to have to type self
. It seems obvious to me what the type of self
is, and Mypy is able to figure it out for A
, so why not B
?
How can I define B
such that I'm not forced to do the following?
class A:
def __init__(self, num: int):
self.value = num
class B(A):
def __init__(self: 'B'):
A.__init__(self, 7)
You need to annotate the return type of your signature:
class Foo:
def __init__(self) -> None:
pass
Mypy will let you omit the return type on specifically the constructor if one or more other arguments are annotated, but you'll need to include it on no-argument constructors.