Update signature of dict.get (#852)

Without this change, mypy can't infer proper types for cases like
`d.get(k, [])` where it needs type context to infer the type of
`[]`. We add the value type to the second argument to `get` using
union types, and this provides the context. This doesn't affect
the effective signature of `get`, other than providing the type
context for mypy.

Also removed some related redundant method definitions where we can
just inherit the base class definition. This makes it easier to
keep the method signatures consistent.

Note that this requires a few mypy PRs before mypy will be able to
use this effectively:

* https://github.com/python/mypy/pull/2718
* https://github.com/python/mypy/pull/2715
This commit is contained in:
Jukka Lehtosalo
2017-01-20 15:41:54 +00:00
committed by Guido van Rossum
parent c577c84a17
commit f2579e532f
5 changed files with 3 additions and 16 deletions

View File

@@ -23,7 +23,7 @@ class DictMixin(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]):
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def get(self, k: _KT, default: Union[_VT, _T]) -> Union[_VT, _T]: ...
def values(self) -> List[_VT]: ...
def items(self) -> List[Tuple[_KT, _VT]]: ...
def iterkeys(self) -> Iterator[_KT]: ...

View File

@@ -538,10 +538,6 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def has_key(self, k: _KT) -> bool: ...
def clear(self) -> None: ...
def copy(self) -> Dict[_KT, _VT]: ...
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
@@ -549,9 +545,6 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def update(self, m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
@overload
def update(self, m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
def keys(self) -> List[_KT]: ...
def values(self) -> List[_VT]: ...
def items(self) -> List[Tuple[_KT, _VT]]: ...
def iterkeys(self) -> Iterator[_KT]: ...
def itervalues(self) -> Iterator[_VT]: ...
def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ...
@@ -568,7 +561,6 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __getitem__(self, k: _KT) -> _VT: ...
def __setitem__(self, k: _KT, v: _VT) -> None: ...
def __delitem__(self, v: _KT) -> None: ...
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_KT]: ...
def __str__(self) -> str: ...

View File

@@ -184,7 +184,7 @@ class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
@overload # type: ignore
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload # type: ignore
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
def get(self, k: _KT, default: Union[_VT_co, _T]) -> Union[_VT_co, _T]: ...
def keys(self) -> list[_KT]: ...
def values(self) -> list[_VT_co]: ...
def items(self) -> list[Tuple[_KT, _VT_co]]: ...

View File

@@ -556,10 +556,6 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self, iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
def clear(self) -> None: ...
def copy(self) -> Dict[_KT, _VT]: ...
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def pop(self, k: _KT, default: _VT = None) -> _VT: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = None) -> _VT: ...
@@ -580,7 +576,6 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __getitem__(self, k: _KT) -> _VT: ...
def __setitem__(self, k: _KT, v: _VT) -> None: ...
def __delitem__(self, v: _KT) -> None: ...
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_KT]: ...
def __str__(self) -> str: ...

View File

@@ -271,7 +271,7 @@ class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
@overload # type: ignore
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload # type: ignore
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
def get(self, k: _KT, default: Union[_VT_co, _T]) -> Union[_VT_co, _T]: ...
def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ...
def keys(self) -> AbstractSet[_KT]: ...
def values(self) -> ValuesView[_VT_co]: ...