From f2579e532f89254b22f7f7f4203da5be698057fc Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Fri, 20 Jan 2017 15:41:54 +0000 Subject: [PATCH] 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 --- stdlib/2/UserDict.pyi | 2 +- stdlib/2/__builtin__.pyi | 8 -------- stdlib/2/typing.pyi | 2 +- stdlib/3/builtins.pyi | 5 ----- stdlib/3/typing.pyi | 2 +- 5 files changed, 3 insertions(+), 16 deletions(-) diff --git a/stdlib/2/UserDict.pyi b/stdlib/2/UserDict.pyi index 6b1f528f7..885a62abf 100644 --- a/stdlib/2/UserDict.pyi +++ b/stdlib/2/UserDict.pyi @@ -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]: ... diff --git a/stdlib/2/__builtin__.pyi b/stdlib/2/__builtin__.pyi index 4378fdf39..9ba1faece 100644 --- a/stdlib/2/__builtin__.pyi +++ b/stdlib/2/__builtin__.pyi @@ -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: ... diff --git a/stdlib/2/typing.pyi b/stdlib/2/typing.pyi index f7a14d390..e591333f3 100644 --- a/stdlib/2/typing.pyi +++ b/stdlib/2/typing.pyi @@ -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]]: ... diff --git a/stdlib/3/builtins.pyi b/stdlib/3/builtins.pyi index af9937e2a..8d07c01fd 100644 --- a/stdlib/3/builtins.pyi +++ b/stdlib/3/builtins.pyi @@ -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: ... diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index c4018938f..c48f4a8b1 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -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]: ...