在运行时,以下代码完全有效:
class Base():
@abstractmethod
def get(self, arg: str, **kwargs: Any):
pass
class Derived(Base):
def get(self, arg: str, optional_arg: bool = False, **kwargs: Any):
pass
这样做的好处是,在静态分析期间,如果您碰巧知道您有一个类型的变量,Derived
您将获得额外的好处,即发现可选的命名参数optional_arg
及其类型。
我认为这Derived
是以兼容的方式覆盖get
这里,但是,PyRight 说覆盖是不兼容的:
Method "get" overrides class "Base" in an incompatible manner
Parameter 3 type mismatch: base parameter is type "Any", override parameter is type "bool"
PylancereportIncompatibleMethodOverride
使用overload
装饰器的尝试对我来说也失败了,所以我相信我也没有正确使用它:
class Derived(Base):
@overload
def get(self, path: Path, local_path: Path, is_directory: bool = False):
...
def get(self, path: Path, local_path: Path, **kwargs: Any):
pass
给予:
"get" is marked as overload, but additional overloads are missing
PylancereportGeneralTypeIssues
和
Overloaded function implementation is not consistent with signature of overload 1
Type "(self: Derived, arg: str, **kwargs: Any) -> None" cannot be assigned to type "(self: Derived, arg: str, optional_arg: bool = False) -> None"
Function accepts too many positional parameters; expected 2 but received 3
PylancereportGeneralTypeIssues
有没有一种正确的方法来输入注释我想要实现的目标?即基类抽象方法对派生类的可选参数的名称和类型一无所知,但派生类可以公开特定的命名参数及其类型。