mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-09 21:46:42 +08:00
Make itertools.groupby covariant (#11032)
This commit is contained in:
@@ -9,7 +9,7 @@ import collections
|
||||
import math
|
||||
import operator
|
||||
import sys
|
||||
from itertools import chain, combinations, count, cycle, filterfalse, islice, repeat, starmap, tee, zip_longest
|
||||
from itertools import chain, combinations, count, cycle, filterfalse, groupby, islice, repeat, starmap, tee, zip_longest
|
||||
from typing import Any, Callable, Hashable, Iterable, Iterator, Sequence, Tuple, Type, TypeVar, Union, overload
|
||||
from typing_extensions import Literal, TypeAlias, TypeVarTuple, Unpack
|
||||
|
||||
@@ -272,6 +272,15 @@ def unique_everseen(iterable: Iterable[_T], key: Callable[[_T], Hashable] | None
|
||||
# yield from dict(zip(map(key, t1), t2)).values()
|
||||
|
||||
|
||||
# Slightly adapted from the docs recipe; a one-liner was a bit much for pyright
|
||||
def unique_justseen(iterable: Iterable[_T], key: Callable[[_T], bool] | None = None) -> Iterator[_T]:
|
||||
"List unique elements, preserving order. Remember only the element just seen."
|
||||
# unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
|
||||
# unique_justseen('ABBcCAD', str.lower) --> A B c A D
|
||||
g: groupby[_T | bool, _T] = groupby(iterable, key)
|
||||
return map(next, map(operator.itemgetter(1), g))
|
||||
|
||||
|
||||
def powerset(iterable: Iterable[_T]) -> Iterator[tuple[_T, ...]]:
|
||||
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
||||
s = list(iterable)
|
||||
|
||||
Reference in New Issue
Block a user