Angenommen, eine Funktion, die als Eingabe ein Iterable[int] oder ein Iterable[str] akzeptiert, versucht, alle Elemente zu erzeugen und sie als list[int] oder list[str] im Speicher zu speichern:
Code: Select all
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)
Code: Select all
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"
Mobile version