For my code I have an aggregate class that needs a validation method defined for each of the subclasses of base class BaseC
, in this case InheritC
inherits from BaseC
.
The validation method is then passed into the aggregate class through a register method.
See the following simple example
from typing import Callable
class BaseC:
def __init__(self) -> None:
pass
class InheritC(BaseC):
def __init__(self) -> None:
super().__init__()
@classmethod
def validate(cls, c:'InheritC') ->bool:
return False
class AggrC:
def register_validate_fn(self, fn: Callable[[BaseC], bool])-> None:
self.validate_fn = fn
ac = AggrC()
ic = InheritC()
ac.validate_fn(ic.fn)
I added type hints on the parameter for registering a function, which is a Callable object Callable[[BaseC], bool]
since potentially there will be several other validation methods which is defined for each class inherited from BaseC
.
However, pylance doesn't seem to recognize this polymorphism in a Callable
type hint and throws a warning (I set up my VScode to type check it) that said
Argument of type "(c: InheritC) -> bool" cannot be assigned to parameter "fn" of type "(BaseC) -> bool" in function "register_fn"
Type "(c: InheritC) -> bool" cannot be assigned to type "(BaseC) -> bool"
Parameter 1: type "BaseC" cannot be assigned to type "InheritC"
"BaseC" is incompatible with "InheritC" Pylance(reportGeneralTypeIssues)
I don't see where I made an mistake in design, and I don't want to simply ignore the warning.
Can any one explain why this is invaid? Or is it just simply a bug from pylance
I'm using python version 3.8.13
for development.