Skip to content

stdlib: Add several missing comparison methods #7202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stdlib/asyncio/events.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down
8 changes: 8 additions & 0 deletions stdlib/collections/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...

Expand Down Expand Up @@ -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: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use Counter[Any] here. It doesn't work with all Counters, only with Counters of a type that's comparable to our type. That's something we can't express in the current type system, so we should use Any.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> from collections import Counter
>>> Counter('abcde') < Counter((1, 2, 3))
False

What am I missing? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, the type parameter is the keys, but it compares the values, which are always ints.

This does make for some fun behavior:

In [16]: Counter((1, 2, 3)) < Counter('abcde')
Out[16]: False

In [17]: Counter('abcde') < Counter((1, 2, 3))
Out[17]: False

Copy link
Member Author

@AlexWaygood AlexWaygood Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Counters are sort of halfway between dicts and sets, and the comparison methods come from the set-like side of their personality, so it does sort of make sense if you squint at it from the right angle.

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]
Expand Down
20 changes: 19 additions & 1 deletion stdlib/tracemalloc.pyi
Original file line number Diff line number Diff line change
@@ -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: ...
Expand Down Expand Up @@ -41,6 +41,15 @@ class Frame:
filename: str
lineno: int
def __init__(self, frame: _FrameTupleT) -> None: ...
def __lt__(self, other: Frame) -> bool: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that __lt__ for some reason does not have the NotImplemented trick.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because it's functools.total_ordering that uses the NotImplemented trick, and __lt__ is the only method that's actually defined on the class (all the other methods are autogenerated using @functools.total_ordering).

Copy link
Member Author

@AlexWaygood AlexWaygood Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This recent change is why I used the sys.version_info guards, FWIW: python/cpython#30818

if sys.version_info >= (3, 11):
def __gt__(self, other: Frame) -> bool: ...
def __ge__(self, other: Frame) -> bool: ...
def __le__(self, other: Frame) -> bool: ...
else:
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]]]
Expand Down Expand Up @@ -69,6 +78,15 @@ 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 __gt__(self, other: Traceback) -> bool: ...
def __ge__(self, other: Traceback) -> bool: ...
def __le__(self, other: Traceback) -> bool: ...
else:
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: ...
Expand Down
1 change: 1 addition & 0 deletions stdlib/urllib/request.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions stdlib/xml/etree/ElementTree.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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: ...
Expand Down