How do I specify an object that must satisfy several protocols? For instance, suppose I need an object that satisfies both Reversible and Iterable:
from typing import Iterable, Reversible
l = [1, 2, 3]
r: Reversible[int] = l
i: Iterable[int] = l
reversed(r) # fine
iter(i) # fine
reversed(i) # err since not iterable
iter(r) #err since not reversible
I would like to somehow annotate a variable where all operations are possible. For instance (made up syntax):
T = TypeVar('T')
ReversibleIterable = Intersection[Reversible[T], Iterable[T]]
ri: ReversibleIterable[int] = l
reversed(ri) # fine
iter(ri) # fine
Does such a thing exist? I searched for work arounds using protocols, bounded TypeVars, etc. but was unable to make it work.