Предположим, функция, которая принимает в качестве входных данных Iterable[int] или Iterable[str], пытается создать все элементы и сохранить их в памяти как список[int] или список[str]:
Код: Выделить всё
from typing import Union, TypeAlias
from collections.abc import Iterable
Items: TypeAlias = Union[Iterable[int], Iterable[str]]
def process_items(items: Items) -> None:
item_sequence: Union[list[int], list[str]] = list(items)
Код: Выделить всё
Type "list[int | str]" is not assignable to declared type "list[int] | list[str]"
Type "list[int | str]" is not assignable to type "list[int] | list[str]"
"list[int | str]" is not assignable to "list[int]"
Type parameter "_T@list" is invariant, but "int | str" is not the same as "int"
Подробнее здесь: https://stackoverflow.com/questions/798 ... th-pyright
Мобильная версия