2828__version__ = "0.0.0+auto.0"
2929__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Itertools.git"
3030
31+ try :
32+ from typing import (
33+ Any ,
34+ Callable ,
35+ Iterable ,
36+ Iterator ,
37+ List ,
38+ Optional ,
39+ Sequence ,
40+ Tuple ,
41+ TypeVar ,
42+ Union ,
43+ )
44+ from typing_extensions import TypeAlias
45+
46+ _T = TypeVar ("_T" )
47+ _Fill = TypeVar ("_Fill" )
48+ _OptionalFill : TypeAlias = Optional [_Fill ]
49+ _N : TypeAlias = Union [int , float , complex ]
50+ _Predicate : TypeAlias = Callable [[_T ], object ]
51+
52+ except ImportError :
53+ pass
54+
3155
3256def accumulate (iterable , func = lambda x , y : x + y ):
3357 """Make an iterator that returns accumulated sums, or accumulated
@@ -52,7 +76,7 @@ def accumulate(iterable, func=lambda x, y: x + y):
5276 yield acc
5377
5478
55- def chain (* iterables ) :
79+ def chain (* iterables : Iterable [ _T ]) -> Iterator [ _T ] :
5680 """Make an iterator that returns elements from the first iterable until it
5781 is exhausted, then proceeds to the next iterable, until all of the iterables
5882 are exhausted. Used for treating consecutive sequences as a single sequence.
@@ -65,7 +89,7 @@ def chain(*iterables):
6589 yield from i
6690
6791
68- def chain_from_iterable (iterables ) :
92+ def chain_from_iterable (iterables : Iterable [ Iterable [ _T ]]) -> Iterator [ _T ] :
6993 """Alternate constructor for chain(). Gets chained inputs from a
7094 single iterable argument that is evaluated lazily.
7195
@@ -78,7 +102,7 @@ def chain_from_iterable(iterables):
78102 yield element
79103
80104
81- def combinations (iterable , r ) :
105+ def combinations (iterable : Iterable [ _T ] , r : int ) -> Iterator [ Tuple [ _T , ...]] :
82106 """Return r length subsequences of elements from the input iterable.
83107 Combinations are emitted in lexicographic sort order. So, if the input
84108 iterable is sorted, the combination tuples will be produced in sorted order.
@@ -113,7 +137,9 @@ def combinations(iterable, r):
113137 yield tuple (pool [i ] for i in indices )
114138
115139
116- def combinations_with_replacement (iterable , r ):
140+ def combinations_with_replacement (
141+ iterable : Iterable [_T ], r : int
142+ ) -> Iterator [Tuple [_T , ...]]:
117143 """Return r length subsequences of elements from the input iterable allowing
118144 individual elements to be repeated more than once.
119145
@@ -147,7 +173,7 @@ def combinations_with_replacement(iterable, r):
147173 yield tuple (pool [i ] for i in indices )
148174
149175
150- def compress (data , selectors ) :
176+ def compress (data : Iterable [ _T ] , selectors : Iterable [ Any ]) -> Iterable [ _T ] :
151177 """Make an iterator that filters elements from data returning only those
152178 that have a corresponding element in selectors that evaluates to True.
153179 Stops when either the data or selectors iterables has been exhausted.
@@ -160,7 +186,7 @@ def compress(data, selectors):
160186 return (d for d , s in zip (data , selectors ) if s )
161187
162188
163- def count (start = 0 , step = 1 ) :
189+ def count (start : _N = 0 , step : _N = 1 ) -> Iterator [ _N ] :
164190 """Make an iterator that returns evenly spaced values starting with number
165191 start. Often used as an argument to map() to generate consecutive data
166192 points. Also, used with zip() to add sequence numbers.
@@ -196,7 +222,7 @@ def cycle(p):
196222 yield from p
197223
198224
199- def dropwhile (predicate , iterable ) :
225+ def dropwhile (predicate : _Predicate [ _T ] , iterable : Iterable [ _T ]) -> Iterator [ _T ] :
200226 """Make an iterator that drops elements from the iterable as long as the
201227 predicate is true; afterwards, returns every element. Note, the iterator
202228 does not produce any output until the predicate first becomes false, so it
@@ -216,7 +242,7 @@ def dropwhile(predicate, iterable):
216242 yield x
217243
218244
219- def filterfalse (predicate , iterable ) :
245+ def filterfalse (predicate : _Predicate [ _T ] , iterable : Iterable [ _T ]) -> Iterator [ _T ] :
220246 """Make an iterator that filters elements from iterable returning only those
221247 for which the predicate is False. If predicate is None, return the items
222248 that are false.
@@ -332,7 +358,9 @@ def islice(p, start, stop=(), step=1):
332358 return
333359
334360
335- def permutations (iterable , r = None ):
361+ def permutations (
362+ iterable : Iterable [_T ], r : Optional [int ] = None
363+ ) -> Iterator [Tuple [_T , ...]]:
336364 """Return successive r length permutations of elements in the iterable.
337365
338366 If r is not specified or is None, then r defaults to the length of the
@@ -375,7 +403,7 @@ def permutations(iterable, r=None):
375403 return
376404
377405
378- def product (* args , r = 1 ) :
406+ def product (* args : Iterable [ _T ] , r : int = 1 ) -> Iterator [ Tuple [ _T , ...]] :
379407 """Cartesian product of input iterables.
380408
381409 Roughly equivalent to nested for-loops in a generator expression. For
@@ -399,14 +427,14 @@ def product(*args, r=1):
399427 # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
400428 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
401429 pools = [tuple (pool ) for pool in args ] * r
402- result = [[]]
430+ result : List [ List [ _T ]] = [[]]
403431 for pool in pools :
404432 result = [x + [y ] for x in result for y in pool ]
405433 for prod in result :
406434 yield tuple (prod )
407435
408436
409- def repeat (el , n = None ):
437+ def repeat (el : _T , n : Optional [ int ] = None ) -> Iterator [ _T ] :
410438 """Make an iterator that returns object over and over again. Runs
411439 indefinitely unless the times argument is specified. Used as argument to
412440 map() for invariant parameters to the called function. Also used with zip()
@@ -424,7 +452,9 @@ def repeat(el, n=None):
424452 yield el
425453
426454
427- def starmap (function , iterable ):
455+ def starmap (
456+ function : Callable [..., _T ], iterable : Iterable [Iterable [Any ]]
457+ ) -> Iterator [_T ]:
428458 """Make an iterator that computes the function using arguments obtained from
429459 the iterable. Used instead of map() when argument parameters are already
430460 grouped in tuples from a single iterable (the data has been “pre-zipped”).
@@ -439,7 +469,7 @@ def starmap(function, iterable):
439469 yield function (* args )
440470
441471
442- def takewhile (predicate , iterable ) :
472+ def takewhile (predicate : _Predicate [ _T ] , iterable : Iterable [ _T ]) -> Iterator [ _T ] :
443473 """Make an iterator that returns elements from the iterable as long
444474 as the predicate is true.
445475
@@ -455,7 +485,7 @@ def takewhile(predicate, iterable):
455485 break
456486
457487
458- def tee (iterable , n = 2 ) :
488+ def tee (iterable : Iterable [ _T ] , n : int = 2 ) -> Sequence [ Iterator [ _T ]] :
459489 """Return n independent iterators from a single iterable.
460490
461491 :param iterable: the iterator from which to make iterators.
@@ -465,7 +495,9 @@ def tee(iterable, n=2):
465495 return [iter (iterable ) for _ in range (n )]
466496
467497
468- def zip_longest (* args , fillvalue = None ):
498+ def zip_longest (
499+ * args : Iterable [_T ], fillvalue : _OptionalFill = None
500+ ) -> Iterator [Tuple [Union [_T , _OptionalFill ], ...]]:
469501 """Make an iterator that aggregates elements from each of the
470502 iterables. If the iterables are of uneven length, missing values are
471503 filled-in with fillvalue. Iteration continues until the longest
@@ -475,7 +507,7 @@ def zip_longest(*args, fillvalue=None):
475507 :param fillvalue: value to fill in those missing from shorter iterables
476508 """
477509 # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
478- iterators = [iter (it ) for it in args ]
510+ iterators : List [ Iterator [ Union [ _T , _OptionalFill ]]] = [iter (it ) for it in args ]
479511 num_active = len (iterators )
480512 if not num_active :
481513 return
0 commit comments