Skip to content

Support variable intersections #21

@NeilGirdhar

Description

@NeilGirdhar

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 python/typing#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.

Originally posted by @randolf-scholz in #12 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions