Generic Type Assumption from enum key #1831
Answered
by
erictraut
InvincibleRMC
asked this question in
Q&A
-
Is there any way to type annotate so from typing import overload, TypeVar, Literal
T = TypeVar('T')
from enum import Enum
class TypeEnum(Enum):
INT = 0
BOOL = 1
class Test(Generic[T]):
@overload
def __init__(self, foo: Literal[TypeEnum.INT], value: int | None) -> None:...
@overload
def __init__(self, foo: None, value: T) -> None: ...
def __init__(self, foo, value):
self.value = value
@property
def value(self) -> T:
return self._value
@value.setter
def value(self, value: T) -> None:
self._value = value
a = Test(TypeEnum.INT, None)
reveal_type(a)
b = Test(None, 3)
reveal_type(b) |
Beta Was this translation helpful? Give feedback.
Answered by
erictraut
Aug 3, 2024
Replies: 1 comment 1 reply
-
Yes, you can add a type annotation for the For details, refer to this section of the typing spec. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
InvincibleRMC
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes, you can add a type annotation for the
self
parameter in the first overload. Pyright playground link.For details, refer to this section of the typing spec.