f | from typing import Protocol, TypeVar, Callable, runtime_checkable | f | from typing import Protocol, TypeVar, Callable, runtime_checkable |
| from collections.abc import MutableSequence, Sized | | from collections.abc import MutableSequence, Sized |
| | | |
| @runtime_checkable | | @runtime_checkable |
n | class SupportsLessThan(Protocol): | n | class LessThen(Protocol): |
| | | |
| def __lt__(self, other: object) -> bool: | | def __lt__(self, other: object) -> bool: |
| ... | | ... |
n | Comparable = TypeVar('Comparable', bound=SupportsLessThan) | n | Comparable = TypeVar('Comparable', bound=LessThen) |
| Sortable = MutableSequence[Comparable] | | Sortable = MutableSequence[Comparable] |
| | | |
n | def defkey(element: Comparable) -> SupportsLessThan: | n | def defkey(element: Comparable) -> LessThen: |
| if isinstance(element, Sized): | | if isinstance(element, Sized): |
| return len(element) | | return len(element) |
| return element | | return element |
| | | |
t | def strictsort(seq: Sortable[Comparable], key: Callable[[Comparable], Su | t | def strictsort(inp: Sortable[Comparable], key: Callable[[Comparable], Le |
| pportsLessThan]=defkey) -> Sortable[Comparable]: | | ssThen]=defkey) -> Sortable[Comparable]: |
| tmp = list(seq) | | lst = list(inp) |
| tmp.sort(key=key) | | lst.sort(key=key) |
| seq[:] = tmp | | inp[:] = lst |
| return seq | | return inp |