我的 mypy 遇到一些问题。
我有一个抽象类和一个继承它的类:
from __future__ import annotations
from abc import abstractmethod, ABC
from typing import Union
class Base(ABC):
@abstractmethod
def the_method(self, a_class: Union[Base, float, int]) -> None:
...
@abstractmethod
def other_method(self) -> None:
...
class MyClass(Base):
def __init__(self, something: str = "Hello") -> None:
self.something = something
def the_method(self, a_class: Union[MyClass, float, int]) -> None:
print(a_class)
def other_method(self) -> None:
print(self.something)
Run Code Online (Sandbox Code Playgroud)
我知道里氏替换原则。然而MyClass是 的一种类型,Base因为它继承自它。但mypy仍然会引发错误:
from __future__ import annotations
from abc import abstractmethod, …Run Code Online (Sandbox Code Playgroud)