Код: Выделить всё
from enum import StrEnum
from typing import Literal, overload
class A(StrEnum):
X = "X"
Y = "Y"
class X:
pass
class Y:
pass
@overload
def enum_to_cls(var: Literal[A.X]) -> type[X]: ...
@overload
def enum_to_cls(var: Literal[A.Y]) -> type[Y]: ...
def enum_to_cls(var: A) -> type[X] | type[Y]:
match var:
case A.X:
return X
case A.Y:
return Y
case _:
raise ValueError(f"Unknown enum value: {var}")
Код: Выделить всё
selected_enum = random.choice([x for x in A])
enum_to_cls(selected_enum)
# Argument of type "A" cannot be assigned to parameter "var" of type "Literal[A.Y]" in
# function "enum_to_cls"
# "A" is not assignable to type "Literal[A.Y]" [reportArgumentType]
Подробнее здесь: https://stackoverflow.com/questions/793 ... -with-enum
Мобильная версия