From 3de5d20c2ba0e20b6a7e0b48e400e56a8b6ae972 Mon Sep 17 00:00:00 2001 From: tharvik Date: Wed, 25 May 2016 13:11:28 +0200 Subject: [PATCH 1/2] Mapping.{get,pop} can return default type --- stdlib/2.7/__builtin__.pyi | 12 +++++++++--- stdlib/2.7/typing.pyi | 10 ++++++++-- stdlib/3/builtins.pyi | 12 +++++++++--- stdlib/3/typing.pyi | 10 ++++++++-- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/stdlib/2.7/__builtin__.pyi b/stdlib/2.7/__builtin__.pyi index 507cde9581fd..63bce2e93b69 100644 --- a/stdlib/2.7/__builtin__.pyi +++ b/stdlib/2.7/__builtin__.pyi @@ -8,7 +8,7 @@ from typing import ( Sequence, Mapping, Tuple, List, Any, Dict, Callable, Generic, Set, AbstractSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs, SupportsRound, IO, BinaryIO, Union, AnyStr, MutableSequence, MutableMapping, - MutableSet + MutableSet, Optional, ) from abc import abstractmethod, ABCMeta @@ -534,8 +534,14 @@ 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]: ... - def get(self, k: _KT, default: _VT = None) -> _VT: ... - def pop(self, k: _KT, default: _VT = ...) -> _VT: ... + @overload + def get(self, k: _KT) -> Optional[_VT]: ... + @overload + def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... + @overload + def pop(self, k: _KT) -> _VT: ... + @overload + def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ... def popitem(self) -> Tuple[_KT, _VT]: ... def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ... @overload diff --git a/stdlib/2.7/typing.pyi b/stdlib/2.7/typing.pyi index 9d7446909f95..925127a2ef6a 100644 --- a/stdlib/2.7/typing.pyi +++ b/stdlib/2.7/typing.pyi @@ -161,7 +161,10 @@ class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT]): @abstractmethod def __getitem__(self, k: _KT) -> _VT: ... # Mixin methods - def get(self, k: _KT, default: _VT = ...) -> _VT: ... + @overload + def get(self, k: _KT) -> Optional[_VT]: ... + @overload + def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... def keys(self) -> list[_KT]: ... def values(self) -> list[_VT]: ... def items(self) -> list[Tuple[_KT, _VT]]: ... @@ -177,7 +180,10 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]): def __delitem__(self, v: _KT) -> None: ... def clear(self) -> None: ... - def pop(self, k: _KT, default: _VT = ...) -> _VT: ... + @overload + def pop(self, k: _KT) -> _VT: ... + @overload + def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ... def popitem(self) -> Tuple[_KT, _VT]: ... def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ... @overload diff --git a/stdlib/3/builtins.pyi b/stdlib/3/builtins.pyi index e0c41892b51e..181652999bed 100644 --- a/stdlib/3/builtins.pyi +++ b/stdlib/3/builtins.pyi @@ -4,7 +4,7 @@ from typing import ( TypeVar, Iterator, Iterable, overload, Sequence, MutableSequence, Mapping, MutableMapping, Tuple, List, Any, Dict, Callable, Generic, Set, AbstractSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsBytes, - SupportsAbs, SupportsRound, IO, Union, ItemsView, KeysView, ValuesView, ByteString + SupportsAbs, SupportsRound, IO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional, ) from abc import abstractmethod, ABCMeta @@ -507,8 +507,14 @@ 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]: ... - def get(self, k: _KT, default: _VT = None) -> _VT: ... - def pop(self, k: _KT, default: _VT = None) -> _VT: ... + @overload + def get(self, k: _KT) -> Optional[_VT]: ... + @overload + def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... + @overload + def pop(self, k: _KT) -> _VT: ... + @overload + def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ... def popitem(self) -> Tuple[_KT, _VT]: ... def setdefault(self, k: _KT, default: _VT = None) -> _VT: ... @overload diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 33235c799487..fa1df9bcf8d0 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -221,7 +221,10 @@ class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]): @abstractmethod def __getitem__(self, k: _KT) -> _VT: ... # Mixin methods - def get(self, k: _KT, default: _VT = ...) -> _VT: ... + @overload + def get(self, k: _KT) -> Optional[_VT]: ... + @overload + def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... def items(self) -> AbstractSet[Tuple[_KT, _VT]]: ... def keys(self) -> AbstractSet[_KT]: ... def values(self) -> ValuesView[_VT]: ... @@ -234,7 +237,10 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]): def __delitem__(self, v: _KT) -> None: ... def clear(self) -> None: ... - def pop(self, k: _KT, default: _VT = ...) -> _VT: ... + @overload + def pop(self, k: _KT) -> _VT: ... + @overload + def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ... def popitem(self) -> Tuple[_KT, _VT]: ... def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ... # 'update' used to take a Union, but using overloading is better. From 9b6b2ce4da9c16bf9d2bc164463212cf9fe2edf0 Mon Sep 17 00:00:00 2001 From: tharvik Date: Sat, 28 May 2016 10:59:32 +0200 Subject: [PATCH 2/2] Mapping values are covariant --- stdlib/2.7/typing.pyi | 16 ++++++++-------- stdlib/3/typing.pyi | 12 ++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/stdlib/2.7/typing.pyi b/stdlib/2.7/typing.pyi index 925127a2ef6a..edf759da050d 100644 --- a/stdlib/2.7/typing.pyi +++ b/stdlib/2.7/typing.pyi @@ -157,20 +157,20 @@ class MutableSet(AbstractSet[_T], Generic[_T]): def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ... def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ... -class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT]): +class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]): @abstractmethod - def __getitem__(self, k: _KT) -> _VT: ... + def __getitem__(self, k: _KT) -> _VT_co: ... # Mixin methods @overload - def get(self, k: _KT) -> Optional[_VT]: ... + def get(self, k: _KT) -> Optional[_VT_co]: ... @overload - def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... + def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ... def keys(self) -> list[_KT]: ... - def values(self) -> list[_VT]: ... - def items(self) -> list[Tuple[_KT, _VT]]: ... + def values(self) -> list[_VT_co]: ... + def items(self) -> list[Tuple[_KT, _VT_co]]: ... def iterkeys(self) -> Iterator[_KT]: ... - def itervalues(self) -> Iterator[_VT]: ... - def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ... + def itervalues(self) -> Iterator[_VT_co]: ... + def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ... def __contains__(self, o: object) -> bool: ... class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]): diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index fa1df9bcf8d0..248ef2230396 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -215,19 +215,19 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]): # TODO: ContextManager (only if contextlib.AbstractContextManager exists) -class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]): +class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]): # TODO: Value type should be covariant, but currently we can't give a good signature for # get if this is the case. @abstractmethod - def __getitem__(self, k: _KT) -> _VT: ... + def __getitem__(self, k: _KT) -> _VT_co: ... # Mixin methods @overload - def get(self, k: _KT) -> Optional[_VT]: ... + def get(self, k: _KT) -> Optional[_VT_co]: ... @overload - def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ... - def items(self) -> AbstractSet[Tuple[_KT, _VT]]: ... + def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ... + def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ... def keys(self) -> AbstractSet[_KT]: ... - def values(self) -> ValuesView[_VT]: ... + def values(self) -> ValuesView[_VT_co]: ... def __contains__(self, o: object) -> bool: ... class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):