The subclass in Callable #1133
Answered
by
carljm
HouLingLXH
asked this question in
Q&A
|
EventTest is the subclass of EventBase, how to fixed it? |
Answered by
carljm
Apr 14, 2022
Replies: 1 comment 7 replies
|
You asked for a callable that can take any EventBase as an argument, you provided one that only accepts an EventTest. This is a type error because the thing you provided is "less capable" than the expected type. In type system terms, a callable type is "contravariant" in its accepted arguments: if you accept only a subtype of the arguments of another callable type, that makes you a supertype of it -- the relationship is reversed. In this case, you either need to provide a callback that can accept any EventBase, or you might want to use a typevar (eg |
7 replies
Answer selected by
HouLingLXH
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment




You asked for a callable that can take any EventBase as an argument, you provided one that only accepts an EventTest. This is a type error because the thing you provided is "less capable" than the expected type. In type system terms, a callable type is "contravariant" in its accepted arguments: if you accept only a subtype of the arguments of another callable type, that makes you a supertype of it -- the relationship is reversed.
In this case, you either need to provide a callback that can accept any EventBase, or you might want to use a typevar (eg
EventBaseT = TypeVar("EventBaseT", bound=EventBase)to replaceEventBasein the type annotations.