Use basedpyright refactor

This commit is contained in:
marc
2025-03-22 23:06:34 +01:00
parent ac54453b7f
commit 2f7c7c2429
41 changed files with 480 additions and 381 deletions

View File

@@ -1,6 +1,22 @@
import itertools
from collections.abc import Callable, Iterable, Iterator
from typing import Protocol, Self, TypeVar
def groupby(it, key_fn=lambda x: x, group_fn=lambda x: x):
class SupportsLessThan(Protocol):
def __lt__(self, other: Self) -> bool:
raise NotImplementedError()
T = TypeVar("T")
KeyT = TypeVar("KeyT", bound=SupportsLessThan)
GroupT = TypeVar("GroupT")
def groupby(
it: Iterable[T],
key_fn: Callable[[T], KeyT],
group_fn: Callable[[Iterable[T]], GroupT] = lambda x: x,
) -> Iterator[tuple[KeyT, GroupT]]:
for k, g in itertools.groupby(sorted(it, key=key_fn), key=key_fn):
yield k, group_fn(g)