It looks like a dictionary of SomeKeyType
cannot be assigned as a dictionary of objects
, even though all types are subtypes of object
:
x: dict[str, str] = {"a": "b"}
y: dict[object, str] = x
Incompatible types in assignment (expression has type "Dict[str, str]", variable has type "Dict[object, str]")
Copying doesn't help either:
x: dict[str, str] = {"a": "b"}
y: dict[object, str] = dict(x)
Argument 1 to "dict" has incompatible type "Dict[str, str]"; expected "SupportsKeysAndGetItem[object, str]"
A similar error occurs when updating a dictionary of objects
with a dictionary of another type:
x: dict[object, str] = {"a": "a"}
y: dict[str, str] = {"b": "b"}
x.update(y)
x = {**x, **y}
x |= y
Argument 1 to "update" of "dict" has incompatible type "Dict[str, str]"; expected "Mapping[object, str]"
Argument 1 to "update" of "dict" has incompatible type "Dict[str, str]"; expected "Mapping[object, str]"
Argument 1 to "__ior__" of "dict" has incompatible type "Dict[str, str]"; expected "Mapping[object, str]"
Note that a naive implementation of dict.update
does not generate any typing issue:
for k, v in y.items():
x[k] = v
Is that on purpose or is it an issue I should report on the mypy tracker?