At runtime, the following code is perfectly valid:
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
What's nice about this is that during static analysis, if you happen to know you have a variable of type Derived
you get the extra bonus of discovering about the optional named argument optional_arg
and it's type.
I thought that Derived
is overriding get
here in a compatible manner, however, PyRight says that the override is incompatible:
Method "get" overrides class "Base" in an incompatible manner
Parameter 3 type mismatch: base parameter is type "Any", override parameter is type "bool"
PylancereportIncompatibleMethodOverride
An attempt using the overload
decorator fails for me as well so I believe I'm not using it correctly either:
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
Giving:
"get" is marked as overload, but additional overloads are missing
PylancereportGeneralTypeIssues
and
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
Is there a proper way to type annotate what I'm trying to achieve? i.e. base class abstract method known nothing about the names and types of optional arguments of derived classes, but derived classes can expose specific named arguments and their types.