Skip to content

Can't have an alias that just targets a type variable #543

@eric-wieser

Description

@eric-wieser

Consider version 1 of my package, which defines

T = TypeVar('T', int, float)
MyType = Union[T, List[T]]

# in the user's code
U = TypeVar('U', int, float)
def myfunc(MyType[U], U): ...

In version 2, I decide to drop support for passing List[T]. However, this causes an error on the downstream type definitions

T = TypeVar('T', int, float)
MyType = Union[T]  # or Generic[T], or T - all fail

# downstream code doesn't change - I want the freedom to change `MyType` again in future
U = TypeVar('U', int, float)
def myfunc(MyType[U], U): ...  # fails due to missing __getitem__

Is there a way to write this? Does implementing TypeVar.__getitem__ seem reasonable? Or should we introduce Identity[T], which boxes a typevar into a generic?

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions