Код: Выделить всё
ExprType: TypeAlias = int | str | datetime
def check_type(value: ExprType, expected_type: type[ExprType]):
if not isinstance(value, expected_type):
raise TypeError(f"Expected {expected_type}, but got {type(result)}.")
value: ExprType = 0
# ... more processing ...
if m := re.match("(\d+)", text_var):
check_type(value, int)
value += int(m.group(1))
Код: Выделить всё
Operator + is not supported for types ExprType and int.
Код: Выделить всё
my_var: int | None = 5 # type(my_var) = int | None
if my_var: # type(my_var) = int | None
print("I'm not None") # type(my_var) = int
else:
print("I'm None") # type(my_var) = NoneType
Подробнее здесь: https://stackoverflow.com/questions/772 ... tion-has-c