From a67d6401bd3946721c7449fb861b8db6290d0527 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Thu, 29 Nov 2018 14:29:35 +0100 Subject: [PATCH 01/14] Add type annotations to `sortedcontainers` --- sortedcontainers/sorteddict.pyi | 98 ++++++++++++++++++++ sortedcontainers/sortedlist.pyi | 155 ++++++++++++++++++++++++++++++++ sortedcontainers/sortedset.pyi | 82 +++++++++++++++++ 3 files changed, 335 insertions(+) create mode 100644 sortedcontainers/sorteddict.pyi create mode 100644 sortedcontainers/sortedlist.pyi create mode 100644 sortedcontainers/sortedset.pyi diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi new file mode 100644 index 00000000..70ee7866 --- /dev/null +++ b/sortedcontainers/sorteddict.pyi @@ -0,0 +1,98 @@ +from __future__ import annotations + +import typing +from typing import ( + Any, + Callable, + Dict, + Generic, + Iterator, + Iterable, + ItemsView, + KeysView, + List, + Mapping, + Optional, + Sequence, + Type, + Tuple, + Union, + ValuesView, +) + +T = typing.TypeVar("T") +S = typing.TypeVar("S") +KT = typing.TypeVar("KT", bound=typing.Hashable) # Key type. +VT = typing.TypeVar("VT") # Value type. +Key = Callable[[KT], Any] + +class SortedDict(typing.Dict[KT, VT], typing.Generic[KT, VT]): + @typing.overload + def __init__(self, **kwargs: VT) -> None: ... + @typing.overload + def __init__(self, map: Mapping[KT, VT], **kwargs: VT) -> None: ... + @typing.overload + def __init__(self, iterable: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: ... + @typing.overload + def __init__(self, key: Key, **kwargs: VT) -> None: ... + @typing.overload + def __init__(self, key: Key, map: Mapping[KT, VT], **kwargs: VT) -> None: ... + @typing.overload + def __init__(self, key: Key, iterable: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: ... + @property + def key(self) -> Optional[Key]: ... + @property + def iloc(self) -> SortedKeysView[KT]: ... + def clear(self) -> None: ... + def __delitem__(self, key: KT) -> None: ... + def __iter__(self) -> Iterator[KT]: ... + def __reversed__(self) -> Iterator[KT]: ... + def __setitem__(self, key: KT, value: VT) -> None: ... + _setitem = __setitem__ + def copy(self) -> SortedDict[KT, VT]: ... + __copy__ = copy + @classmethod + @typing.overload + def fromkeys(cls, seq: Iterable[T]) -> SortedDict[T, None]: ... + @classmethod + @typing.overload + def fromkeys(cls, seq: Iterable[T], value: S) -> SortedDict[T, S]: ... + def keys(self) -> SortedKeysView[KT]: ... + def items(self) -> SortedItemsView[KT, VT]: ... + def values(self) -> SortedValuesView[VT]: ... + @typing.overload + def pop(self, key: KT) -> VT: ... + @typing.overload + def pop(self, key: KT, default: T = ...) -> Union[VT, T]: ... + def popitem(self, index: int = ...) -> Tuple[KT, VT]: ... + def peekitem(self, index: int = ...) -> Tuple[KT, VT]: ... + def setdefault(self, key: KT, default: Optional[VT] = None) -> VT: ... + def update(self, *args: Union[Mapping[KT, VT], Iterable[Tuple[KT, VT]]], **kwargs: VT): ... + def __reduce__( + self + ) -> Tuple[Type[SortedDict[KT, VT]], Tuple[Callable[[KT], Any], List[Tuple[KT, VT]]]]: ... + def __repr__(self) -> str: ... + def _check(self) -> None: ... + +class SortedKeysView(Generic[KT], KeysView[KT], Sequence[KT]): + @typing.overload + def __getitem__(self, index: int) -> KT: ... + @typing.overload + def __getitem__(self, index: slice) -> List[KT]: ... + def __delitem__(self, index: Union[int, slice]) -> None: ... + +# NB: currently errors, see mypy #5973 +class SortedItemsView(Generic[KT, VT], ItemsView[KT, VT], Sequence[Tuple[KT, VT]]): + def __iter__(self) -> Iterator[Tuple[KT, VT]]: ... + @typing.overload + def __getitem__(self, index: int) -> Tuple[KT, VT]: ... + @typing.overload + def __getitem__(self, index: slice) -> List[Tuple[KT, VT]]: ... + def __delitem__(self, index: Union[int, slice]) -> None: ... + +class SortedValuesView(Generic[VT], ValuesView[VT], Sequence[VT]): + @typing.overload + def __getitem__(self, index: int) -> VT: ... + @typing.overload + def __getitem__(self, index: slice) -> List[VT]: ... + def __delitem__(self, index: Union[int, slice]) -> None: ... diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi new file mode 100644 index 00000000..3f6df53c --- /dev/null +++ b/sortedcontainers/sortedlist.pyi @@ -0,0 +1,155 @@ +from __future__ import annotations + +import typing +from typing import Any, Callable, Iterable, Iterator, List, Optional, Tuple, Type, TypeVar, Union + +T = TypeVar("T") +Key = Callable[[T], Any] +Repr = Callable[[], str] + +def recursive_repr(fillvalue: str = ...) -> Callable[[Repr], Repr]: ... + + +class SortedList(typing.Generic[T], typing.MutableSequence[T]): + + DEFAULT_LOAD_FACTOR: int = ... + def __init__( + self, iterable: Optional[Iterable[T]] = ..., key: Optional[Key] = ... + ): ... + + # NB: currently mypy does not honour return type, see mypy #3307 + @typing.overload + def __new__(cls, iterable: None, key: None) -> SortedList[T]: ... + @typing.overload + def __new__(cls, iterable: None, key: Key) -> SortedKeyList[T]: ... + @typing.overload + def __new__(cls, iterable: Iterable[T], key: None) -> SortedList[T]: ... + @typing.overload + def __new__(cls, iterable: Iterable[T], key: Key) -> SortedKeyList[T]: ... + + @property + def key(self) -> Optional[Callable[[T], Any]]: ... + def _reset(self, load: int) -> None: ... + def clear(self) -> None: ... + _clear = clear + def add(self, value: T) -> None: ... + def _expand(self, pos: int) -> None: ... + def update(self, iterable: Iterable[T]) -> None: ... + _update = update + def discard(self, value: T) -> None: ... + def remove(self, value: T) -> None: ... + def _delete(self, pos: int, idx: int) -> None: ... + def _loc(self, pos: int, idx: int) -> int: ... + def _pos(self, idx: int) -> int: ... + def _build_index(self) -> None: ... + def __contains__(self, value: Any) -> bool: ... + def __delitem__(self, index: Union[int, slice]) -> None: ... + @typing.overload + def __getitem__(self, index: int) -> T: ... + @typing.overload + def __getitem__(self, index: slice) -> List[T]: ... + _getitem = __getitem__ + @typing.overload + def __setitem__(self, index: int, value: T) -> None: ... + @typing.overload + def __setitem__(self, index: slice, value: Iterable[T]) -> None: ... + def __iter__(self) -> Iterator[T]: ... + def __reversed__(self) -> Iterator[T]: ... + def __len__(self) -> int: ... + def reverse(self) -> None: ... + def islice( + self, start: Optional[int] = ..., stop: Optional[int] = ..., reverse=bool + ) -> Iterator[T]: ... + def _islice( + self, min_pos: int, min_idx: int, max_pos: int, max_idx: int, reverse: bool + ) -> Iterator[T]: ... + def irange( + self, + minimum: Optional[int] = ..., + maximum: Optional[int] = ..., + inclusive: Tuple[bool, bool] = ..., + reverse: bool = ..., + ) -> Iterator[T]: ... + def bisect_left(self, value: T) -> int: ... + def bisect_right(self, value: T) -> int: ... + bisect = bisect_right + _bisect_right = bisect_right + def count(self, value: T) -> int: ... + def copy(self) -> SortedList[T]: ... + __copy__ = copy + def append(self, value: T) -> None: ... + def extend(self, values: Iterable[T]) -> None: ... + def insert(self, index: int, value: T) -> None: ... + def pop(self, index: int = -1) -> T: ... + def index(self, value: T, start: Optional[int] = None, stop: Optional[int] = None) -> int: ... + def __add__(self, other: Iterable[T]) -> SortedList[T]: ... + __radd__ = __add__ + def __iadd__(self, other: Iterable[T]) -> SortedList[T]: ... + def __mul__(self, num: int) -> SortedList[T]: ... + __rmul__ = __mul__ + def __imul__(self, num: int) -> SortedList[T]: ... + def __eq__(self, other: Any) -> bool: ... + def __ne__(self, other: Any) -> bool: ... + def __lt__(self, other: typing.Sequence[T]) -> bool: ... + def __gt__(self, other: typing.Sequence[T]) -> bool: ... + def __le__(self, other: typing.Sequence[T]) -> bool: ... + def __ge__(self, other: typing.Sequence[T]) -> bool: ... + def __repr__(self) -> str: ... + def _check(self) -> None: ... + +class SortedKeyList(typing.Generic[T], SortedList[T]): + def __init__( + self, iterable: Optional[Iterable[T]] = ..., key: Callable[[T], Any] = ... + ) -> None: ... + def __new__( + cls, iterable: Optional[Iterable[T]] = ..., key: Callable[[T], Any] = ... + ) -> SortedKeyList[T]: ... + @property + def key(self) -> Callable[[T], Any]: ... + def clear(self) -> None: ... + _clear = clear + def add(self, value: T) -> None: ... + def _expand(self, pos: int) -> None: ... + def update(self, iterable: Iterable[T]) -> None: ... + _update = update + + # NB: Should be T to be safely passed to self.func, but base class imposes Any + def __contains__( + self, value: Any + ) -> bool: ... + def discard(self, value: T) -> None: ... + def remove(self, value: T) -> None: ... + def _delete(self, pos: int, idx: int) -> None: ... + def irange( + self, + minimum: Optional[int] = ..., + maximum: Optional[int] = ..., + inclusive: Tuple[bool, bool] = ..., + reverse: bool = ..., + ): ... + def irange_key( + self, + min_key: Optional[Any] = ..., + max_key: Optional[Any] = ..., + inclusive: Tuple[bool, bool] = ..., + reserve: bool = ..., + ): ... + def bisect_left(self, value: T) -> int: ... + def bisect_right(self, value: T) -> int: ... + bisect = bisect_right + def bisect_key_left(self, key: Any) -> int: ... + _bisect_key_left = bisect_key_left + def bisect_key_right(self, key: Any) -> int: ... + bisect_key = bisect_key_right + _bisect_key_right = bisect_key_right + def count(self, value: T) -> int: ... + def copy(self) -> SortedKeyList[T]: ... + __copy__ = copy + def index(self, value: T, start: Optional[int] = ..., stop: Optional[int] = ...) -> int: ... + def __add__(self, other: Iterable[T]) -> SortedKeyList[T]: ... + __radd__ = __add__ + def __mul__(self, num: int) -> SortedKeyList[T]: ... + def __repr__(self) -> str: ... + def _check(self) -> None: ... + +SortedListWithKey = SortedKeyList diff --git a/sortedcontainers/sortedset.pyi b/sortedcontainers/sortedset.pyi new file mode 100644 index 00000000..ce9911a3 --- /dev/null +++ b/sortedcontainers/sortedset.pyi @@ -0,0 +1,82 @@ +from __future__ import annotations + +import typing +from typing import ( + Any, + AbstractSet, + Callable, + Generic, + Hashable, + Iterable, + Iterator, + List, + MutableSet, + Optional, + Sequence, + Tuple, + Type, + Set, + TypeVar, + Union, +) + +# --- Global + +T = TypeVar("T", bound=Hashable) +S = TypeVar("S", bound=Hashable) +Key = Callable[[T], Any] + +class SortedSet(MutableSet[T], Sequence[T], Generic[T]): + def __init__(self, iterable: Optional[Iterable[T]] = ..., key: Optional[Key] = ...) -> None: ... + @classmethod + def _fromset(cls, values: Set[T], key: Optional[Key] = ...) -> SortedSet[T]: ... + @property + def key(self) -> Optional[Key]: ... + def __contains__(self, value: Any) -> bool: ... + @typing.overload + def __getitem__(self, index: int) -> T: ... + @typing.overload + def __getitem__(self, index: slice) -> List[T]: ... + def __delitem__(self, index: Union[int, slice]) -> None: ... + def __eq__(self, other: Any) -> bool: ... + def __ne__(self, other: Any) -> bool: ... + def __lt__(self, other: Iterable[T]) -> bool: ... + def __gt__(self, other: Iterable[T]) -> bool: ... + def __le__(self, other: Iterable[T]) -> bool: ... + def __ge__(self, other: Iterable[T]) -> bool: ... + def __len__(self) -> int: ... + def __iter__(self) -> Iterator[T]: ... + def __reversed__(self) -> Iterator[T]: ... + def add(self, value: T) -> None: ... + _add = add + def clear(self) -> None: ... + def copy(self) -> SortedSet[T]: ... + __copy__ = copy + def count(self, value: T) -> int: ... + def discard(self, value: T) -> None: ... + _discard = discard + def pop(self, index: int = ...) -> T: ... + def remove(self, value: T) -> None: ... + def difference(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __sub__ = difference + def difference_update(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __isub__ = difference_update + def intersection(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __and__ = intersection + __rand__ = __and__ + def intersection_update(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __iand__ = intersection_update + def symmetric_difference(self, other: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __xor__ = symmetric_difference + __rxor__ = __xor__ + def symmetric_difference_update(self, other: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __ixor__ = symmetric_difference_update + def union(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __or__ = union + __ror__ = __or__ + def update(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + __ior__ = update + _update = update + def __reduce__(self) -> Tuple[Type[SortedSet[T]], Set[T], Callable[[T], Any]]: ... + def __repr__(self) -> str: ... + def _check(self) -> None: ... From 7648c8151b0ef0b24b619f6b6dc8916988e6d422 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Thu, 29 Nov 2018 14:30:05 +0100 Subject: [PATCH 02/14] Make distribution include type hints and flag package as typed --- setup.py | 1 + sortedcontainers/py.typed | 0 2 files changed, 1 insertion(+) create mode 100644 sortedcontainers/py.typed diff --git a/setup.py b/setup.py index e3b55375..0ea5374d 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ def run_tests(self): url='http://www.grantjenks.com/docs/sortedcontainers/', license='Apache 2.0', packages=['sortedcontainers'], + package_data={'sortedcontainers': ['py.typed', '*.pyi']}, tests_require=['tox'], cmdclass={'test': Tox}, install_requires=[], diff --git a/sortedcontainers/py.typed b/sortedcontainers/py.typed new file mode 100644 index 00000000..e69de29b From e6f72b7670b5b216ae3ad2b05a5cb137974982ff Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 5 Dec 2018 01:44:46 +0100 Subject: [PATCH 03/14] Improve annotations based on @bryanforbes suggestions --- sortedcontainers/sorteddict.pyi | 100 ++++++++++---------- sortedcontainers/sortedlist.pyi | 161 +++++++++++++++++--------------- sortedcontainers/sortedset.pyi | 65 +++++++------ 3 files changed, 170 insertions(+), 156 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index 70ee7866..2fa76291 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -1,11 +1,10 @@ -from __future__ import annotations - import typing from typing import ( Any, Callable, Dict, Generic, + Hashable, Iterator, Iterable, ItemsView, @@ -15,84 +14,85 @@ from typing import ( Optional, Sequence, Type, + TypeVar, Tuple, Union, ValuesView, ) -T = typing.TypeVar("T") -S = typing.TypeVar("S") -KT = typing.TypeVar("KT", bound=typing.Hashable) # Key type. -VT = typing.TypeVar("VT") # Value type. -Key = Callable[[KT], Any] +_T = TypeVar("_T") +_S = TypeVar("_S") +_KT = TypeVar("_KT", bound=Hashable) # Key type. +_VT = TypeVar("_VT") # Value type. +_KT_co = TypeVar("_KT_co", covariant=True, bound=Hashable) +_VT_co = TypeVar("_VT_co", covariant=True) +_SD = TypeVar("_SD", bound="SortedDict") +_Key = Callable[[_KT], Any] -class SortedDict(typing.Dict[KT, VT], typing.Generic[KT, VT]): +class SortedDict(Dict[_KT, _VT], Generic[_KT, _VT]): @typing.overload - def __init__(self, **kwargs: VT) -> None: ... + def __init__(self, **kwargs: _VT) -> None: ... @typing.overload - def __init__(self, map: Mapping[KT, VT], **kwargs: VT) -> None: ... + def __init__(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... @typing.overload - def __init__(self, iterable: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: ... + def __init__(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... @typing.overload - def __init__(self, key: Key, **kwargs: VT) -> None: ... + def __init__(self, __key: _Key, **kwargs: _VT) -> None: ... @typing.overload - def __init__(self, key: Key, map: Mapping[KT, VT], **kwargs: VT) -> None: ... + def __init__(self, __key: _Key, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... @typing.overload - def __init__(self, key: Key, iterable: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: ... + def __init__(self, __key: _Key, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... @property - def key(self) -> Optional[Key]: ... + def key(self) -> Optional[_Key]: ... @property - def iloc(self) -> SortedKeysView[KT]: ... + def iloc(self) -> SortedKeysView[_KT]: ... def clear(self) -> None: ... - def __delitem__(self, key: KT) -> None: ... - def __iter__(self) -> Iterator[KT]: ... - def __reversed__(self) -> Iterator[KT]: ... - def __setitem__(self, key: KT, value: VT) -> None: ... - _setitem = __setitem__ - def copy(self) -> SortedDict[KT, VT]: ... - __copy__ = copy + def __delitem__(self, key: _KT) -> None: ... + def __iter__(self) -> Iterator[_KT]: ... + def __reversed__(self) -> Iterator[_KT]: ... + def __setitem__(self, key: _KT, value: _VT) -> None: ... + def _setitem(self, key: _KT, value: _VT) -> None: ... + def copy(self: _SD) -> _SD: ... + def __copy__(self: _SD) -> _SD: ... @classmethod @typing.overload - def fromkeys(cls, seq: Iterable[T]) -> SortedDict[T, None]: ... + def fromkeys(cls, seq: Iterable[_T]) -> SortedDict[_T, None]: ... @classmethod @typing.overload - def fromkeys(cls, seq: Iterable[T], value: S) -> SortedDict[T, S]: ... - def keys(self) -> SortedKeysView[KT]: ... - def items(self) -> SortedItemsView[KT, VT]: ... - def values(self) -> SortedValuesView[VT]: ... - @typing.overload - def pop(self, key: KT) -> VT: ... - @typing.overload - def pop(self, key: KT, default: T = ...) -> Union[VT, T]: ... - def popitem(self, index: int = ...) -> Tuple[KT, VT]: ... - def peekitem(self, index: int = ...) -> Tuple[KT, VT]: ... - def setdefault(self, key: KT, default: Optional[VT] = None) -> VT: ... - def update(self, *args: Union[Mapping[KT, VT], Iterable[Tuple[KT, VT]]], **kwargs: VT): ... - def __reduce__( - self - ) -> Tuple[Type[SortedDict[KT, VT]], Tuple[Callable[[KT], Any], List[Tuple[KT, VT]]]]: ... + def fromkeys(cls, seq: Iterable[_T], value: _S) -> SortedDict[_T, _S]: ... + def keys(self) -> SortedKeysView[_KT]: ... + def items(self) -> SortedItemsView[_KT, _VT]: ... + def values(self) -> SortedValuesView[_VT]: ... + @typing.overload + def pop(self, key: _KT) -> _VT: ... + @typing.overload + def pop(self, key: _KT, default: _T = ...) -> Union[_VT, _T]: ... + def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... + def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... + def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> _VT: ... + def update(self, *args: Union[Mapping[_KT, _VT], Iterable[Tuple[_KT, _VT]]], **kwargs: _VT): ... + def __reduce__(self) -> Tuple[Type[SortedDict[_KT, _VT]], Tuple[Callable[[_KT], Any], List[Tuple[_KT, _VT]]]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... -class SortedKeysView(Generic[KT], KeysView[KT], Sequence[KT]): +class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co], Generic[_KT_co]): @typing.overload - def __getitem__(self, index: int) -> KT: ... + def __getitem__(self, index: int) -> _KT_co: ... @typing.overload - def __getitem__(self, index: slice) -> List[KT]: ... + def __getitem__(self, index: slice) -> List[_KT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -# NB: currently errors, see mypy #5973 -class SortedItemsView(Generic[KT, VT], ItemsView[KT, VT], Sequence[Tuple[KT, VT]]): - def __iter__(self) -> Iterator[Tuple[KT, VT]]: ... +class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]): # type: ignore + def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... @typing.overload - def __getitem__(self, index: int) -> Tuple[KT, VT]: ... + def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ... @typing.overload - def __getitem__(self, index: slice) -> List[Tuple[KT, VT]]: ... + def __getitem__(self, index: slice) -> List[Tuple[_KT_co, _VT_co]]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -class SortedValuesView(Generic[VT], ValuesView[VT], Sequence[VT]): +class SortedValuesView(ValuesView[_VT_co], Sequence[_VT_co], Generic[_VT_co]): @typing.overload - def __getitem__(self, index: int) -> VT: ... + def __getitem__(self, index: int) -> _VT_co: ... @typing.overload - def __getitem__(self, index: slice) -> List[VT]: ... + def __getitem__(self, index: slice) -> List[_VT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi index 3f6df53c..5b54e7a0 100644 --- a/sortedcontainers/sortedlist.pyi +++ b/sortedcontainers/sortedlist.pyi @@ -1,43 +1,54 @@ -from __future__ import annotations - import typing -from typing import Any, Callable, Iterable, Iterator, List, Optional, Tuple, Type, TypeVar, Union +from typing import ( + Any, + Callable, + Generic, + Iterable, + Iterator, + List, + MutableSequence, + Optional, + Tuple, + Type, + TypeVar, + Union, +) -T = TypeVar("T") -Key = Callable[[T], Any] -Repr = Callable[[], str] +_T = TypeVar("_T") +_SL = TypeVar("_SL", bound="SortedList") +_SKL = TypeVar("_SKL", bound="SortedKeyList") +_Key = Callable[[_T], Any] +_Repr = Callable[[], str] -def recursive_repr(fillvalue: str = ...) -> Callable[[Repr], Repr]: ... +def recursive_repr(fillvalue: str = ...) -> Callable[[_Repr], _Repr]: ... -class SortedList(typing.Generic[T], typing.MutableSequence[T]): +class SortedList(MutableSequence[_T], Generic[_T]): DEFAULT_LOAD_FACTOR: int = ... - def __init__( - self, iterable: Optional[Iterable[T]] = ..., key: Optional[Key] = ... - ): ... + def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key] = ...): ... # NB: currently mypy does not honour return type, see mypy #3307 @typing.overload - def __new__(cls, iterable: None, key: None) -> SortedList[T]: ... + def __new__(cls: Type[_SL], iterable: None, key: None) -> _SL: ... @typing.overload - def __new__(cls, iterable: None, key: Key) -> SortedKeyList[T]: ... + def __new__(cls: Type[_SL], iterable: None, key: _Key) -> SortedKeyList[_T]: ... @typing.overload - def __new__(cls, iterable: Iterable[T], key: None) -> SortedList[T]: ... + def __new__(cls: Type[_SL], iterable: Iterable[_T], key: None) -> _SL: ... @typing.overload - def __new__(cls, iterable: Iterable[T], key: Key) -> SortedKeyList[T]: ... + def __new__(cls, iterable: Iterable[_T], key: _Key) -> SortedKeyList[_T]: ... @property - def key(self) -> Optional[Callable[[T], Any]]: ... + def key(self) -> Optional[Callable[[_T], Any]]: ... def _reset(self, load: int) -> None: ... def clear(self) -> None: ... - _clear = clear - def add(self, value: T) -> None: ... + def _clear(self) -> None: ... + def add(self, value: _T) -> None: ... def _expand(self, pos: int) -> None: ... - def update(self, iterable: Iterable[T]) -> None: ... - _update = update - def discard(self, value: T) -> None: ... - def remove(self, value: T) -> None: ... + def update(self, iterable: Iterable[_T]) -> None: ... + def _update(self, iterable: Iterable[_T]) -> None:... + def discard(self, value: _T) -> None: ... + def remove(self, value: _T) -> None: ... def _delete(self, pos: int, idx: int) -> None: ... def _loc(self, pos: int, idx: int) -> int: ... def _pos(self, idx: int) -> int: ... @@ -45,80 +56,81 @@ class SortedList(typing.Generic[T], typing.MutableSequence[T]): def __contains__(self, value: Any) -> bool: ... def __delitem__(self, index: Union[int, slice]) -> None: ... @typing.overload - def __getitem__(self, index: int) -> T: ... + def __getitem__(self, index: int) -> _T: ... + @typing.overload + def __getitem__(self, index: slice) -> List[_T]: ... @typing.overload - def __getitem__(self, index: slice) -> List[T]: ... - _getitem = __getitem__ + def _getitem(self, index: int) -> _T: ... @typing.overload - def __setitem__(self, index: int, value: T) -> None: ... + def _getitem(self, index: slice) -> List[_T]: ... @typing.overload - def __setitem__(self, index: slice, value: Iterable[T]) -> None: ... - def __iter__(self) -> Iterator[T]: ... - def __reversed__(self) -> Iterator[T]: ... + def __setitem__(self, index: int, value: _T) -> None: ... + @typing.overload + def __setitem__(self, index: slice, value: Iterable[_T]) -> None: ... + def __iter__(self) -> Iterator[_T]: ... + def __reversed__(self) -> Iterator[_T]: ... def __len__(self) -> int: ... def reverse(self) -> None: ... def islice( self, start: Optional[int] = ..., stop: Optional[int] = ..., reverse=bool - ) -> Iterator[T]: ... + ) -> Iterator[_T]: ... def _islice( self, min_pos: int, min_idx: int, max_pos: int, max_idx: int, reverse: bool - ) -> Iterator[T]: ... + ) -> Iterator[_T]: ... def irange( self, minimum: Optional[int] = ..., maximum: Optional[int] = ..., inclusive: Tuple[bool, bool] = ..., reverse: bool = ..., - ) -> Iterator[T]: ... - def bisect_left(self, value: T) -> int: ... - def bisect_right(self, value: T) -> int: ... + ) -> Iterator[_T]: ... + def bisect_left(self, value: _T) -> int: ... + def bisect_right(self, value: _T) -> int: ... bisect = bisect_right _bisect_right = bisect_right - def count(self, value: T) -> int: ... - def copy(self) -> SortedList[T]: ... - __copy__ = copy - def append(self, value: T) -> None: ... - def extend(self, values: Iterable[T]) -> None: ... - def insert(self, index: int, value: T) -> None: ... - def pop(self, index: int = -1) -> T: ... - def index(self, value: T, start: Optional[int] = None, stop: Optional[int] = None) -> int: ... - def __add__(self, other: Iterable[T]) -> SortedList[T]: ... - __radd__ = __add__ - def __iadd__(self, other: Iterable[T]) -> SortedList[T]: ... - def __mul__(self, num: int) -> SortedList[T]: ... - __rmul__ = __mul__ - def __imul__(self, num: int) -> SortedList[T]: ... + def count(self, value: _T) -> int: ... + def copy(self: _SL) -> _SL: ... + def __copy__(self: _SL) -> _SL: ... + def append(self, value: _T) -> None: ... + def extend(self, values: Iterable[_T]) -> None: ... + def insert(self, index: int, value: _T) -> None: ... + def pop(self, index: int = -1) -> _T: ... + def index(self, value: _T, start: Optional[int] = None, stop: Optional[int] = None) -> int: ... + def __add__(self: _SL, other: Iterable[_T]) -> _SL: ... + def __radd__(self: _SL, other: Iterable[_T]) -> _SL: ... + def __iadd__(self: _SL, other: Iterable[_T]) -> _SL: ... + def __mul__(self: _SL, num: int) -> _SL: ... + def __rmul__(self: _SL, num: int) -> _SL: ... + def __imul__(self: _SL, num: int) -> _SL: ... def __eq__(self, other: Any) -> bool: ... def __ne__(self, other: Any) -> bool: ... - def __lt__(self, other: typing.Sequence[T]) -> bool: ... - def __gt__(self, other: typing.Sequence[T]) -> bool: ... - def __le__(self, other: typing.Sequence[T]) -> bool: ... - def __ge__(self, other: typing.Sequence[T]) -> bool: ... + def __lt__(self, other: typing.Sequence[_T]) -> bool: ... + def __gt__(self, other: typing.Sequence[_T]) -> bool: ... + def __le__(self, other: typing.Sequence[_T]) -> bool: ... + def __ge__(self, other: typing.Sequence[_T]) -> bool: ... def __repr__(self) -> str: ... def _check(self) -> None: ... -class SortedKeyList(typing.Generic[T], SortedList[T]): +class SortedKeyList(SortedList[_T], Generic[_T]): def __init__( - self, iterable: Optional[Iterable[T]] = ..., key: Callable[[T], Any] = ... + self, iterable: Optional[Iterable[_T]] = ..., key: Callable[[_T], Any] = ... ) -> None: ... def __new__( - cls, iterable: Optional[Iterable[T]] = ..., key: Callable[[T], Any] = ... - ) -> SortedKeyList[T]: ... + cls, iterable: Optional[Iterable[_T]] = ..., key: Callable[[_T], Any] = ... + ) -> SortedKeyList[_T]: ... @property - def key(self) -> Callable[[T], Any]: ... + def key(self) -> Callable[[_T], Any]: ... def clear(self) -> None: ... _clear = clear - def add(self, value: T) -> None: ... + def add(self, value: _T) -> None: ... def _expand(self, pos: int) -> None: ... - def update(self, iterable: Iterable[T]) -> None: ... + def update(self, iterable: Iterable[_T]) -> None: ... _update = update # NB: Should be T to be safely passed to self.func, but base class imposes Any - def __contains__( - self, value: Any - ) -> bool: ... - def discard(self, value: T) -> None: ... - def remove(self, value: T) -> None: ... + def __contains__(self, value: Any) -> bool: ... + def discard(self, value: _T) -> None: ... + def remove(self, value: _T) -> None: ... def _delete(self, pos: int, idx: int) -> None: ... def irange( self, @@ -134,21 +146,24 @@ class SortedKeyList(typing.Generic[T], SortedList[T]): inclusive: Tuple[bool, bool] = ..., reserve: bool = ..., ): ... - def bisect_left(self, value: T) -> int: ... - def bisect_right(self, value: T) -> int: ... + def bisect_left(self, value: _T) -> int: ... + def bisect_right(self, value: _T) -> int: ... bisect = bisect_right def bisect_key_left(self, key: Any) -> int: ... _bisect_key_left = bisect_key_left def bisect_key_right(self, key: Any) -> int: ... bisect_key = bisect_key_right _bisect_key_right = bisect_key_right - def count(self, value: T) -> int: ... - def copy(self) -> SortedKeyList[T]: ... - __copy__ = copy - def index(self, value: T, start: Optional[int] = ..., stop: Optional[int] = ...) -> int: ... - def __add__(self, other: Iterable[T]) -> SortedKeyList[T]: ... - __radd__ = __add__ - def __mul__(self, num: int) -> SortedKeyList[T]: ... + def count(self, value: _T) -> int: ... + def copy(self: _SKL) -> _SKL: ... + def __copy__(self: _SKL) -> _SKL: ... + def index(self, value: _T, start: Optional[int] = ..., stop: Optional[int] = ...) -> int: ... + def __add__(self: _SKL, other: Iterable[_T]) -> _SKL: ... + def __radd__(self: _SKL, other: Iterable[_T]) -> _SKL: ... + def __iadd__(self: _SKL, other: Iterable[_T]) -> _SKL: ... + def __mul__(self: _SKL, num: int) -> _SKL: ... + def __rmul__(self: _SKL, num: int) -> _SKL: ... + def __imul__(self: _SKL, num: int) -> _SKL: ... def __repr__(self) -> str: ... def _check(self) -> None: ... diff --git a/sortedcontainers/sortedset.pyi b/sortedcontainers/sortedset.pyi index ce9911a3..26cb39d8 100644 --- a/sortedcontainers/sortedset.pyi +++ b/sortedcontainers/sortedset.pyi @@ -1,5 +1,3 @@ -from __future__ import annotations - import typing from typing import ( Any, @@ -22,61 +20,62 @@ from typing import ( # --- Global -T = TypeVar("T", bound=Hashable) -S = TypeVar("S", bound=Hashable) -Key = Callable[[T], Any] +_T = TypeVar("_T", bound=Hashable) +_S = TypeVar("_S", bound=Hashable) +_SS = TypeVar("_SS", bound="SortedSet") +_Key = Callable[[_T], Any] -class SortedSet(MutableSet[T], Sequence[T], Generic[T]): - def __init__(self, iterable: Optional[Iterable[T]] = ..., key: Optional[Key] = ...) -> None: ... +class SortedSet(MutableSet[_T], Sequence[_T], Generic[_T]): + def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key] = ...) -> None: ... @classmethod - def _fromset(cls, values: Set[T], key: Optional[Key] = ...) -> SortedSet[T]: ... + def _fromset(cls, values: Set[_T], key: Optional[_Key] = ...) -> SortedSet[_T]: ... @property - def key(self) -> Optional[Key]: ... + def key(self) -> Optional[_Key]: ... def __contains__(self, value: Any) -> bool: ... @typing.overload - def __getitem__(self, index: int) -> T: ... + def __getitem__(self, index: int) -> _T: ... @typing.overload - def __getitem__(self, index: slice) -> List[T]: ... + def __getitem__(self, index: slice) -> List[_T]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... def __eq__(self, other: Any) -> bool: ... def __ne__(self, other: Any) -> bool: ... - def __lt__(self, other: Iterable[T]) -> bool: ... - def __gt__(self, other: Iterable[T]) -> bool: ... - def __le__(self, other: Iterable[T]) -> bool: ... - def __ge__(self, other: Iterable[T]) -> bool: ... + def __lt__(self, other: Iterable[_T]) -> bool: ... + def __gt__(self, other: Iterable[_T]) -> bool: ... + def __le__(self, other: Iterable[_T]) -> bool: ... + def __ge__(self, other: Iterable[_T]) -> bool: ... def __len__(self) -> int: ... - def __iter__(self) -> Iterator[T]: ... - def __reversed__(self) -> Iterator[T]: ... - def add(self, value: T) -> None: ... + def __iter__(self) -> Iterator[_T]: ... + def __reversed__(self) -> Iterator[_T]: ... + def add(self, value: _T) -> None: ... _add = add def clear(self) -> None: ... - def copy(self) -> SortedSet[T]: ... - __copy__ = copy - def count(self, value: T) -> int: ... - def discard(self, value: T) -> None: ... + def copy(self: _SS) -> _SS: ... + def __copy__(self: _SS) -> _SS: ... + def count(self, value: _T) -> int: ... + def discard(self, value: _T) -> None: ... _discard = discard - def pop(self, index: int = ...) -> T: ... - def remove(self, value: T) -> None: ... - def difference(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def pop(self, index: int = ...) -> _T: ... + def remove(self, value: _T) -> None: ... + def difference(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __sub__ = difference - def difference_update(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def difference_update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __isub__ = difference_update - def intersection(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def intersection(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __and__ = intersection __rand__ = __and__ - def intersection_update(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def intersection_update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __iand__ = intersection_update - def symmetric_difference(self, other: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def symmetric_difference(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __xor__ = symmetric_difference __rxor__ = __xor__ - def symmetric_difference_update(self, other: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def symmetric_difference_update(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __ixor__ = symmetric_difference_update - def union(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def union(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __or__ = union __ror__ = __or__ - def update(self, *iterables: Iterable[S]) -> SortedSet[Union[T, S]]: ... + def update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... __ior__ = update _update = update - def __reduce__(self) -> Tuple[Type[SortedSet[T]], Set[T], Callable[[T], Any]]: ... + def __reduce__(self) -> Tuple[Type[SortedSet[_T]], Set[_T], Callable[[_T], Any]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... From bcc0c7c023b8fe698115879d88bead47897ed435 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 5 Dec 2018 01:49:24 +0100 Subject: [PATCH 04/14] Fix `SortedDict.update` type annotations --- sortedcontainers/sorteddict.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index 2fa76291..6a5d23da 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -70,7 +70,12 @@ class SortedDict(Dict[_KT, _VT], Generic[_KT, _VT]): def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> _VT: ... - def update(self, *args: Union[Mapping[_KT, _VT], Iterable[Tuple[_KT, _VT]]], **kwargs: _VT): ... + @typing.overload + def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... + @typing.overload + def update(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... + @typing.overload + def update(self, **kwargs: _VT) -> None: ... def __reduce__(self) -> Tuple[Type[SortedDict[_KT, _VT]], Tuple[Callable[[_KT], Any], List[Tuple[_KT, _VT]]]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... From a020db5e920f8ee8501d1af17c33028854d05e5e Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 5 Dec 2018 01:51:43 +0100 Subject: [PATCH 05/14] Remove uneeded `Generic` base classes --- sortedcontainers/sorteddict.pyi | 8 ++++---- sortedcontainers/sortedlist.pyi | 4 ++-- sortedcontainers/sortedset.pyi | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index 6a5d23da..c238a07d 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -29,7 +29,7 @@ _VT_co = TypeVar("_VT_co", covariant=True) _SD = TypeVar("_SD", bound="SortedDict") _Key = Callable[[_KT], Any] -class SortedDict(Dict[_KT, _VT], Generic[_KT, _VT]): +class SortedDict(Dict[_KT, _VT]): @typing.overload def __init__(self, **kwargs: _VT) -> None: ... @typing.overload @@ -80,14 +80,14 @@ class SortedDict(Dict[_KT, _VT], Generic[_KT, _VT]): def __repr__(self) -> str: ... def _check(self) -> None: ... -class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co], Generic[_KT_co]): +class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]): @typing.overload def __getitem__(self, index: int) -> _KT_co: ... @typing.overload def __getitem__(self, index: slice) -> List[_KT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]], Generic[_KT_co, _VT_co]): # type: ignore +class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]]): # type: ignore def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... @typing.overload def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ... @@ -95,7 +95,7 @@ class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]] def __getitem__(self, index: slice) -> List[Tuple[_KT_co, _VT_co]]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -class SortedValuesView(ValuesView[_VT_co], Sequence[_VT_co], Generic[_VT_co]): +class SortedValuesView(ValuesView[_VT_co], Sequence[_VT_co]): @typing.overload def __getitem__(self, index: int) -> _VT_co: ... @typing.overload diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi index 5b54e7a0..080886ae 100644 --- a/sortedcontainers/sortedlist.pyi +++ b/sortedcontainers/sortedlist.pyi @@ -23,7 +23,7 @@ _Repr = Callable[[], str] def recursive_repr(fillvalue: str = ...) -> Callable[[_Repr], _Repr]: ... -class SortedList(MutableSequence[_T], Generic[_T]): +class SortedList(MutableSequence[_T]): DEFAULT_LOAD_FACTOR: int = ... def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key] = ...): ... @@ -111,7 +111,7 @@ class SortedList(MutableSequence[_T], Generic[_T]): def __repr__(self) -> str: ... def _check(self) -> None: ... -class SortedKeyList(SortedList[_T], Generic[_T]): +class SortedKeyList(SortedList[_T]): def __init__( self, iterable: Optional[Iterable[_T]] = ..., key: Callable[[_T], Any] = ... ) -> None: ... diff --git a/sortedcontainers/sortedset.pyi b/sortedcontainers/sortedset.pyi index 26cb39d8..b1878edb 100644 --- a/sortedcontainers/sortedset.pyi +++ b/sortedcontainers/sortedset.pyi @@ -25,7 +25,7 @@ _S = TypeVar("_S", bound=Hashable) _SS = TypeVar("_SS", bound="SortedSet") _Key = Callable[[_T], Any] -class SortedSet(MutableSet[_T], Sequence[_T], Generic[_T]): +class SortedSet(MutableSet[_T], Sequence[_T]): def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key] = ...) -> None: ... @classmethod def _fromset(cls, values: Set[_T], key: Optional[_Key] = ...) -> SortedSet[_T]: ... From 17fd45a26389cd8247691405be62fb281653aeec Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 5 Dec 2018 19:55:47 +0100 Subject: [PATCH 06/14] Fix duplicate signatures and remove uneeded `typing` imports --- sortedcontainers/sorteddict.pyi | 52 ++++++++++++------------- sortedcontainers/sortedlist.pyi | 69 ++++++++++++++++----------------- sortedcontainers/sortedset.pyi | 42 ++++++++++---------- 3 files changed, 80 insertions(+), 83 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index c238a07d..fb6a9349 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -1,4 +1,3 @@ -import typing from typing import ( Any, Callable, @@ -18,6 +17,7 @@ from typing import ( Tuple, Union, ValuesView, + overload, ) _T = TypeVar("_T") @@ -26,24 +26,24 @@ _KT = TypeVar("_KT", bound=Hashable) # Key type. _VT = TypeVar("_VT") # Value type. _KT_co = TypeVar("_KT_co", covariant=True, bound=Hashable) _VT_co = TypeVar("_VT_co", covariant=True) -_SD = TypeVar("_SD", bound="SortedDict") -_Key = Callable[[_KT], Any] +_SD = TypeVar("_SD", bound=SortedDict) +_Key = Callable[[_T], Any] class SortedDict(Dict[_KT, _VT]): - @typing.overload + @overload def __init__(self, **kwargs: _VT) -> None: ... - @typing.overload + @overload def __init__(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... - @typing.overload + @overload def __init__(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... - @typing.overload - def __init__(self, __key: _Key, **kwargs: _VT) -> None: ... - @typing.overload - def __init__(self, __key: _Key, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... - @typing.overload - def __init__(self, __key: _Key, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... + @overload + def __init__(self, __key: _Key[_KT], **kwargs: _VT) -> None: ... + @overload + def __init__(self, __key: _Key[_KT], __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... + @overload + def __init__(self, __key: _Key[_KT], __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... @property - def key(self) -> Optional[_Key]: ... + def key(self) -> Optional[_Key[_KT]]: ... @property def iloc(self) -> SortedKeysView[_KT]: ... def clear(self) -> None: ... @@ -55,49 +55,49 @@ class SortedDict(Dict[_KT, _VT]): def copy(self: _SD) -> _SD: ... def __copy__(self: _SD) -> _SD: ... @classmethod - @typing.overload + @overload def fromkeys(cls, seq: Iterable[_T]) -> SortedDict[_T, None]: ... @classmethod - @typing.overload + @overload def fromkeys(cls, seq: Iterable[_T], value: _S) -> SortedDict[_T, _S]: ... def keys(self) -> SortedKeysView[_KT]: ... def items(self) -> SortedItemsView[_KT, _VT]: ... def values(self) -> SortedValuesView[_VT]: ... - @typing.overload + @overload def pop(self, key: _KT) -> _VT: ... - @typing.overload + @overload def pop(self, key: _KT, default: _T = ...) -> Union[_VT, _T]: ... def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> _VT: ... - @typing.overload + @overload def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... - @typing.overload + @overload def update(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... - @typing.overload + @overload def update(self, **kwargs: _VT) -> None: ... def __reduce__(self) -> Tuple[Type[SortedDict[_KT, _VT]], Tuple[Callable[[_KT], Any], List[Tuple[_KT, _VT]]]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]): - @typing.overload + @overload def __getitem__(self, index: int) -> _KT_co: ... - @typing.overload + @overload def __getitem__(self, index: slice) -> List[_KT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]]): # type: ignore def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... - @typing.overload + @overload def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ... - @typing.overload + @overload def __getitem__(self, index: slice) -> List[Tuple[_KT_co, _VT_co]]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... class SortedValuesView(ValuesView[_VT_co], Sequence[_VT_co]): - @typing.overload + @overload def __getitem__(self, index: int) -> _VT_co: ... - @typing.overload + @overload def __getitem__(self, index: slice) -> List[_VT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi index 080886ae..001f60c6 100644 --- a/sortedcontainers/sortedlist.pyi +++ b/sortedcontainers/sortedlist.pyi @@ -1,4 +1,3 @@ -import typing from typing import ( Any, Callable, @@ -8,15 +7,17 @@ from typing import ( List, MutableSequence, Optional, + Sequence, Tuple, Type, TypeVar, Union, + overload ) _T = TypeVar("_T") -_SL = TypeVar("_SL", bound="SortedList") -_SKL = TypeVar("_SKL", bound="SortedKeyList") +_SL = TypeVar("_SL", bound=SortedList) +_SKL = TypeVar("_SKL", bound=SortedKeyList) _Key = Callable[[_T], Any] _Repr = Callable[[], str] @@ -26,17 +27,17 @@ def recursive_repr(fillvalue: str = ...) -> Callable[[_Repr], _Repr]: ... class SortedList(MutableSequence[_T]): DEFAULT_LOAD_FACTOR: int = ... - def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key] = ...): ... + def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key[_T]] = ...): ... # NB: currently mypy does not honour return type, see mypy #3307 - @typing.overload + @overload def __new__(cls: Type[_SL], iterable: None, key: None) -> _SL: ... - @typing.overload - def __new__(cls: Type[_SL], iterable: None, key: _Key) -> SortedKeyList[_T]: ... - @typing.overload + @overload + def __new__(cls: Type[_SL], iterable: None, key: _Key[_T]) -> SortedKeyList[_T]: ... + @overload def __new__(cls: Type[_SL], iterable: Iterable[_T], key: None) -> _SL: ... - @typing.overload - def __new__(cls, iterable: Iterable[_T], key: _Key) -> SortedKeyList[_T]: ... + @overload + def __new__(cls, iterable: Iterable[_T], key: _Key[_T]) -> SortedKeyList[_T]: ... @property def key(self) -> Optional[Callable[[_T], Any]]: ... @@ -55,17 +56,17 @@ class SortedList(MutableSequence[_T]): def _build_index(self) -> None: ... def __contains__(self, value: Any) -> bool: ... def __delitem__(self, index: Union[int, slice]) -> None: ... - @typing.overload + @overload def __getitem__(self, index: int) -> _T: ... - @typing.overload + @overload def __getitem__(self, index: slice) -> List[_T]: ... - @typing.overload + @overload def _getitem(self, index: int) -> _T: ... - @typing.overload + @overload def _getitem(self, index: slice) -> List[_T]: ... - @typing.overload + @overload def __setitem__(self, index: int, value: _T) -> None: ... - @typing.overload + @overload def __setitem__(self, index: slice, value: Iterable[_T]) -> None: ... def __iter__(self) -> Iterator[_T]: ... def __reversed__(self) -> Iterator[_T]: ... @@ -86,8 +87,8 @@ class SortedList(MutableSequence[_T]): ) -> Iterator[_T]: ... def bisect_left(self, value: _T) -> int: ... def bisect_right(self, value: _T) -> int: ... - bisect = bisect_right - _bisect_right = bisect_right + def bisect(self, value: _T) -> int: ... + def _bisect_right(self, value: _T) -> int: ... def count(self, value: _T) -> int: ... def copy(self: _SL) -> _SL: ... def __copy__(self: _SL) -> _SL: ... @@ -104,31 +105,27 @@ class SortedList(MutableSequence[_T]): def __imul__(self: _SL, num: int) -> _SL: ... def __eq__(self, other: Any) -> bool: ... def __ne__(self, other: Any) -> bool: ... - def __lt__(self, other: typing.Sequence[_T]) -> bool: ... - def __gt__(self, other: typing.Sequence[_T]) -> bool: ... - def __le__(self, other: typing.Sequence[_T]) -> bool: ... - def __ge__(self, other: typing.Sequence[_T]) -> bool: ... + def __lt__(self, other: Sequence[_T]) -> bool: ... + def __gt__(self, other: Sequence[_T]) -> bool: ... + def __le__(self, other: Sequence[_T]) -> bool: ... + def __ge__(self, other: Sequence[_T]) -> bool: ... def __repr__(self) -> str: ... def _check(self) -> None: ... class SortedKeyList(SortedList[_T]): - def __init__( - self, iterable: Optional[Iterable[_T]] = ..., key: Callable[[_T], Any] = ... - ) -> None: ... - def __new__( - cls, iterable: Optional[Iterable[_T]] = ..., key: Callable[[_T], Any] = ... - ) -> SortedKeyList[_T]: ... + def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: _Key[_T] = ...) -> None: ... + def __new__(cls, iterable: Optional[Iterable[_T]] = ..., key: _Key[_T] = ...) -> SortedKeyList[_T]: ... @property def key(self) -> Callable[[_T], Any]: ... def clear(self) -> None: ... - _clear = clear + def _clear(self) -> None: ... def add(self, value: _T) -> None: ... def _expand(self, pos: int) -> None: ... def update(self, iterable: Iterable[_T]) -> None: ... - _update = update + def _update(self, iterable: Iterable[_T]) -> None: ... - # NB: Should be T to be safely passed to self.func, but base class imposes Any - def __contains__(self, value: Any) -> bool: ... + # NB: Must be T to be safely passed to self.func, yet base class imposes Any + def __contains__(self, value: _T) -> bool: ... # type: ignore def discard(self, value: _T) -> None: ... def remove(self, value: _T) -> None: ... def _delete(self, pos: int, idx: int) -> None: ... @@ -148,12 +145,12 @@ class SortedKeyList(SortedList[_T]): ): ... def bisect_left(self, value: _T) -> int: ... def bisect_right(self, value: _T) -> int: ... - bisect = bisect_right + def bisect(self, value: _T) -> int: ... def bisect_key_left(self, key: Any) -> int: ... - _bisect_key_left = bisect_key_left + def _bisect_key_left(self, key: Any) -> int: ... def bisect_key_right(self, key: Any) -> int: ... - bisect_key = bisect_key_right - _bisect_key_right = bisect_key_right + def _bisect_key_right(self, key: Any) -> int: ... + def bisect_key(self, key: Any) -> int: ... def count(self, value: _T) -> int: ... def copy(self: _SKL) -> _SKL: ... def __copy__(self: _SKL) -> _SKL: ... diff --git a/sortedcontainers/sortedset.pyi b/sortedcontainers/sortedset.pyi index b1878edb..9da1eba8 100644 --- a/sortedcontainers/sortedset.pyi +++ b/sortedcontainers/sortedset.pyi @@ -1,4 +1,3 @@ -import typing from typing import ( Any, AbstractSet, @@ -16,25 +15,26 @@ from typing import ( Set, TypeVar, Union, + overload, ) # --- Global _T = TypeVar("_T", bound=Hashable) _S = TypeVar("_S", bound=Hashable) -_SS = TypeVar("_SS", bound="SortedSet") +_SS = TypeVar("_SS", bound=SortedSet) _Key = Callable[[_T], Any] class SortedSet(MutableSet[_T], Sequence[_T]): - def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key] = ...) -> None: ... + def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key[_T]] = ...) -> None: ... @classmethod - def _fromset(cls, values: Set[_T], key: Optional[_Key] = ...) -> SortedSet[_T]: ... + def _fromset(cls, values: Set[_T], key: Optional[_Key[_T]] = ...) -> SortedSet[_T]: ... @property - def key(self) -> Optional[_Key]: ... + def key(self) -> Optional[_Key[_T]]: ... def __contains__(self, value: Any) -> bool: ... - @typing.overload + @overload def __getitem__(self, index: int) -> _T: ... - @typing.overload + @overload def __getitem__(self, index: slice) -> List[_T]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... def __eq__(self, other: Any) -> bool: ... @@ -47,35 +47,35 @@ class SortedSet(MutableSet[_T], Sequence[_T]): def __iter__(self) -> Iterator[_T]: ... def __reversed__(self) -> Iterator[_T]: ... def add(self, value: _T) -> None: ... - _add = add + def _add(self, value: _T) -> None: ... def clear(self) -> None: ... def copy(self: _SS) -> _SS: ... def __copy__(self: _SS) -> _SS: ... def count(self, value: _T) -> int: ... def discard(self, value: _T) -> None: ... - _discard = discard + def _discard(self, value: _T) -> None: ... def pop(self, index: int = ...) -> _T: ... def remove(self, value: _T) -> None: ... def difference(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __sub__ = difference + def __sub__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def difference_update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __isub__ = difference_update + def __isub__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def intersection(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __and__ = intersection - __rand__ = __and__ + def __and__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def __rand__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def intersection_update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __iand__ = intersection_update + def __iand__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def symmetric_difference(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __xor__ = symmetric_difference - __rxor__ = __xor__ + def __xor__(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def __rxor__(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def symmetric_difference_update(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __ixor__ = symmetric_difference_update + def __ixor__(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def union(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __or__ = union - __ror__ = __or__ + def __or__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def __ror__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - __ior__ = update - _update = update + def __ior__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def _update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def __reduce__(self) -> Tuple[Type[SortedSet[_T]], Set[_T], Callable[[_T], Any]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... From 058cf6bbfeceabc8d3b60892791add2347ccc63c Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 5 Dec 2018 19:57:07 +0100 Subject: [PATCH 07/14] Format annotation files with `black -l80` --- sortedcontainers/sorteddict.pyi | 30 ++++++++++++++++---- sortedcontainers/sortedlist.pyi | 50 +++++++++++++++++++++++---------- sortedcontainers/sortedset.pyi | 50 +++++++++++++++++++++++++-------- 3 files changed, 97 insertions(+), 33 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index fb6a9349..87e35d68 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -35,13 +35,22 @@ class SortedDict(Dict[_KT, _VT]): @overload def __init__(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... @overload - def __init__(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... + def __init__( + self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT + ) -> None: ... @overload def __init__(self, __key: _Key[_KT], **kwargs: _VT) -> None: ... @overload - def __init__(self, __key: _Key[_KT], __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... + def __init__( + self, __key: _Key[_KT], __map: Mapping[_KT, _VT], **kwargs: _VT + ) -> None: ... @overload - def __init__(self, __key: _Key[_KT], __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... + def __init__( + self, + __key: _Key[_KT], + __iterable: Iterable[Tuple[_KT, _VT]], + **kwargs: _VT + ) -> None: ... @property def key(self) -> Optional[_Key[_KT]]: ... @property @@ -73,10 +82,17 @@ class SortedDict(Dict[_KT, _VT]): @overload def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... @overload - def update(self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ... + def update( + self, __iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT + ) -> None: ... @overload def update(self, **kwargs: _VT) -> None: ... - def __reduce__(self) -> Tuple[Type[SortedDict[_KT, _VT]], Tuple[Callable[[_KT], Any], List[Tuple[_KT, _VT]]]]: ... + def __reduce__( + self + ) -> Tuple[ + Type[SortedDict[_KT, _VT]], + Tuple[Callable[[_KT], Any], List[Tuple[_KT, _VT]]], + ]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... @@ -87,7 +103,9 @@ class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]): def __getitem__(self, index: slice) -> List[_KT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -class SortedItemsView(ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]]): # type: ignore +class SortedItemsView( + ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]] +): # type: ignore def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... @overload def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ... diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi index 001f60c6..8890b92e 100644 --- a/sortedcontainers/sortedlist.pyi +++ b/sortedcontainers/sortedlist.pyi @@ -12,7 +12,7 @@ from typing import ( Type, TypeVar, Union, - overload + overload, ) _T = TypeVar("_T") @@ -23,22 +23,27 @@ _Repr = Callable[[], str] def recursive_repr(fillvalue: str = ...) -> Callable[[_Repr], _Repr]: ... - class SortedList(MutableSequence[_T]): DEFAULT_LOAD_FACTOR: int = ... - def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key[_T]] = ...): ... - + def __init__( + self, + iterable: Optional[Iterable[_T]] = ..., + key: Optional[_Key[_T]] = ..., + ): ... # NB: currently mypy does not honour return type, see mypy #3307 @overload def __new__(cls: Type[_SL], iterable: None, key: None) -> _SL: ... @overload - def __new__(cls: Type[_SL], iterable: None, key: _Key[_T]) -> SortedKeyList[_T]: ... + def __new__( + cls: Type[_SL], iterable: None, key: _Key[_T] + ) -> SortedKeyList[_T]: ... @overload def __new__(cls: Type[_SL], iterable: Iterable[_T], key: None) -> _SL: ... @overload - def __new__(cls, iterable: Iterable[_T], key: _Key[_T]) -> SortedKeyList[_T]: ... - + def __new__( + cls, iterable: Iterable[_T], key: _Key[_T] + ) -> SortedKeyList[_T]: ... @property def key(self) -> Optional[Callable[[_T], Any]]: ... def _reset(self, load: int) -> None: ... @@ -47,7 +52,7 @@ class SortedList(MutableSequence[_T]): def add(self, value: _T) -> None: ... def _expand(self, pos: int) -> None: ... def update(self, iterable: Iterable[_T]) -> None: ... - def _update(self, iterable: Iterable[_T]) -> None:... + def _update(self, iterable: Iterable[_T]) -> None: ... def discard(self, value: _T) -> None: ... def remove(self, value: _T) -> None: ... def _delete(self, pos: int, idx: int) -> None: ... @@ -73,10 +78,18 @@ class SortedList(MutableSequence[_T]): def __len__(self) -> int: ... def reverse(self) -> None: ... def islice( - self, start: Optional[int] = ..., stop: Optional[int] = ..., reverse=bool + self, + start: Optional[int] = ..., + stop: Optional[int] = ..., + reverse=bool, ) -> Iterator[_T]: ... def _islice( - self, min_pos: int, min_idx: int, max_pos: int, max_idx: int, reverse: bool + self, + min_pos: int, + min_idx: int, + max_pos: int, + max_idx: int, + reverse: bool, ) -> Iterator[_T]: ... def irange( self, @@ -96,7 +109,9 @@ class SortedList(MutableSequence[_T]): def extend(self, values: Iterable[_T]) -> None: ... def insert(self, index: int, value: _T) -> None: ... def pop(self, index: int = -1) -> _T: ... - def index(self, value: _T, start: Optional[int] = None, stop: Optional[int] = None) -> int: ... + def index( + self, value: _T, start: Optional[int] = None, stop: Optional[int] = None + ) -> int: ... def __add__(self: _SL, other: Iterable[_T]) -> _SL: ... def __radd__(self: _SL, other: Iterable[_T]) -> _SL: ... def __iadd__(self: _SL, other: Iterable[_T]) -> _SL: ... @@ -113,8 +128,12 @@ class SortedList(MutableSequence[_T]): def _check(self) -> None: ... class SortedKeyList(SortedList[_T]): - def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: _Key[_T] = ...) -> None: ... - def __new__(cls, iterable: Optional[Iterable[_T]] = ..., key: _Key[_T] = ...) -> SortedKeyList[_T]: ... + def __init__( + self, iterable: Optional[Iterable[_T]] = ..., key: _Key[_T] = ... + ) -> None: ... + def __new__( + cls, iterable: Optional[Iterable[_T]] = ..., key: _Key[_T] = ... + ) -> SortedKeyList[_T]: ... @property def key(self) -> Callable[[_T], Any]: ... def clear(self) -> None: ... @@ -123,7 +142,6 @@ class SortedKeyList(SortedList[_T]): def _expand(self, pos: int) -> None: ... def update(self, iterable: Iterable[_T]) -> None: ... def _update(self, iterable: Iterable[_T]) -> None: ... - # NB: Must be T to be safely passed to self.func, yet base class imposes Any def __contains__(self, value: _T) -> bool: ... # type: ignore def discard(self, value: _T) -> None: ... @@ -154,7 +172,9 @@ class SortedKeyList(SortedList[_T]): def count(self, value: _T) -> int: ... def copy(self: _SKL) -> _SKL: ... def __copy__(self: _SKL) -> _SKL: ... - def index(self, value: _T, start: Optional[int] = ..., stop: Optional[int] = ...) -> int: ... + def index( + self, value: _T, start: Optional[int] = ..., stop: Optional[int] = ... + ) -> int: ... def __add__(self: _SKL, other: Iterable[_T]) -> _SKL: ... def __radd__(self: _SKL, other: Iterable[_T]) -> _SKL: ... def __iadd__(self: _SKL, other: Iterable[_T]) -> _SKL: ... diff --git a/sortedcontainers/sortedset.pyi b/sortedcontainers/sortedset.pyi index 9da1eba8..2086f76e 100644 --- a/sortedcontainers/sortedset.pyi +++ b/sortedcontainers/sortedset.pyi @@ -26,9 +26,15 @@ _SS = TypeVar("_SS", bound=SortedSet) _Key = Callable[[_T], Any] class SortedSet(MutableSet[_T], Sequence[_T]): - def __init__(self, iterable: Optional[Iterable[_T]] = ..., key: Optional[_Key[_T]] = ...) -> None: ... + def __init__( + self, + iterable: Optional[Iterable[_T]] = ..., + key: Optional[_Key[_T]] = ..., + ) -> None: ... @classmethod - def _fromset(cls, values: Set[_T], key: Optional[_Key[_T]] = ...) -> SortedSet[_T]: ... + def _fromset( + cls, values: Set[_T], key: Optional[_Key[_T]] = ... + ) -> SortedSet[_T]: ... @property def key(self) -> Optional[_Key[_T]]: ... def __contains__(self, value: Any) -> bool: ... @@ -56,19 +62,37 @@ class SortedSet(MutableSet[_T], Sequence[_T]): def _discard(self, value: _T) -> None: ... def pop(self, index: int = ...) -> _T: ... def remove(self, value: _T) -> None: ... - def difference(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def difference( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... def __sub__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def difference_update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def __isub__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def intersection(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def difference_update( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... + def __isub__( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... + def intersection( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... def __and__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def __rand__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def intersection_update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def __iand__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def symmetric_difference(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def __rand__( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... + def intersection_update( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... + def __iand__( + self, *iterables: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... + def symmetric_difference( + self, other: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... def __xor__(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def __rxor__(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def symmetric_difference_update(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... + def symmetric_difference_update( + self, other: Iterable[_S] + ) -> SortedSet[Union[_T, _S]]: ... def __ixor__(self, other: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def union(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def __or__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... @@ -76,6 +100,8 @@ class SortedSet(MutableSet[_T], Sequence[_T]): def update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def __ior__(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... def _update(self, *iterables: Iterable[_S]) -> SortedSet[Union[_T, _S]]: ... - def __reduce__(self) -> Tuple[Type[SortedSet[_T]], Set[_T], Callable[[_T], Any]]: ... + def __reduce__( + self + ) -> Tuple[Type[SortedSet[_T]], Set[_T], Callable[[_T], Any]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... From 1194865f0716e9dde626f4f74a8fcc8b53473814 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 5 Dec 2018 19:58:50 +0100 Subject: [PATCH 08/14] Add `mypy` and `pyre` caches to the `.gitignore` list --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 6a84a336..7ded41e8 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,7 @@ # macOS metadata .DS_Store + +# typing cache +.mypy_cache/ +.pyre/ From 94cec9d0ccc88be04ab8de2821e82afd2ab4fa0f Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 7 Dec 2018 14:41:58 +0100 Subject: [PATCH 09/14] Remove default values from stub methods in `.pyi` files --- sortedcontainers/sorteddict.pyi | 4 ++-- sortedcontainers/sortedlist.pyi | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index 87e35d68..ff03ede7 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -103,9 +103,9 @@ class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]): def __getitem__(self, index: slice) -> List[_KT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -class SortedItemsView( +class SortedItemsView( # type: ignore ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]] -): # type: ignore +): def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... @overload def __getitem__(self, index: int) -> Tuple[_KT_co, _VT_co]: ... diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi index 8890b92e..7bf37553 100644 --- a/sortedcontainers/sortedlist.pyi +++ b/sortedcontainers/sortedlist.pyi @@ -108,9 +108,9 @@ class SortedList(MutableSequence[_T]): def append(self, value: _T) -> None: ... def extend(self, values: Iterable[_T]) -> None: ... def insert(self, index: int, value: _T) -> None: ... - def pop(self, index: int = -1) -> _T: ... + def pop(self, index: int = ...) -> _T: ... def index( - self, value: _T, start: Optional[int] = None, stop: Optional[int] = None + self, value: _T, start: Optional[int] = ..., stop: Optional[int] = ... ) -> int: ... def __add__(self: _SL, other: Iterable[_T]) -> _SL: ... def __radd__(self: _SL, other: Iterable[_T]) -> _SL: ... From 237c382a6bf12d3090014b1359aec8e106b4ada1 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Thu, 13 Dec 2018 18:59:15 +0100 Subject: [PATCH 10/14] Fix signature of `SortedDict.setdefault` and `SortedDict.fromkeys` --- sortedcontainers/sorteddict.pyi | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index ff03ede7..cf5fa49c 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -22,6 +22,7 @@ from typing import ( _T = TypeVar("_T") _S = TypeVar("_S") +_T_h = TypeVar("_T", bound=Hashable) _KT = TypeVar("_KT", bound=Hashable) # Key type. _VT = TypeVar("_VT") # Value type. _KT_co = TypeVar("_KT_co", covariant=True, bound=Hashable) @@ -65,10 +66,10 @@ class SortedDict(Dict[_KT, _VT]): def __copy__(self: _SD) -> _SD: ... @classmethod @overload - def fromkeys(cls, seq: Iterable[_T]) -> SortedDict[_T, None]: ... + def fromkeys(cls, seq: Iterable[_T_h]) -> SortedDict[_T_h, None]: ... @classmethod @overload - def fromkeys(cls, seq: Iterable[_T], value: _S) -> SortedDict[_T, _S]: ... + def fromkeys(cls, seq: Iterable[_T_h], value: _S) -> SortedDict[_T_h, _S]: ... def keys(self) -> SortedKeysView[_KT]: ... def items(self) -> SortedItemsView[_KT, _VT]: ... def values(self) -> SortedValuesView[_VT]: ... @@ -78,7 +79,7 @@ class SortedDict(Dict[_KT, _VT]): def pop(self, key: _KT, default: _T = ...) -> Union[_VT, _T]: ... def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... - def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> _VT: ... + def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> Optional[_VT]: ... @overload def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... @overload From 29f1711f9c66c22b1c8912ee4dcfd99ce91066a8 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 14 Jan 2020 14:32:00 +0100 Subject: [PATCH 11/14] Fix latest linting error in `sorteddict.py` --- sortedcontainers/sortedlist.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sortedcontainers/sortedlist.py b/sortedcontainers/sortedlist.py index e3b58eb9..dc685886 100644 --- a/sortedcontainers/sortedlist.py +++ b/sortedcontainers/sortedlist.py @@ -16,9 +16,6 @@ # pylint: disable=too-many-lines from __future__ import print_function -import sys -import traceback - from bisect import bisect_left, bisect_right, insort from itertools import chain, repeat, starmap from math import log @@ -1670,6 +1667,8 @@ def _check(self): child_sum = self._index[child] + self._index[child + 1] assert child_sum == self._index[pos] except: + import sys # pylint: disable=import-outside-toplevel + import traceback # pylint: disable=import-outside-toplevel traceback.print_exc(file=sys.stdout) print('len', self._len) print('load', self._load) @@ -2628,6 +2627,8 @@ def _check(self): child_sum = self._index[child] + self._index[child + 1] assert child_sum == self._index[pos] except: + import sys # pylint: disable=import-outside-toplevel + import traceback # pylint: disable=import-outside-toplevel traceback.print_exc(file=sys.stdout) print('len', self._len) print('load', self._load) From 09127aa8cc9e8060e4a5aa018b770e766234f9e0 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 14 Jan 2020 17:26:10 +0100 Subject: [PATCH 12/14] Remove uneeded `type: ignore` in `sorteddict.pyi` --- sortedcontainers/sorteddict.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index cf5fa49c..2e60b027 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -104,7 +104,7 @@ class SortedKeysView(KeysView[_KT_co], Sequence[_KT_co]): def __getitem__(self, index: slice) -> List[_KT_co]: ... def __delitem__(self, index: Union[int, slice]) -> None: ... -class SortedItemsView( # type: ignore +class SortedItemsView( ItemsView[_KT_co, _VT_co], Sequence[Tuple[_KT_co, _VT_co]] ): def __iter__(self) -> Iterator[Tuple[_KT_co, _VT_co]]: ... From 5e16045457f02aaad69c6badcb683c69ae4398d7 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Tue, 14 Jan 2020 17:35:17 +0100 Subject: [PATCH 13/14] Fix signature of `SortedDict.setdefault` to comply with `dict` signature --- sortedcontainers/sorteddict.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sortedcontainers/sorteddict.pyi b/sortedcontainers/sorteddict.pyi index 2e60b027..4087cfd9 100644 --- a/sortedcontainers/sorteddict.pyi +++ b/sortedcontainers/sorteddict.pyi @@ -22,7 +22,7 @@ from typing import ( _T = TypeVar("_T") _S = TypeVar("_S") -_T_h = TypeVar("_T", bound=Hashable) +_T_h = TypeVar("_T_h", bound=Hashable) _KT = TypeVar("_KT", bound=Hashable) # Key type. _VT = TypeVar("_VT") # Value type. _KT_co = TypeVar("_KT_co", covariant=True, bound=Hashable) @@ -79,7 +79,7 @@ class SortedDict(Dict[_KT, _VT]): def pop(self, key: _KT, default: _T = ...) -> Union[_VT, _T]: ... def popitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... def peekitem(self, index: int = ...) -> Tuple[_KT, _VT]: ... - def setdefault(self, key: _KT, default: Optional[_VT] = ...) -> Optional[_VT]: ... + def setdefault(self, key: _KT, default: _VT = ...) -> _VT: ... @overload def update(self, __map: Mapping[_KT, _VT], **kwargs: _VT) -> None: ... @overload From d0a225d7fd0fb4c54532b8798af3cbeebf97e2d5 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 8 May 2021 17:07:48 +0200 Subject: [PATCH 14/14] Add type annotation fixes suggested by @SimonBin --- sortedcontainers/sortedlist.pyi | 8 ++++---- sortedcontainers/sortedset.pyi | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/sortedcontainers/sortedlist.pyi b/sortedcontainers/sortedlist.pyi index 7bf37553..cabc697e 100644 --- a/sortedcontainers/sortedlist.pyi +++ b/sortedcontainers/sortedlist.pyi @@ -93,8 +93,8 @@ class SortedList(MutableSequence[_T]): ) -> Iterator[_T]: ... def irange( self, - minimum: Optional[int] = ..., - maximum: Optional[int] = ..., + minimum: Optional[_T] = ..., + maximum: Optional[_T] = ..., inclusive: Tuple[bool, bool] = ..., reverse: bool = ..., ) -> Iterator[_T]: ... @@ -149,8 +149,8 @@ class SortedKeyList(SortedList[_T]): def _delete(self, pos: int, idx: int) -> None: ... def irange( self, - minimum: Optional[int] = ..., - maximum: Optional[int] = ..., + minimum: Optional[_T] = ..., + maximum: Optional[_T] = ..., inclusive: Tuple[bool, bool] = ..., reverse: bool = ..., ): ... diff --git a/sortedcontainers/sortedset.pyi b/sortedcontainers/sortedset.pyi index 2086f76e..5cc07a4e 100644 --- a/sortedcontainers/sortedset.pyi +++ b/sortedcontainers/sortedset.pyi @@ -105,3 +105,22 @@ class SortedSet(MutableSet[_T], Sequence[_T]): ) -> Tuple[Type[SortedSet[_T]], Set[_T], Callable[[_T], Any]]: ... def __repr__(self) -> str: ... def _check(self) -> None: ... + def bisect_left(self, value: _T) -> int: ... + def bisect_right(self, value: _T) -> int: ... + def islice( + self, + start: Optional[int] = ..., + stop: Optional[int] = ..., + reverse=bool, + ) -> Iterator[_T]: ... + def irange( + self, + minimum: Optional[_T] = ..., + maximum: Optional[_T] = ..., + inclusive: Tuple[bool, bool] = ..., + reverse: bool = ..., + ) -> Iterator[_T]: ... + def index( + self, value: _T, start: Optional[int] = ..., stop: Optional[int] = ... + ) -> int: ... + def _reset(self, load: int) -> None: ...