Skip to content

Commit 25cb6e4

Browse files
gvanrossumJukkaL
authored andcommitted
Make Mapping covariant. (#512)
* Make Mapping covariant. Fixes #510. This requires a `# type: ignore` on `__getitem__` and `get` because mypy complains about covariant parameters. FWIW typing.py needs to be changed too. (It was covariant in the value but invariant in the key -- typeshed was invariant in both.) * Delete outdated comment. * Backpeddle a bit -- Mapping key type should not be covariant.
1 parent 1d5b35b commit 25cb6e4

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

stdlib/2.7/typing.pyi

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,21 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
176176
def __contains__(self, o: object) -> bool: ...
177177
def __iter__(self) -> Iterator[_VT_co]: ...
178178

179-
class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT]):
179+
class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
180+
# TODO: We wish the key type could also be covariant, but that doesn't work,
181+
# see discussion in https://github.com/python/typing/pull/273.
180182
@abstractmethod
181-
def __getitem__(self, k: _KT) -> _VT: ...
183+
def __getitem__(self, k: _KT) -> _VT_co:
184+
...
182185
# Mixin methods
183-
def get(self, k: _KT, default: _VT = ...) -> _VT: ...
186+
def get(self, k: _KT, default: _VT_co = ...) -> _VT_co: # type: ignore
187+
...
184188
def keys(self) -> list[_KT]: ...
185-
def values(self) -> list[_VT]: ...
186-
def items(self) -> list[Tuple[_KT, _VT]]: ...
189+
def values(self) -> list[_VT_co]: ...
190+
def items(self) -> list[Tuple[_KT, _VT_co]]: ...
187191
def iterkeys(self) -> Iterator[_KT]: ...
188-
def itervalues(self) -> Iterator[_VT]: ...
189-
def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ...
192+
def itervalues(self) -> Iterator[_VT_co]: ...
193+
def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ...
190194
def __contains__(self, o: object) -> bool: ...
191195

192196
class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):

stdlib/3/typing.pyi

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,16 +226,18 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
226226

227227
# TODO: ContextManager (only if contextlib.AbstractContextManager exists)
228228

229-
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]):
230-
# TODO: Value type should be covariant, but currently we can't give a good signature for
231-
# get if this is the case.
229+
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
230+
# TODO: We wish the key type could also be covariant, but that doesn't work,
231+
# see discussion in https://github.com/python/typing/pull/273.
232232
@abstractmethod
233-
def __getitem__(self, k: _KT) -> _VT: ...
233+
def __getitem__(self, k: _KT) -> _VT_co:
234+
...
234235
# Mixin methods
235-
def get(self, k: _KT, default: _VT = ...) -> _VT: ...
236-
def items(self) -> AbstractSet[Tuple[_KT, _VT]]: ...
236+
def get(self, k: _KT, default: _VT_co = ...) -> _VT_co: # type: ignore
237+
...
238+
def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ...
237239
def keys(self) -> AbstractSet[_KT]: ...
238-
def values(self) -> ValuesView[_VT]: ...
240+
def values(self) -> ValuesView[_VT_co]: ...
239241
def __contains__(self, o: object) -> bool: ...
240242

241243
class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):

0 commit comments

Comments
 (0)