y.py:7: error: List comprehension has incompatible type List[Union[int, float, complex]]; expected List[S] [misc]
y.py:7: error: Unsupported operand types for * (likely involving Union) [operator]
Чтобы избежать проблем с mypy, я думаю, мне нужно создать ограниченную TypeVar из Union (которую я импортирую из другого пакета и, следовательно, в принципе вне моего контроля). Есть ли способ добиться этого? Есть ли другой способ (без #type: ignore), чтобы избежать проблемы с mypy?
Запуск mypy со следующим кодом не вызывает проблем. [code]from typing import TypeVar
S = TypeVar("S", int, float, complex)
def func(x: list[S], m: S) -> list[S]: return [val * m for val in x]
out1: list[int] = func([1, 2, 3], 4) out2: list[complex] = func([1., 2., 3.], 4.) [/code] Но следующий код [code]from typing import TypeVar
Number = int | float | complex S = TypeVar("S", bound=Number)
def func(x: list[S], m: S) -> list[S]: return [val * m for val in x]
out1: list[int] = func([1, 2, 3], 4) out2: list[complex] = func([1., 2., 3.], 4.) [/code] отчеты [code]y.py:7: error: List comprehension has incompatible type List[Union[int, float, complex]]; expected List[S] [misc] y.py:7: error: Unsupported operand types for * (likely involving Union) [operator] [/code] Чтобы избежать проблем с mypy, я думаю, мне нужно создать ограниченную TypeVar из Union (которую я импортирую из другого пакета и, следовательно, в принципе вне моего контроля). Есть ли способ добиться этого? Есть ли другой способ (без #type: ignore), чтобы избежать проблемы с mypy?