From 368c703078ff390bb51c0dd60c6306b77a75fa65 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 21 Jul 2016 11:28:35 -0700 Subject: [PATCH] Add dict views to python 2 (#376) --- stdlib/2.7/__builtin__.pyi | 5 ++++- stdlib/2.7/typing.pyi | 16 ++++++++++++++++ stdlib/3/typing.pyi | 10 +++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/stdlib/2.7/__builtin__.pyi b/stdlib/2.7/__builtin__.pyi index 3f2c6781c..572ccc128 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, ItemsView, KeysView, ValuesView ) from abc import abstractmethod, ABCMeta @@ -546,6 +546,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): def iterkeys(self) -> Iterator[_KT]: ... def itervalues(self) -> Iterator[_VT]: ... def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ... + def viewkeys(self) -> KeysView[_KT]: ... + def viewvalues(self) -> ValuesView[_VT]: ... + def viewitems(self) -> ItemsView[_KT, _VT]: ... @staticmethod @overload def fromkeys(seq: Sequence[_T]) -> Dict[_T, Any]: ... # TODO: Actually a class method diff --git a/stdlib/2.7/typing.pyi b/stdlib/2.7/typing.pyi index bee6314e1..b7a43feea 100644 --- a/stdlib/2.7/typing.pyi +++ b/stdlib/2.7/typing.pyi @@ -43,6 +43,7 @@ _KT = TypeVar('_KT') # Key type. _VT = TypeVar('_VT') # Value type. _T_co = TypeVar('_T_co', covariant=True) # Any type covariant containers. _V_co = TypeVar('_V_co', covariant=True) # Any type covariant containers. +_KT_co = TypeVar('_KT_co', covariant=True) # Key type covariant containers. _VT_co = TypeVar('_VT_co', covariant=True) # Value type covariant containers. _T_contra = TypeVar('_T_contra', contravariant=True) # Ditto contravariant. @@ -157,6 +158,21 @@ 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 MappingView(Sized): + def __len__(self) -> int: ... + +class ItemsView(AbstractSet[Tuple[_KT_co, _VT_co]], MappingView, Generic[_KT_co, _VT_co]): + def __contains__(self, o: object) -> bool: ... + def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... + +class KeysView(AbstractSet[_KT_co], MappingView, Generic[_KT_co]): + def __contains__(self, o: object) -> bool: ... + def __iter__(self) -> Iterator[_KT_co]: ... + +class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]): + def __contains__(self, o: object) -> bool: ... + def __iter__(self) -> Iterator[_VT_co]: ... + class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT]): @abstractmethod def __getitem__(self, k: _KT) -> _VT: ... diff --git a/stdlib/3/typing.pyi b/stdlib/3/typing.pyi index 449242458..b96baa2d6 100644 --- a/stdlib/3/typing.pyi +++ b/stdlib/3/typing.pyi @@ -169,7 +169,7 @@ class MutableSequence(Sequence[_T], Generic[_T]): def remove(self, object: _T) -> None: ... def __iadd__(self, x: Iterable[_T]) -> MutableSequence[_T]: ... -class AbstractSet(Iterable[_KT_co], Container[_KT_co], Sized, Generic[_KT_co]): +class AbstractSet(Iterable[_T_co], Container[_T_co], Sized, Generic[_T_co]): @abstractmethod def __contains__(self, x: object) -> bool: ... # Mixin methods @@ -177,10 +177,10 @@ class AbstractSet(Iterable[_KT_co], Container[_KT_co], Sized, Generic[_KT_co]): def __lt__(self, s: AbstractSet[Any]) -> bool: ... def __gt__(self, s: AbstractSet[Any]) -> bool: ... def __ge__(self, s: AbstractSet[Any]) -> bool: ... - def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_KT_co]: ... - def __or__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_KT_co, _T]]: ... - def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_KT_co]: ... - def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_KT_co, _T]]: ... + def __and__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ... + def __or__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ... + def __sub__(self, s: AbstractSet[Any]) -> AbstractSet[_T_co]: ... + def __xor__(self, s: AbstractSet[_T]) -> AbstractSet[Union[_T_co, _T]]: ... # TODO: Argument can be a more general ABC? def isdisjoint(self, s: AbstractSet[Any]) -> bool: ...