Skip to content

my subclass returns a different type for a method that's only on the superclass. how to type? #1015

Answered by zzzeek
zzzeek asked this question in Q&A
Discussion options

You must be logged in to vote

oh wow, I can put those just in TYPE_CHECKING blocks, and be done with it. cant I. OK.

for anyone reading, just like this:

# amazing syntax version

class ConcreteThing(MoreAbstractOperatorThing):

    if typing.TYPE_CHECKING:
        def __add__(
            self: "ConcreteThing", other: Any
        ) -> "ConcreteThing":
            ...

        def concat(self: "ConcreteThing", other: Any) -> "ConcreteThing":
            ...

    def operate(self, op: Operator, other: Any) -> "ConcreteThing":
        return ConcreteThing()

no @overload needed or any breakage of abstraction.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by zzzeek
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant