Skip to content

The subclass in Callable #1133

Answered by carljm
HouLingLXH asked this question in Q&A
Apr 14, 2022 · 1 comments · 7 replies
Discussion options

You must be logged in to vote

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 replace EventBase in the type annotations.

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@TeamSpen210
Comment options

@HouLingLXH
Comment options

@Akuli
Comment options

@HouLingLXH
Comment options

@carljm
Comment options

carljm Apr 14, 2022
Collaborator

Answer selected by HouLingLXH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants