python - 在派生类的重写方法中键入可选的命名参数
问题描述
在运行时,以下代码完全有效:
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
有没有一种正确的方法来输入注释我想要实现的目标?即基类抽象方法对派生类的可选参数的名称和类型一无所知,但派生类可以公开特定的命名参数及其类型。
解决方案
推荐阅读
- angular - 在库中扩展 Spartacus cms 组件
- sql - SQL员工查询获取年薪Oracle HRMS
- opengl - OpenGL 在着色器之间共享模型数据
- c# - 媒体元素中没有同时播放多个视频-WPF
- python-3.x - 使用坐标因变量切片 xarray 数据集
- sas - 创建索引 (ID) 和增量 (SAS)
- python - 从在线词典中使用 python 抓取网页(反向上下文)
- azure - Azure Functions 实现和触发器的最佳实践
- python-3.x - Matplotlib:出版物的一致图像大小
- php - 我收到消息无法发送。邮件程序错误:SMTP 错误:无法验证