Open
Description
I am trying to type code like this:
class A:
def __init__(self, return_tuple=False):
self.return_tuple = return_tuple
def __call__(self):
if self.return_tuple:
return (1, 2)
return 1
I have tried to make A
generic and use overloads on the self parameter in several ways but none seems to work:
class A(Generic[T]):
def __init__(self, return_tuple: T = False) -> None:
self.return_tuple = return_tuple
@overload
def __call__(self: A[Literal[True]]) -> Tuple[int, int]:
pass
@overload
def __call__(self: A[Literal[False]]) -> int
pass
def __call__(self) -> Union[int, Tuple[int, int]]:
if self.return_tuple:
return (1, 2)
return 1
where T
is either
T = TypeVar("T", bool)
or
T = TypeVar("T", Literal[True], Literal[False])
The first case mark errors in the definition of the TypeVar
, the Generic
and the optional parameter in __init__
. The second one marks an error only in the optional parameter (even if it is essentially the same type). reveal_type
does not have the expected result in both cases.
Am I doing something wrong or is this case just not currently taken into consideration?
Metadata
Metadata
Assignees
Labels
No labels