This came up in #3569. See also the later comments in python/typing#11 for background. Mapping is not a protocol (as opposed to most other typing ABCs). This mean code like the following will not work:
fromtypingimportIterator, MappingclassMyMap:
def__getitem__(self, key: str) ->int:
return0def__iter__(self) ->Iterator[str]:
returniter([])
def__len__(self) ->int:
return0def__contains__(self, key: object) ->bool:
returnFalsedeffoo(x: Mapping[str, int]) ->None:
passfoo(MyMap()) # error: Argument 1 to "foo" has incompatible type "MyMap"; expected "Mapping[str, int]"
Deriving MyMap from Mapping[str, int] works, though. I suggest we don't allow new submissions to use Mapping as argument type and work towards removing existing uses. Instead they should use a custom protocol, which can also be narrower than Mapping.
This came up in #3569. See also the later comments in python/typing#11 for background.
Mappingis not a protocol (as opposed to most other typing ABCs). This mean code like the following will not work:Deriving
MyMapfromMapping[str, int]works, though. I suggest we don't allow new submissions to useMappingas argument type and work towards removing existing uses. Instead they should use a custom protocol, which can also be narrower thanMapping.