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 LessThen(Protocol): | n | class SupportsLessThan(Protocol): |
| | | |
| def __lt__(self, other: object) -> bool: | | def __lt__(self, other: object) -> bool: |
| ... | | ... |
n | Comparable = TypeVar('Comparable', bound=LessThen) | n | Comparable = TypeVar('Comparable', bound=SupportsLessThan) |
| Sortable = MutableSequence[Comparable] | | Sortable = MutableSequence[Comparable] |
| | | |
n | def defkey(element: Comparable) -> LessThen: | n | def defkey(element: Comparable) -> SupportsLessThan: |
| if isinstance(element, Sized): | | if isinstance(element, Sized): |
| return len(element) | | return len(element) |
| return element | | return element |
| | | |
t | def strictsort(inp: Sortable[Comparable], key: Callable[[Comparable], Le | t | def strictsort(seq: Sortable[Comparable], key: Callable[[Comparable], Su |
| ssThen]=defkey) -> Sortable[Comparable]: | | pportsLessThan]=defkey) -> Sortable[Comparable]: |
| lst = list(inp) | | tmp = list(seq) |
| lst.sort(key=key) | | tmp.sort(key=key) |
| inp[:] = lst | | seq[:] = tmp |
| return inp | | return seq |