Search code examples
pythonpython-3.xtyping

Type annotation for any collection of a particular type


Is there a type annotation to specify "any collection or generator whose items are of a given type" using Python's type annotation system?

I have a function that doesn't care if it gets a list or tuple set or generator or whatever, as long as it is a collection of instances of a particular class.


Solution

  • The combining feature of ordered, unordered and lazy collections is that they are Iterable. As such, an arbitrary "collection of Ts" should be annotated as an Iterable[T].