From 60e344dac9bdd9294f9ad6b5b7c229ef05aff78a Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 17:49:07 +0000 Subject: [PATCH 1/4] stdlib: Add several missing comparison methods --- stdlib/asyncio/events.pyi | 5 +++++ stdlib/collections/__init__.pyi | 8 ++++++++ stdlib/tracemalloc.pyi | 8 ++++++++ stdlib/urllib/request.pyi | 1 + stdlib/xml/etree/ElementTree.pyi | 4 ++++ 5 files changed, 26 insertions(+) diff --git a/stdlib/asyncio/events.pyi b/stdlib/asyncio/events.pyi index 491410977963..c86c0a99182e 100644 --- a/stdlib/asyncio/events.pyi +++ b/stdlib/asyncio/events.pyi @@ -55,6 +55,11 @@ class TimerHandle(Handle): if sys.version_info >= (3, 7): def when(self) -> float: ... + def __lt__(self, other: TimerHandle) -> bool: ... + def __le__(self, other: TimerHandle) -> bool: ... + def __gt__(self, other: TimerHandle) -> bool: ... + def __ge__(self, other: TimerHandle) -> bool: ... + class AbstractServer: @abstractmethod def close(self) -> None: ... diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 36849f40a7c0..5b8fdbe01505 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -220,6 +220,10 @@ class deque(MutableSequence[_T], Generic[_T]): def __add__(self: Self, __other: Self) -> Self: ... def __mul__(self: Self, __other: int) -> Self: ... def __imul__(self: Self, __other: int) -> Self: ... + def __lt__(self, __other: deque[_T]) -> bool: ... + def __le__(self, __other: deque[_T]) -> bool: ... + def __gt__(self, __other: deque[_T]) -> bool: ... + def __ge__(self, __other: deque[_T]) -> bool: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... @@ -266,6 +270,10 @@ class Counter(dict[_T, int], Generic[_T]): def __ior__(self: Self, other: Counter[_T]) -> Self: ... # type: ignore[override] if sys.version_info >= (3, 10): def total(self) -> int: ... + def __le__(self, other: Counter[object]) -> bool: ... + def __lt__(self, other: Counter[object]) -> bool: ... + def __ge__(self, other: Counter[object]) -> bool: ... + def __gt__(self, other: Counter[object]) -> bool: ... @final class _OrderedDictKeysView(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] diff --git a/stdlib/tracemalloc.pyi b/stdlib/tracemalloc.pyi index 0af707bb584b..137ed3a47660 100644 --- a/stdlib/tracemalloc.pyi +++ b/stdlib/tracemalloc.pyi @@ -41,6 +41,10 @@ class Frame: filename: str lineno: int def __init__(self, frame: _FrameTupleT) -> None: ... + def __lt__(self, other: Frame) -> bool: ... + def __gt__(self, other: Frame) -> bool: ... + def __ge__(self, other: Frame) -> bool: ... + def __gt__(self, other: Frame) -> bool: ... if sys.version_info >= (3, 9): _TraceTupleT = Union[tuple[int, int, Sequence[_FrameTupleT], Optional[int]], tuple[int, int, Sequence[_FrameTupleT]]] @@ -69,6 +73,10 @@ class Traceback(Sequence[Frame]): @overload def __getitem__(self, s: slice) -> Sequence[Frame]: ... def __len__(self) -> int: ... + def __lt__(self, other: Traceback) -> bool: ... + def __gt__(self, other: Traceback) -> bool: ... + def __le__(self, other: Traceback) -> bool: ... + def __ge__(self, other: Traceback) -> bool: ... class Snapshot: def __init__(self, traces: Sequence[_TraceTupleT], traceback_limit: int) -> None: ... diff --git a/stdlib/urllib/request.pyi b/stdlib/urllib/request.pyi index a69bd650c810..6d59c5d5bfde 100644 --- a/stdlib/urllib/request.pyi +++ b/stdlib/urllib/request.pyi @@ -92,6 +92,7 @@ class BaseHandler: parent: OpenerDirector def add_parent(self, parent: OpenerDirector) -> None: ... def close(self) -> None: ... + def __lt__(self, other: object) -> bool: ... class HTTPDefaultErrorHandler(BaseHandler): def http_error_default( diff --git a/stdlib/xml/etree/ElementTree.pyi b/stdlib/xml/etree/ElementTree.pyi index fa1c01c83411..4561fc82c704 100644 --- a/stdlib/xml/etree/ElementTree.pyi +++ b/stdlib/xml/etree/ElementTree.pyi @@ -111,6 +111,10 @@ PI: Callable[..., Element] class QName: text: str def __init__(self, text_or_uri: str, tag: str | None = ...) -> None: ... + def __lt__(self, other: QName | str) -> bool: ... + def __le__(self, other: QName | str) -> bool: ... + def __gt__(self, other: QName | str) -> bool: ... + def __ge__(self, other: QName | str) -> bool: ... class ElementTree: def __init__(self, element: Element | None = ..., file: _File | None = ...) -> None: ... From c56e7d554ac76e529f5db6baa7a4df7e210d048f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 17:52:45 +0000 Subject: [PATCH 2/4] Update tracemalloc.pyi --- stdlib/tracemalloc.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/tracemalloc.pyi b/stdlib/tracemalloc.pyi index 137ed3a47660..52a41773d7e3 100644 --- a/stdlib/tracemalloc.pyi +++ b/stdlib/tracemalloc.pyi @@ -44,7 +44,7 @@ class Frame: def __lt__(self, other: Frame) -> bool: ... def __gt__(self, other: Frame) -> bool: ... def __ge__(self, other: Frame) -> bool: ... - def __gt__(self, other: Frame) -> bool: ... + def __le__(self, other: Frame) -> bool: ... if sys.version_info >= (3, 9): _TraceTupleT = Union[tuple[int, int, Sequence[_FrameTupleT], Optional[int]], tuple[int, int, Sequence[_FrameTupleT]]] From fa128ab9ccdf978a8c61e57d99b5e7a8fb5944ed Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 17:59:58 +0000 Subject: [PATCH 3/4] Stupid CPython micro-optimisation --- stdlib/tracemalloc.pyi | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/stdlib/tracemalloc.pyi b/stdlib/tracemalloc.pyi index 52a41773d7e3..ad88f8901f75 100644 --- a/stdlib/tracemalloc.pyi +++ b/stdlib/tracemalloc.pyi @@ -1,6 +1,6 @@ import sys from _tracemalloc import * -from typing import Optional, Sequence, Union, overload +from typing import Any, Optional, Sequence, Union, overload from typing_extensions import SupportsIndex def get_object_traceback(obj: object) -> Traceback | None: ... @@ -41,10 +41,16 @@ class Frame: filename: str lineno: int def __init__(self, frame: _FrameTupleT) -> None: ... - def __lt__(self, other: Frame) -> bool: ... - def __gt__(self, other: Frame) -> bool: ... - def __ge__(self, other: Frame) -> bool: ... - def __le__(self, other: Frame) -> bool: ... + if sys.version_info >= (3, 11): + def __lt__(self, other: Frame) -> bool: ... + def __gt__(self, other: Frame) -> bool: ... + def __ge__(self, other: Frame) -> bool: ... + def __le__(self, other: Frame) -> bool: ... + else: + def __lt__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... + def __gt__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... + def __ge__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... + def __le__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... if sys.version_info >= (3, 9): _TraceTupleT = Union[tuple[int, int, Sequence[_FrameTupleT], Optional[int]], tuple[int, int, Sequence[_FrameTupleT]]] @@ -73,10 +79,16 @@ class Traceback(Sequence[Frame]): @overload def __getitem__(self, s: slice) -> Sequence[Frame]: ... def __len__(self) -> int: ... - def __lt__(self, other: Traceback) -> bool: ... - def __gt__(self, other: Traceback) -> bool: ... - def __le__(self, other: Traceback) -> bool: ... - def __ge__(self, other: Traceback) -> bool: ... + if sys.version_info >= (3, 11): + def __lt__(self, other: Traceback) -> bool: ... + def __gt__(self, other: Traceback) -> bool: ... + def __ge__(self, other: Traceback) -> bool: ... + def __le__(self, other: Traceback) -> bool: ... + else: + def __lt__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... + def __gt__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... + def __ge__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... + def __le__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... class Snapshot: def __init__(self, traces: Sequence[_TraceTupleT], traceback_limit: int) -> None: ... From b28484b57b875874b66b86ac9c5352abc0a58c53 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 14 Feb 2022 18:02:31 +0000 Subject: [PATCH 4/4] Whose side are you on, stubtest? --- stdlib/tracemalloc.pyi | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/stdlib/tracemalloc.pyi b/stdlib/tracemalloc.pyi index ad88f8901f75..3b6e7f676d90 100644 --- a/stdlib/tracemalloc.pyi +++ b/stdlib/tracemalloc.pyi @@ -41,13 +41,12 @@ class Frame: filename: str lineno: int def __init__(self, frame: _FrameTupleT) -> None: ... + def __lt__(self, other: Frame) -> bool: ... if sys.version_info >= (3, 11): - def __lt__(self, other: Frame) -> bool: ... def __gt__(self, other: Frame) -> bool: ... def __ge__(self, other: Frame) -> bool: ... def __le__(self, other: Frame) -> bool: ... else: - def __lt__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... def __gt__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... def __ge__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... def __le__(self, other: Frame, NotImplemented: Any = ...) -> bool: ... @@ -79,13 +78,12 @@ class Traceback(Sequence[Frame]): @overload def __getitem__(self, s: slice) -> Sequence[Frame]: ... def __len__(self) -> int: ... + def __lt__(self, other: Traceback) -> bool: ... if sys.version_info >= (3, 11): - def __lt__(self, other: Traceback) -> bool: ... def __gt__(self, other: Traceback) -> bool: ... def __ge__(self, other: Traceback) -> bool: ... def __le__(self, other: Traceback) -> bool: ... else: - def __lt__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... def __gt__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... def __ge__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ... def __le__(self, other: Traceback, NotImplemented: Any = ...) -> bool: ...