Add dict views to python 2 (#376)

This commit is contained in:
Michael Lee
2016-07-21 11:28:35 -07:00
committed by Guido van Rossum
parent d787dbe984
commit 368c703078
3 changed files with 25 additions and 6 deletions

View File

@@ -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

View File

@@ -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: ...

View File

@@ -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: ...