Skip to content

Commit 55ab59c

Browse files
author
Guido van Rossum
committed
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.)
1 parent c46993a commit 55ab59c

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

stdlib/2.7/typing.pyi

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,19 @@ 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]):
180-
@abstractmethod
181-
def __getitem__(self, k: _KT) -> _VT: ...
179+
class Mapping(Sized, Iterable[_KT_co], Container[_KT_co], Generic[_KT_co, _VT_co]):
180+
@abstractmethod # type: ignore
181+
def __getitem__(self, k: _KT_co) -> _VT_co: # type: ignore
182+
...
182183
# Mixin methods
183-
def get(self, k: _KT, default: _VT = ...) -> _VT: ...
184-
def keys(self) -> list[_KT]: ...
185-
def values(self) -> list[_VT]: ...
186-
def items(self) -> list[Tuple[_KT, _VT]]: ...
187-
def iterkeys(self) -> Iterator[_KT]: ...
188-
def itervalues(self) -> Iterator[_VT]: ...
189-
def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ...
184+
def get(self, k: _KT_co, default: _VT_co = ...) -> _VT_co: # type: ignore
185+
...
186+
def keys(self) -> list[_KT_co]: ...
187+
def values(self) -> list[_VT_co]: ...
188+
def items(self) -> list[Tuple[_KT_co, _VT_co]]: ...
189+
def iterkeys(self) -> Iterator[_KT_co]: ...
190+
def itervalues(self) -> Iterator[_VT_co]: ...
191+
def iteritems(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ...
190192
def __contains__(self, o: object) -> bool: ...
191193

192194
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]):
229+
class Mapping(Iterable[_KT_co], Container[_KT_co], Sized, Generic[_KT_co, _VT_co]):
230230
# TODO: Value type should be covariant, but currently we can't give a good signature for
231231
# get if this is the case.
232-
@abstractmethod
233-
def __getitem__(self, k: _KT) -> _VT: ...
232+
@abstractmethod # type: ignore
233+
def __getitem__(self, k: _KT_co) -> _VT_co: # type: ignore
234+
...
234235
# Mixin methods
235-
def get(self, k: _KT, default: _VT = ...) -> _VT: ...
236-
def items(self) -> AbstractSet[Tuple[_KT, _VT]]: ...
237-
def keys(self) -> AbstractSet[_KT]: ...
238-
def values(self) -> ValuesView[_VT]: ...
236+
def get(self, k: _KT_co, default: _VT_co = ...) -> _VT_co: # type: ignore
237+
...
238+
def items(self) -> AbstractSet[Tuple[_KT_co, _VT_co]]: ...
239+
def keys(self) -> AbstractSet[_KT_co]: ...
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)