Почему «dict[int, int]» несовместим с «dict[int, int | str]»?Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Почему «dict[int, int]» несовместим с «dict[int, int | str]»?

Сообщение Anonymous »

Код: Выделить всё

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
Pylance сообщает об ошибке для b: dict[int, int | str] = 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
Но 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 "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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»