I have a class method that can return the instance of its class or subclass (depending on the
arguments), but not instance of the supertype.
How to add annotations here?
from typing import *
class A:
@classmethod
def meth(cls): # Type annotation here
# Can return instance of A, B or C class
...
class B(A):
@classmethod
@override
def meth(cls): # Type annotation here
# Can return instance of B or C class, but not A
...
class C(B):
@classmethod
@override
def meth(cls): # Type annotation here
# Can return only instance of C
...
I know that I can just add -> 'A'
, -> 'B'
, -> 'C'
annotations to these methods, but this class is intended for subclassing by library user, I need to force user’s subclasses to be annotated to return only subclasses of them.