I am was trying to use __new__ to do some customizations to my class but I ran into some mypy errors. The following code is a simplified version.
from abc import ABC, abstractproperty
class A(ABC):
def __new__(cls, x: int) -> 'A':
return super().__new__(cls)
@abstractproperty
def log(self) -> None:
pass
class B(A):
def __new__(cls, x: str) -> 'B':
return super().__new__(cls, int(x))
def log(self) -> None:
print('Hello World')
The mypy errors were as follows:
test.py:5: error: Argument 1 to "__new__" of "object" has incompatible type "Type[A]"; expected "Type[object]"
test.py:13: error: Incompatible return value type (got "A", expected "B")
test.py:13: error: Argument 1 to "__new__" of "A" has incompatible type "Type[B]"; expected "Type[A]"