Search code examples
pythontypingmypy

Iterable without side effect type annotation


I'm looking for Iterable which is able to be iterated without side effect. For example, I want to annotate the below args x:

def foo(x: NoSideEffectIterable[int]):
    for i in range(10):
        for xx in x: # x is iterated 10 times here
            print(i*xx)
  1. Iterable is not suited because x is iterated more than once
  2. Sequence is not suited because the order of x doesn't matter

How to annotate x?


Solution

  • There's no annotation for that. It's not a concept the type system can express. Iterable is your best bet. If you want to get a bit more restrictive, you can use Collection, but while Iterable expresses less requirements than you want, Collection expresses more.