Use basedpyright refactor
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user