Skip to content

typing should have a single-method protocol for__lt__ #728

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

Closed
ramalho opened this issue May 28, 2020 · 2 comments
Closed

typing should have a single-method protocol for__lt__ #728

ramalho opened this issue May 28, 2020 · 2 comments

Comments

@ramalho
Copy link

ramalho commented May 28, 2020

On typeshed there are several issues that can be fixed with a protocol like this:

class _Sortable(Protocol):
        def __lt__(self, other) -> bool: ...
_SortableT = TypeVar("_SortableT", bound=_Sortable)

That protocol is already defined ad hoc in statistics.pyi, to solve a couple of cases there, but it can also be used in builtins.pyi for max(), min() sorted(), list.sort() or any other function that depends on sorting items to do its job.

For many user-defined functions that use one of these functions internally, a protocol like _Sortable is needed to provide accurate type hints. That's the case of statistics.median_high and median_low: the use cases that prompted the definition of _Sortable in statistics.pyi.

I'm not sure what its name should be. A few options:

  • Sortable is OK by me.
  • PEP 484 has an example ABC named Comparable that defines __lt__—but the text suggests the other comparison operators would be part of that ABC.
  • typing has a bunch of SupportsXyz protocols, so maybe SupportsLessThan?
@ramalho ramalho changed the title typing should include a single-method protocol for__lt__ typing should have a single-method protocol for__lt__ May 28, 2020
@srittau
Copy link
Collaborator

srittau commented May 28, 2020

See also python/typeshed#4131 for a suggestion to add such protocols to typeshed for now and then possibly transferring them to typing if they prove to be used.

@gvanrossum
Copy link
Member

This should probably be an issue (and preferably a PR) for typeshed.

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

No branches or pull requests

3 participants