Код: Выделить всё
from typing import Literal
PossibleValues = Literal["a", "b"]
x = input()
Код: Выделить всё
>>> isinstance(x, PossibleValues)
Traceback (most recent call last):
File "", line 1, in
File "/.../typing.py", line 720, in __instancecheck__
return self.__subclasscheck__(type(obj))
File "/.../typing.py", line 723, in __subclasscheck__
raise TypeError("Subscripted generics cannot be used with"
TypeError: Subscripted generics cannot be used with class and instance checks
Код: Выделить всё
>>> from typing import get_args
>>> x = input()
'c'
>>> x in get_args(PossibleValues)
False
Подробнее здесь: https://stackoverflow.com/questions/726 ... teral-type
Мобильная версия