FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

stdlib: Add several missing comparison methods by AlexWaygood · Pull Request #7202 · python/typeshed · GitHub

Repository navigation

stdlib: Add several missing comparison methods - #7202

Merged
JelleZijlstra merged 4 commits into
python:masterfrom
AlexWaygood:comparisons
Feb 14, 2022
Merged

JelleZijlstra merged 4 commits into
python:masterfrom
AlexWaygood:comparisons

Conversation

Copy link
Copy Markdown
Member

No description provided.

Comment thread stdlib/tracemalloc.pyi Outdated

This comment has been minimized.

This comment has been minimized.

This comment has been minimized.

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
>>> from collections import Counter
>>> Counter('abcde') < Counter((1, 2, 3))
False

What am I missing? :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

AlexWaygood Feb 14, 2022 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment thread stdlib/tracemalloc.pyi
filename: str
lineno: int
def __init__(self, frame: _FrameTupleT) -> None: ...
def __lt__(self, other: Frame) -> bool: ...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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).

AlexWaygood Feb 14, 2022 •
edited
Loading

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL