I just want to mention one more example here, which requires Union and Intersection, which is when the number of things intersected is variable. This can happen when TypeVarTuple are involved, and it kind of implies that a speccing of empty unions and empty intersections is necessary. (See https://github.com/python/typing/issues/1362)
from typing import TypeVarTuple, Union
items = TypeVarTuple("items")
protocols = TypeVarTuple("protocols") # something like tuple[type[Protocol], ...]
def pick_random_element(tup: tuple[*items]) -> Union[*items]: ...
"""Return random element form a tuple."""
# if tuple is empty, raises an exception ⟺ return `Never`.
def create_intersection_protocol(tuple: tuple[*protocols]) -> type[Intersection[*Protocols]]: ...
"""Creates the intersection of pairwise disjoint protocol classes."""
# if intersection is empty, returns a protocol that matches with any class.
EMPTY_TUPLE = ()
pick_random_element(EMPTY_TUPLE) # should reveal as `Never`.
create_intersection_protocol(EMPTY_TUPLE) # should reveal as `object`!?
Note that bounds are (not yet) supperted by TypeVarTuple, so the latter cannot be hinted properly right now.
I just want to mention one more example here, which requires
Union
andIntersection
, which is when the number of things intersected is variable. This can happen whenTypeVarTuple
are involved, and it kind of implies that a speccing of empty unions and empty intersections is necessary. (See https://github.com/python/typing/issues/1362)Note that bounds are (not yet) supperted by
TypeVarTuple
, so the latter cannot be hinted properly right now._Originally posted by @randolf-scholz in https://github.com/CarliJoy/intersection_examples/issues/12#issuecomment-1665869505_