53
53
pass
54
54
55
55
56
- def accumulate (iterable , func = lambda x , y : x + y ):
56
+ def accumulate (
57
+ iterable : Iterable [_T ],
58
+ func : Callable [[_T , _T ], _T ] = lambda x , y : x + y , # type: ignore[operator]
59
+ ) -> Iterator [_T ]:
57
60
"""Make an iterator that returns accumulated sums, or accumulated
58
61
results of other binary functions (specified via the optional func
59
62
argument). If func is supplied, it should be a function of two
@@ -200,7 +203,7 @@ def count(start: _N = 0, step: _N = 1) -> Iterator[_N]:
200
203
start += step
201
204
202
205
203
- def cycle (p ) :
206
+ def cycle (p : Iterable [ _T ]) -> Iterator [ _T ] :
204
207
"""Make an iterator returning elements from the iterable and saving a copy
205
208
of each. When the iterable is exhausted, return elements from the saved
206
209
copy. Repeats indefinitely.
@@ -209,7 +212,7 @@ def cycle(p):
209
212
210
213
"""
211
214
try :
212
- len (p )
215
+ len (p ) # type: ignore[arg-type]
213
216
except TypeError :
214
217
# len() is not defined for this type. Assume it is
215
218
# a finite iterable so we must cache the elements.
@@ -242,7 +245,9 @@ def dropwhile(predicate: _Predicate[_T], iterable: Iterable[_T]) -> Iterator[_T]
242
245
yield x
243
246
244
247
245
- def filterfalse (predicate : _Predicate [_T ], iterable : Iterable [_T ]) -> Iterator [_T ]:
248
+ def filterfalse (
249
+ predicate : Optional [_Predicate [_T ]], iterable : Iterable [_T ]
250
+ ) -> Iterator [_T ]:
246
251
"""Make an iterator that filters elements from iterable returning only those
247
252
for which the predicate is False. If predicate is None, return the items
248
253
that are false.
@@ -288,23 +293,29 @@ class groupby:
288
293
# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
289
294
# [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
290
295
291
- def __init__ (self , iterable , key = None ):
296
+ def __init__ (
297
+ self ,
298
+ iterable : Iterable [_T ],
299
+ key : Optional [Callable [[_T ], Any ]] = None ,
300
+ ):
292
301
self .keyfunc = key if key is not None else lambda x : x
293
302
self .it = iter (iterable )
294
- self .tgtkey = self .currkey = self .currvalue = object ()
303
+ # Sentinel values, not actually returned during iteration.
304
+ self .currvalue : _T = object () # type: ignore[assignment]
305
+ self .tgtkey = self .currkey = self .currvalue
295
306
296
- def __iter__ (self ):
307
+ def __iter__ (self ) -> Iterator [ Tuple [ Any , Iterator [ _T ]]] :
297
308
return self
298
309
299
- def __next__ (self ):
310
+ def __next__ (self ) -> Tuple [ Any , Iterator [ _T ]] :
300
311
self .id = object ()
301
312
while self .currkey == self .tgtkey :
302
313
self .currvalue = next (self .it ) # Exit on StopIteration
303
314
self .currkey = self .keyfunc (self .currvalue )
304
315
self .tgtkey = self .currkey
305
316
return (self .currkey , self ._grouper (self .tgtkey , self .id ))
306
317
307
- def _grouper (self , tgtkey , id ) :
318
+ def _grouper (self , tgtkey : Any , id : object ) -> Iterator [ _T ] :
308
319
while self .id is id and self .currkey == tgtkey :
309
320
yield self .currvalue
310
321
try :
@@ -314,7 +325,12 @@ def _grouper(self, tgtkey, id):
314
325
self .currkey = self .keyfunc (self .currvalue )
315
326
316
327
317
- def islice (p , start , stop = (), step = 1 ):
328
+ def islice (
329
+ p : Iterable [_T ],
330
+ start : int ,
331
+ stop : Optional [int ] = (), # type: ignore[assignment]
332
+ step : int = 1 ,
333
+ ) -> Iterator [_T ]:
318
334
"""Make an iterator that returns selected elements from the
319
335
iterable. If start is non-zero and stop is unspecified, then the
320
336
value for start is used as end, and start is taken to be 0. Thus the
@@ -420,7 +436,8 @@ def permutations(
420
436
return
421
437
422
438
423
- def product (* args : Iterable [_T ], r : int = 1 ) -> Iterator [Tuple [_T , ...]]:
439
+ # def product(*args: Iterable[_T], r: int = 1) -> Iterator[Tuple[_T, ...]]:
440
+ def product (* args : Iterable [Any ], r : int = 1 ) -> Iterator [Tuple [Any , ...]]:
424
441
"""Cartesian product of input iterables.
425
442
426
443
Roughly equivalent to nested for-loops in a generator expression. For
@@ -444,7 +461,7 @@ def product(*args: Iterable[_T], r: int = 1) -> Iterator[Tuple[_T, ...]]:
444
461
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
445
462
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
446
463
pools = [tuple (pool ) for pool in args ] * r
447
- result : List [List [_T ]] = [[]]
464
+ result : List [List [Any ]] = [[]]
448
465
for pool in pools :
449
466
result = [x + [y ] for x in result for y in pool ]
450
467
for prod in result :
@@ -513,8 +530,8 @@ def tee(iterable: Iterable[_T], n: int = 2) -> Sequence[Iterator[_T]]:
513
530
514
531
515
532
def zip_longest (
516
- * args : Iterable [_T ], fillvalue : _OptionalFill = None
517
- ) -> Iterator [Tuple [Union [ _T , _OptionalFill ] , ...]]:
533
+ * args : Iterable [Any ], fillvalue : _OptionalFill = None
534
+ ) -> Iterator [Tuple [Any , ...]]:
518
535
"""Make an iterator that aggregates elements from each of the
519
536
iterables. If the iterables are of uneven length, missing values are
520
537
filled-in with fillvalue. Iteration continues until the longest
@@ -524,7 +541,7 @@ def zip_longest(
524
541
:param fillvalue: value to fill in those missing from shorter iterables
525
542
"""
526
543
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
527
- iterators : List [Iterator [Union [ _T , _OptionalFill ] ]] = [iter (it ) for it in args ]
544
+ iterators : List [Iterator [Any ]] = [iter (it ) for it in args ]
528
545
num_active = len (iterators )
529
546
if not num_active :
530
547
return
0 commit comments