Skip to content

Bounds and constraints of type variables cannot be generic #1839

@mtshiba

Description

@mtshiba

Summary

In the current Python type specification, bounds and constraints for type variables must be concrete types and cannot contain type variables (even though it's possible at runtime).

peps.python.org/pep-0695#type-parameter-scopes

So, all of the following cases should be reported as errors:

from typing import TypeVar

# OK
T1 = TypeVar("T1", bound=list[int])
T2 = TypeVar("T2", list[int], str)

U = TypeVar("U")
# error
T3 = TypeVar("T3", bound=U)
# error
T4 = TypeVar("T4", bound=list[U])
# error
T5 = TypeVar("T5", bound=list["T5"])
# error
T6 = TypeVar("T6", U, str)
# error
T7 = TypeVar("T7", list["T7"], str)
# OK
def _[T: list[int]](): ...
def _[T: (list[int], str)](): ...

# error
def _[U, T: U](): ...
# error
def _[U, T: list[U]](): ...
# error
def _[T: list[T]](): ...
# error
def _[U, T: (U, str)](): ...
# error
def _[T: (list[T], str)](): ...

However, this is difficult to implement straightforwardly in the current implementation of ty, because bounds/constraints on type variables are lazily evaluated.

Version

No response

Metadata

Metadata

Assignees

Labels

genericsBugs or features relating to ty's generics implementation

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions