From 36c17a8376e4804ab647b7a083a93c56e6599619 Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Tue, 6 Feb 2018 14:46:20 -0800 Subject: [PATCH 1/4] OrderedDict copy should return self-type As should __copy__ --- stdlib/2/collections.pyi | 5 ++++- stdlib/3/collections/__init__.pyi | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/stdlib/2/collections.pyi b/stdlib/2/collections.pyi index bd74205377fc..fc5a696c313a 100644 --- a/stdlib/2/collections.pyi +++ b/stdlib/2/collections.pyi @@ -93,10 +93,13 @@ class Counter(Dict[_T, int], Generic[_T]): def __iand__(self, other: Counter[_T]) -> Counter[_T]: ... def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... +_OrderedDictT = TypeVar('_OrderedDictT', bound=OrderedDict) + class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ... def __reversed__(self) -> Iterator[_KT]: ... - def __copy__(self) -> OrderedDict[_KT, _VT]: ... + def __copy__(self: _OrderedDictT) -> _OrderedDictT: ... + def copy(self: _OrderedDictT) -> _OrderedDictT: ... class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory = ... # type: Callable[[], _VT] diff --git a/stdlib/3/collections/__init__.pyi b/stdlib/3/collections/__init__.pyi index d5e8f346a50f..1fe7461fff6e 100644 --- a/stdlib/3/collections/__init__.pyi +++ b/stdlib/3/collections/__init__.pyi @@ -277,11 +277,14 @@ class Counter(Dict[_T, int], Generic[_T]): def __iand__(self, other: Counter[_T]) -> Counter[_T]: ... def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... +_OrderedDictT = TypeVar('_OrderedDictT', bound=OrderedDict) + 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]: ... - def __copy__(self) -> OrderedDict[_KT, _VT]: ... + def __copy__(self: _OrderedDictT) -> _OrderedDictT: ... + def copy(self: _OrderedDictT) -> _OrderedDictT: ... class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory = ... # type: Callable[[], _VT] From 52cb11e1afd9582e16c27b8242fc78acec185f62 Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Tue, 6 Feb 2018 17:45:16 -0800 Subject: [PATCH 2/4] Remove __copy__ and reorder for consistency with other classes --- stdlib/2/collections.pyi | 3 +-- stdlib/3/collections/__init__.pyi | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/2/collections.pyi b/stdlib/2/collections.pyi index fc5a696c313a..de4b59f5271d 100644 --- a/stdlib/2/collections.pyi +++ b/stdlib/2/collections.pyi @@ -97,9 +97,8 @@ _OrderedDictT = TypeVar('_OrderedDictT', bound=OrderedDict) class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def popitem(self, last: bool = ...) -> Tuple[_KT, _VT]: ... - def __reversed__(self) -> Iterator[_KT]: ... - def __copy__(self: _OrderedDictT) -> _OrderedDictT: ... def copy(self: _OrderedDictT) -> _OrderedDictT: ... + def __reversed__(self) -> Iterator[_KT]: ... class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory = ... # type: Callable[[], _VT] diff --git a/stdlib/3/collections/__init__.pyi b/stdlib/3/collections/__init__.pyi index 1fe7461fff6e..156409ac4966 100644 --- a/stdlib/3/collections/__init__.pyi +++ b/stdlib/3/collections/__init__.pyi @@ -282,9 +282,8 @@ _OrderedDictT = TypeVar('_OrderedDictT', bound=OrderedDict) 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]: ... - def __copy__(self: _OrderedDictT) -> _OrderedDictT: ... def copy(self: _OrderedDictT) -> _OrderedDictT: ... + def __reversed__(self) -> Iterator[_KT]: ... class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory = ... # type: Callable[[], _VT] From 0e91cc692c9eab1e860a0ca9daabdb3710d50b5e Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Tue, 6 Feb 2018 17:45:46 -0800 Subject: [PATCH 3/4] Add defaultdict.copy() --- stdlib/2/collections.pyi | 3 +++ stdlib/3/collections/__init__.pyi | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/stdlib/2/collections.pyi b/stdlib/2/collections.pyi index de4b59f5271d..754259f4e19d 100644 --- a/stdlib/2/collections.pyi +++ b/stdlib/2/collections.pyi @@ -100,6 +100,8 @@ class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def copy(self: _OrderedDictT) -> _OrderedDictT: ... def __reversed__(self) -> Iterator[_KT]: ... +_DefaultDictT = TypeVar('_DefaultDictT', bound=defaultdict) + class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory = ... # type: Callable[[], _VT] @overload @@ -113,3 +115,4 @@ class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): def __init__(self, default_factory: Optional[Callable[[], _VT]], iterable: Iterable[Tuple[_KT, _VT]]) -> None: ... def __missing__(self, key: _KT) -> _VT: ... + def copy(self: _DefaultDictT) -> _DefaultDictT: ... diff --git a/stdlib/3/collections/__init__.pyi b/stdlib/3/collections/__init__.pyi index 156409ac4966..e451f510896f 100644 --- a/stdlib/3/collections/__init__.pyi +++ b/stdlib/3/collections/__init__.pyi @@ -285,6 +285,8 @@ class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def copy(self: _OrderedDictT) -> _OrderedDictT: ... def __reversed__(self) -> Iterator[_KT]: ... +_DefaultDictT = TypeVar('_DefaultDictT', bound=defaultdict) + class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory = ... # type: Callable[[], _VT] @@ -298,9 +300,9 @@ class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): @overload def __init__(self, default_factory: Optional[Callable[[], _VT]], iterable: Iterable[Tuple[_KT, _VT]]) -> None: ... - def __missing__(self, key: _KT) -> _VT: ... # TODO __reversed__ + def copy(self: _DefaultDictT) -> _DefaultDictT: ... if sys.version_info >= (3, 3): class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]): From e1c84aeda7fbad45aea266147fc8f21e0082490a Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Wed, 7 Feb 2018 09:26:13 -0800 Subject: [PATCH 4/4] Add Counter.copy() Counter also inherits from dict and so needs its own copy with self-type. --- stdlib/2/collections.pyi | 3 +++ stdlib/3/collections/__init__.pyi | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/stdlib/2/collections.pyi b/stdlib/2/collections.pyi index 754259f4e19d..b4c94dd77feb 100644 --- a/stdlib/2/collections.pyi +++ b/stdlib/2/collections.pyi @@ -59,6 +59,8 @@ class deque(Sized, Iterable[_T], Reversible[_T], Generic[_T]): def __contains__(self, o: _T) -> bool: ... def __reversed__(self) -> Iterator[_T]: ... +_CounterT = TypeVar('_CounterT', bound=Counter) + class Counter(Dict[_T, int], Generic[_T]): @overload def __init__(self, **kwargs: int) -> None: ... @@ -66,6 +68,7 @@ class Counter(Dict[_T, int], Generic[_T]): def __init__(self, mapping: Mapping[_T, int]) -> None: ... @overload def __init__(self, iterable: Iterable[_T]) -> None: ... + def copy(self: _CounterT) -> _CounterT: ... def elements(self) -> Iterator[_T]: ... def most_common(self, n: Optional[int] = ...) -> List[Tuple[_T, int]]: ... @overload diff --git a/stdlib/3/collections/__init__.pyi b/stdlib/3/collections/__init__.pyi index e451f510896f..88c2c95aa0ba 100644 --- a/stdlib/3/collections/__init__.pyi +++ b/stdlib/3/collections/__init__.pyi @@ -236,6 +236,7 @@ class deque(MutableSequence[_T], Generic[_T]): def __mul__(self, other: int) -> deque[_T]: ... def __imul__(self, other: int) -> None: ... +_CounterT = TypeVar('_CounterT', bound=Counter) class Counter(Dict[_T, int], Generic[_T]): @overload @@ -244,7 +245,7 @@ class Counter(Dict[_T, int], Generic[_T]): def __init__(self, mapping: Mapping[_T, int]) -> None: ... @overload def __init__(self, iterable: Iterable[_T]) -> None: ... - + def copy(self: _CounterT) -> _CounterT: ... def elements(self) -> Iterator[_T]: ... def most_common(self, n: Optional[int] = ...) -> List[Tuple[_T, int]]: ...