|
22 | 22 | Sequence, |
23 | 23 | Tuple, |
24 | 24 | Type, |
| 25 | + TypeAlias, |
25 | 26 | TypeVar, |
26 | 27 | Union, |
27 | 28 | overload, |
28 | 29 | ) |
29 | | -from typing_extensions import TypeAlias, TypeVarTuple, Unpack |
| 30 | +from typing_extensions import TypeVarTuple, Unpack |
30 | 31 |
|
31 | 32 | _T = TypeVar("_T") |
32 | 33 | _T1 = TypeVar("_T1") |
@@ -326,44 +327,46 @@ def nth_combination(iterable: Iterable[_T], r: int, index: int) -> tuple[_T, ... |
326 | 327 | return tuple(result) |
327 | 328 |
|
328 | 329 |
|
329 | | -if sys.version_info >= (3, 10): |
330 | | - |
331 | | - @overload |
332 | | - def grouper( |
333 | | - iterable: Iterable[_T], n: int, *, incomplete: Literal["fill"] = "fill", fillvalue: None = None |
334 | | - ) -> Iterator[tuple[_T | None, ...]]: ... |
335 | | - |
336 | | - @overload |
337 | | - def grouper( |
338 | | - iterable: Iterable[_T], n: int, *, incomplete: Literal["fill"] = "fill", fillvalue: _T1 |
339 | | - ) -> Iterator[tuple[_T | _T1, ...]]: ... |
340 | | - |
341 | | - @overload |
342 | | - def grouper( |
343 | | - iterable: Iterable[_T], n: int, *, incomplete: Literal["strict", "ignore"], fillvalue: None = None |
344 | | - ) -> Iterator[tuple[_T, ...]]: ... |
345 | | - |
346 | | - def grouper( |
347 | | - iterable: Iterable[object], n: int, *, incomplete: Literal["fill", "strict", "ignore"] = "fill", fillvalue: object = None |
348 | | - ) -> Iterator[tuple[object, ...]]: |
349 | | - "Collect data into non-overlapping fixed-length chunks or blocks" |
350 | | - # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx |
351 | | - # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError |
352 | | - # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF |
353 | | - args = [iter(iterable)] * n |
354 | | - if incomplete == "fill": |
355 | | - return zip_longest(*args, fillvalue=fillvalue) |
356 | | - if incomplete == "strict": |
357 | | - return zip(*args, strict=True) |
358 | | - if incomplete == "ignore": |
359 | | - return zip(*args) |
360 | | - else: |
361 | | - raise ValueError("Expected fill, strict, or ignore") |
362 | | - |
363 | | - def transpose(it: Iterable[Iterable[_T]]) -> Iterator[tuple[_T, ...]]: |
364 | | - "Swap the rows and columns of the input." |
365 | | - # transpose([(1, 2, 3), (11, 22, 33)]) --> (1, 11) (2, 22) (3, 33) |
366 | | - return zip(*it, strict=True) |
| 330 | +@overload |
| 331 | +def grouper( |
| 332 | + iterable: Iterable[_T], n: int, *, incomplete: Literal["fill"] = "fill", fillvalue: None = None |
| 333 | +) -> Iterator[tuple[_T | None, ...]]: ... |
| 334 | + |
| 335 | + |
| 336 | +@overload |
| 337 | +def grouper( |
| 338 | + iterable: Iterable[_T], n: int, *, incomplete: Literal["fill"] = "fill", fillvalue: _T1 |
| 339 | +) -> Iterator[tuple[_T | _T1, ...]]: ... |
| 340 | + |
| 341 | + |
| 342 | +@overload |
| 343 | +def grouper( |
| 344 | + iterable: Iterable[_T], n: int, *, incomplete: Literal["strict", "ignore"], fillvalue: None = None |
| 345 | +) -> Iterator[tuple[_T, ...]]: ... |
| 346 | + |
| 347 | + |
| 348 | +def grouper( |
| 349 | + iterable: Iterable[object], n: int, *, incomplete: Literal["fill", "strict", "ignore"] = "fill", fillvalue: object = None |
| 350 | +) -> Iterator[tuple[object, ...]]: |
| 351 | + "Collect data into non-overlapping fixed-length chunks or blocks" |
| 352 | + # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx |
| 353 | + # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError |
| 354 | + # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF |
| 355 | + args = [iter(iterable)] * n |
| 356 | + if incomplete == "fill": |
| 357 | + return zip_longest(*args, fillvalue=fillvalue) |
| 358 | + if incomplete == "strict": |
| 359 | + return zip(*args, strict=True) |
| 360 | + if incomplete == "ignore": |
| 361 | + return zip(*args) |
| 362 | + else: |
| 363 | + raise ValueError("Expected fill, strict, or ignore") |
| 364 | + |
| 365 | + |
| 366 | +def transpose(it: Iterable[Iterable[_T]]) -> Iterator[tuple[_T, ...]]: |
| 367 | + "Swap the rows and columns of the input." |
| 368 | + # transpose([(1, 2, 3), (11, 22, 33)]) --> (1, 11) (2, 22) (3, 33) |
| 369 | + return zip(*it, strict=True) |
367 | 370 |
|
368 | 371 |
|
369 | 372 | if sys.version_info >= (3, 12): |
|
0 commit comments