[英]Seems like a bug in Pylance type checking
我得到了一個奇怪的錯誤, list[dict[str, str | int]]
list[dict[str, str | int]]
不能分配給Sequence[dict[str, str | float | int] | None]
Sequence[dict[str, str | float | int] | None]
Sequence[dict[str, str | float | int] | None]
。
Argument of type "list[dict[str, str | int]]" cannot be assigned to parameter "params" of type "Sequence[dict[str, str | float | int] | None]" in function "batch_call"
"list[dict[str, str | int]]" is incompatible with "Sequence[dict[str, str | float | int] | None]"
TypeVar "_T_co@Sequence" is covariant
Type "dict[str, str | int]" cannot be assigned to type "dict[str, str | float | int] | None"
"dict[str, str | int]" is incompatible with "dict[str, str | float | int]"
TypeVar "_VT@dict" is invariant
Type cannot be assigned to type "None"
這是沒有意義的。 該列表顯然是序列的子集。
這實際上是預期的行為,因為dict
是可變的。 Mypy 在其文檔中提供了對該問題的簡要說明。
看看下面的例子:
from collections.abc import Sequence
foo: list[dict[str, str | int]] = [{"foo": 2}]
def bar(s: Sequence[dict[str, str | float | int] | None]):
d = s[0]
if d:
d["foo"] = 0.5
bar(foo)
表達式bar(foo)
將通過插入一個float
來修改foo
,這違反了foo
的指定類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.