Update collections and typing.NamedTuple (#1276)

* Update stubs for `collections` module in both Pythons.
* Update `typing.NamedTuple` stub to have `_source` attribute.
* Fix compatibility of `deque.index` signature with supertype `Sequence`
This commit is contained in:
Semyon Proshev
2017-05-28 01:01:40 +03:00
committed by Jelle Zijlstra
parent 6f0c929658
commit f9872cafd8
4 changed files with 57 additions and 52 deletions

View File

@@ -3,7 +3,8 @@
# Based on http://docs.python.org/2.7/library/collections.html
# These are not exported.
from typing import Any, Dict, Generic, TypeVar, Tuple, overload, Type, Optional, List, Union, Reversible
import typing
from typing import Dict, Generic, TypeVar, Tuple, overload, Type, Optional, List, Union, Reversible
# These are exported.
from typing import (
@@ -30,7 +31,7 @@ _KT = TypeVar('_KT')
_VT = TypeVar('_VT')
# namedtuple is special-cased in the type checker; the initializer is ignored.
def namedtuple(typename: Union[str, unicode], field_names: Union[str, unicode, Iterable[Any]], *,
def namedtuple(typename: Union[str, unicode], field_names: Union[str, unicode, Iterable[Union[str, unicode]]], *,
verbose: bool = ..., rename: bool = ...) -> Type[tuple]: ...
class deque(Sized, Iterable[_T], Reversible[_T], Generic[_T]):
@@ -59,17 +60,16 @@ class deque(Sized, Iterable[_T], Reversible[_T], Generic[_T]):
def __reversed__(self) -> Iterator[_T]: ...
class Counter(Dict[_T, int], Generic[_T]):
# TODO: __init__ keyword arguments
@overload
def __init__(self) -> None: ...
def __init__(self, **kwargs: int) -> None: ...
@overload
def __init__(self, Mapping: Mapping[_T, int]) -> None: ...
def __init__(self, mapping: Mapping[_T, int]) -> None: ...
@overload
def __init__(self, iterable: Iterable[_T]) -> None: ...
def elements(self) -> Iterator[_T]: ...
def most_common(self, n: int = ...) -> List[_T]: ...
@overload
def subtract(self, mapping: Mapping[_T, int]) -> None: ...
def subtract(self, __mapping: Mapping[_T, int]) -> None: ...
@overload
def subtract(self, iterable: Iterable[_T]) -> None: ...
# The Iterable[Tuple[...]] argument type is not actually desirable
@@ -78,30 +78,33 @@ class Counter(Dict[_T, int], Generic[_T]):
# Dict.update. Not sure if we should use '# type: ignore' instead
# and omit the type from the union.
@overload
def update(self, m: Mapping[_T, int], **kwargs: _VT) -> None: ...
def update(self, m: Mapping[_T, int], **kwargs: int) -> None: ...
@overload
def update(self, m: Union[Iterable[_T], Iterable[Tuple[_T, int]]], **kwargs: _VT) -> None: ...
def update(self, m: Union[Iterable[_T], Iterable[Tuple[_T, int]]], **kwargs: int) -> None: ...
def __add__(self, other: Counter[_T]) -> Counter[_T]: ...
def __sub__(self, other: Counter[_T]) -> Counter[_T]: ...
def __and__(self, other: Counter[_T]) -> Counter[_T]: ...
def __or__(self, other: Counter[_T]) -> Counter[_T]: ...
def __iadd__(self, other: Counter[_T]) -> Counter[_T]: ...
def __isub__(self, other: Counter[_T]) -> Counter[_T]: ...
def __iand__(self, other: Counter[_T]) -> Counter[_T]: ...
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ...
class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ...
def move_to_end(self, key: _KT, last: bool = ...) -> None: ...
def __reversed__(self) -> Iterator[_KT]: ...
class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
default_factory = ... # type: Callable[[], _VT]
# TODO: __init__ keyword args
@overload
def __init__(self) -> None: ...
def __init__(self, **kwargs: _VT) -> None: ...
@overload
def __init__(self, map: Mapping[_KT, _VT]) -> None: ...
def __init__(self, default_factory: Optional[Callable[[], _VT]]) -> None: ...
@overload
def __init__(self, iterable: Iterable[Tuple[_KT, _VT]]) -> None: ...
@overload
def __init__(self, default_factory: Callable[[], _VT]) -> None: ...
@overload
def __init__(self, default_factory: Callable[[], _VT],
def __init__(self, default_factory: Optional[Callable[[], _VT]],
map: Mapping[_KT, _VT]) -> None: ...
@overload
def __init__(self, default_factory: Callable[[], _VT],
def __init__(self, default_factory: Optional[Callable[[], _VT]],
iterable: Iterable[Tuple[_KT, _VT]]) -> None: ...
def __missing__(self, key: _KT) -> _VT: ...

View File

@@ -52,8 +52,12 @@ _VT = TypeVar('_VT')
# namedtuple is special-cased in the type checker; the initializer is ignored.
def namedtuple(typename: str, field_names: Union[str, Iterable[Any]], *,
verbose: bool = ..., rename: bool = ..., module: str = None) -> Type[tuple]: ...
if sys.version_info >= (3, 6):
def namedtuple(typename: str, field_names: Union[str, Iterable[str]], *,
verbose: bool = ..., rename: bool = ..., module: Optional[str] = ...) -> Type[tuple]: ...
else:
def namedtuple(typename: str, field_names: Union[str, Iterable[str]],
verbose: bool = ..., rename: bool = ...) -> Type[tuple]: ...
class UserDict(MutableMapping): ...
class UserList(MutableSequence): ...
@@ -67,11 +71,15 @@ class deque(MutableSequence[_T], Generic[_T]):
maxlen: int = ...) -> None: ...
def append(self, x: _T) -> None: ...
def appendleft(self, x: _T) -> None: ...
def insert(self, i: int, x: _T) -> None: ...
def clear(self) -> None: ...
if sys.version_info >= (3, 5):
def copy(self) -> deque[_T]: ...
def count(self, x: _T) -> int: ...
def extend(self, iterable: Iterable[_T]) -> None: ...
def extendleft(self, iterable: Iterable[_T]) -> None: ...
if sys.version_info >= (3, 5):
def insert(self, i: int, x: _T) -> None: ...
def index(self, x: _T, start: int = ..., stop: int = ...) -> int: ...
def pop(self, i: int = ...) -> _T: ...
def popleft(self) -> _T: ...
def remove(self, value: _T) -> None: ...
@@ -99,28 +107,28 @@ class deque(MutableSequence[_T], Generic[_T]):
def __delitem__(self, s: slice) -> None: raise TypeError
def __contains__(self, o: object) -> bool: ...
def __reversed__(self) -> Iterator[_T]: ...
# TODO __reversed__
if sys.version_info >= (3, 5):
def __add__(self, other: deque[_T]) -> deque[_T]: ...
def __mul__(self, other: int) -> deque[_T]: ...
def __imul__(self, other: int) -> None: ...
class Counter(Dict[_T, int], Generic[_T]):
@overload
def __init__(self) -> None: ...
def __init__(self, **kwargs: int) -> None: ...
@overload
def __init__(self, Mapping: Mapping[_T, int]) -> None: ...
def __init__(self, mapping: Mapping[_T, int]) -> None: ...
@overload
def __init__(self, iterable: Iterable[_T]) -> None: ...
# TODO keyword arguments
def elements(self) -> Iterator[_T]: ...
@overload
def most_common(self) -> List[_T]: ...
@overload
def most_common(self, n: int) -> List[_T]: ...
def most_common(self, n: int = ...) -> List[_T]: ...
@overload
def subtract(self, Mapping: Mapping[_T, int]) -> None: ...
def subtract(self, __mapping: Mapping[_T, int]) -> None: ...
@overload
def subtract(self, iterable: Iterable[_T]) -> None: ...
@@ -134,16 +142,16 @@ class Counter(Dict[_T, int], Generic[_T]):
@overload
def update(self, m: Union[Iterable[_T], Iterable[Tuple[_T, int]]], **kwargs: int) -> None: ...
def __add__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __sub__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __and__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __or__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __pos__(self) -> typing.Counter[_T]: ...
def __neg__(self) -> typing.Counter[_T]: ...
def __iadd__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __isub__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __iand__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __ior__(self, other: typing.Counter[_T]) -> typing.Counter[_T]: ...
def __add__(self, other: Counter[_T]) -> Counter[_T]: ...
def __sub__(self, other: Counter[_T]) -> Counter[_T]: ...
def __and__(self, other: Counter[_T]) -> Counter[_T]: ...
def __or__(self, other: Counter[_T]) -> Counter[_T]: ...
def __pos__(self) -> Counter[_T]: ...
def __neg__(self) -> Counter[_T]: ...
def __iadd__(self, other: Counter[_T]) -> Counter[_T]: ...
def __isub__(self, other: Counter[_T]) -> Counter[_T]: ...
def __iand__(self, other: Counter[_T]) -> Counter[_T]: ...
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ...
class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):
def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ...
@@ -154,29 +162,21 @@ class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]):
default_factory = ... # type: Callable[[], _VT]
@overload
def __init__(self) -> None: ...
def __init__(self, **kwargs: _VT) -> None: ...
@overload
def __init__(self, map: Mapping[_KT, _VT]) -> None: ...
def __init__(self, default_factory: Optional[Callable[[], _VT]]) -> None: ...
@overload
def __init__(self, iterable: Iterable[Tuple[_KT, _VT]]) -> None: ...
@overload
def __init__(self, default_factory: Callable[[], _VT]) -> None: ...
@overload
def __init__(self, default_factory: Callable[[], _VT],
def __init__(self, default_factory: Optional[Callable[[], _VT]],
map: Mapping[_KT, _VT]) -> None: ...
@overload
def __init__(self, default_factory: Callable[[], _VT],
def __init__(self, default_factory: Optional[Callable[[], _VT]],
iterable: Iterable[Tuple[_KT, _VT]]) -> None: ...
# TODO __init__ keyword args
def __missing__(self, key: _KT) -> _VT: ...
# TODO __reversed__
if sys.version_info >= (3, 3):
class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
@overload
def __init__(self) -> None: ...
@overload
def __init__(self, *maps: Mapping[_KT, _VT]) -> None: ...
@property

View File

@@ -10,6 +10,7 @@ if sys.version_info >= (3, 3):
Iterable as Iterable,
Iterator as Iterator,
Sized as Sized,
Callable as Callable,
Mapping as Mapping,
MutableMapping as MutableMapping,
Sequence as Sequence,

View File

@@ -495,6 +495,7 @@ def cast(tp: Type[_T], obj: Any) -> _T: ...
# NamedTuple is special-cased in the type checker
class NamedTuple(tuple):
_fields = ... # type: Tuple[str, ...]
_source = ... # type: str
def __init__(self, typename: str, fields: Iterable[Tuple[str, Any]] = ..., *,
verbose: bool = ..., rename: bool = ..., **kwargs: Any) -> None: ...