Typeshed annotates collections.ChainMap as:
class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
maps: list[Mapping[_KT, _VT]]
def __init__(self, *maps: Mapping[_KT, _VT]) -> None: ...
def new_child(self: Self, m: Mapping[_KT, _VT] | None = ...) -> Self: ...
The self.maps attribute holds the chained mappings. It should be annotated as list[MutableMapping[_KT, _VT]].
The *maps parameter of __init__ and the m parameter of new_child need to be MutableMapping[_KT, _VT] as well.
As it is, the implementation in the standard library violates the type hint for self.maps in its __setitem__ method:
def __setitem__(self, key, value):
self.maps[0][key] = value
Typeshed annotates
collections.ChainMapas:The
self.mapsattribute holds the chained mappings. It should be annotated aslist[MutableMapping[_KT, _VT]].The
*mapsparameter of__init__and themparameter ofnew_childneed to beMutableMapping[_KT, _VT]as well.As it is, the implementation in the standard library violates the type hint for
self.mapsin its__setitem__method: