Search code examples
pythonpython-typing

Typehint a method that returns new instance using a superclass classmethod


from typing import Self, Union


class Superclass:
    @classmethod
    def from_dict(cls, dict_: dict[str, str]) -> Self:
        return cls(**dict_)


class Subclass(Superclass):
    def __init__(self, name: Union[str, None] = None):
        self.name = name

    def copy(self) -> Self:
        return Subclass.from_dict({'name': self.name})

I get an error on the bottom line,

Type "Subclass" is not assignable to return type "Self@Subclass"

I've also tried

from typing import Type, TypeVar, Union, Dict

T = TypeVar('T', bound='Superclass')

class Superclass:
    @classmethod
    def from_dict(cls: Type[T], dict_: dict[str, str]) -> T:
        return cls(**dict_)


class Subclass(Superclass):
    def __init__(self, name: Union[str, None] = None):
        self.name = name

    def copy(self: T) -> T:
        return self.from_dict({'name': self.name})

but this one gives me an error

Cannot access attribute "name" for class "Superclass*"   Attribute "name" is unknown


How can I use a superclass' class method to generate an instance of a child class, inside the child class method?


Solution

  • You've got three problems here.

    The first problem is that there's no guarantee the type of self is specifically Subclass. If you call copy on an instance of a subclass of Subclass, then Self refers to that subclass, and returning an instance of Subclass is wrong.

    You need to return an instance of whatever type self is an instance of. The easiest way to do that is to just call from_dict on self or type(self) instead of Subclass:

    return self.from_dict({'name': self.name})
    

    ...which is exactly what you did in the second version of your code. You fixed this problem! But you introduced a second problem for some reason.

    In the second version of your code, you switched to using a type variable T instead of Self. Your T has an upper bound of Superclass rather than Subclass:

    T = TypeVar('T', bound='Superclass')
    

    which is okay when you're using it in Superclass, but in Subclass, it causes the new error. You'd need a second type variable with a Subclass bound if you wanted to go with this approach, but it'd be easier to just use Self.

    The third problem is that self.name could be None. You can't use None as a value of a dict[str, str]. You'll have to do something about that.