Код: Выделить всё
import typing
a: dict[int, int] = {}
b: dict[int, int | str] = a
c: typing.Mapping[int, int | str] = a
d: typing.Mapping[int | str, int] = a
Код: Выделить всё
Expression of type "dict[int, int]" is incompatible with declared type "dict[int, int | str]"
"dict[int, int]" is incompatible with "dict[int, int | str]"
Type parameter "_VT@dict" is invariant, but "int" is not the same as "int | str"
Consider switching from "dict" to "Mapping" which is covariant in the value type
Кроме того, d: typing.Mapping[int | str, int] = a также выдает ошибку:
Код: Выделить всё
Expression of type "dict[int, int]" is incompatible with declared type "Mapping[int | str, int]"
"dict[int, int]" is incompatible with "Mapping[int | str, int]"
Type parameter "_KT@Mapping" is invariant, but "int" is not the same as "int | str"
Если функция объявляет параметр типа dict[int, int | str], как передать объект dict[int, int] в качестве параметра?
Подробнее здесь: https://stackoverflow.com/questions/785 ... nt-int-str