[Python] How can I hint that a type is comparable with typing

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    Say I want to write a comparison sorting function, I can hint that the input must be a sequence with Sequence[T] (or MutableSequence[T] in this case).

    from typing import MutableSequence, T

    def comparison_sort(s: MutableSequence[T]) -> None:
    pass


    However there does not seems to be an out-of-the box way to hint that T must be comparable. (There is no Comparable or Ordered or anything it seems in typing.) How can I achieve this? I would like to avoid specifying a specific set of types, like int, float, str so that a user could also hint that their own type is comparable.

    Continue reading...

Compartilhe esta Página