The subclass in Callable #1133
-
EventTest is the subclass of EventBase, how to fixed it? |
Beta Was this translation helpful? Give feedback.
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 |
Beta Was this translation helpful? Give feedback.
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 replaceEventBase
in the type annotations.