Custom return type for class function based on how it was initialized

52 Views Asked by At

Is it possible or makes sense to @overload the return type Hashable of a CLASS.function() -> Hashable: based on how the relevant self.obj was initialized?

from typing import Hashable, overload

class CLASS:

    obj = None
    obj_type = Hashable

    def __init__(self, INPUT: Hashable) -> None:
        self.obj = INPUT
        # Specifying the type
        self.obj_type = type(self.obj)
        # This does not work?
        if self.obj_type == int:
            @overload
            def returner() -> int: ...

    def returner() -> Hashable:
        return self.obj

If I pass a value of a certain type to the constructor, then the type of the value returned is unambiguous.

0

There are 0 best solutions below