add TODO comment about PEP 613 type aliases

This commit is contained in:
Shunsuke Shibayama 2025-12-12 19:03:25 +09:00
parent b6da8547d9
commit cdec0f9421
2 changed files with 27 additions and 11 deletions

View File

@ -107,21 +107,32 @@ reveal_type(S) # revealed: TypeVar
### No explicit specialization ### No explicit specialization
A type variable itself cannot be explicitly specialized; the result of the specialization is A type variable itself cannot be explicitly specialized; the result of the specialization is
`Unknown`. `Unknown`. However, anything designated as a generic type alias by `typing.TypeAlias` can be
explicitly specialized.
```py ```py
from typing import TypeVar from typing import TypeVar, TypeAlias
T = TypeVar("T") T = TypeVar("T")
BareAnnotated = T
Annotated: TypeAlias = T
def _( def _(
# error: [invalid-type-form] "A type variable itself cannot be specialized" # error: [invalid-type-form] "A type variable itself cannot be specialized"
x: T[int], a: T[int],
# error: [invalid-type-form] "A type variable itself cannot be specialized" # error: [invalid-type-form] "A type variable itself cannot be specialized"
y: T[T], b: T[T],
# error: [invalid-type-form] "A type variable itself cannot be specialized"
c: BareAnnotated[int],
# TODO: no error
# error: [invalid-type-form]
d: Annotated[int],
): ):
reveal_type(x) # revealed: Unknown reveal_type(a) # revealed: Unknown
reveal_type(y) # revealed: Unknown reveal_type(b) # revealed: Unknown
reveal_type(c) # revealed: Unknown
# TODO: should be `int`
reveal_type(d) # revealed: Unknown
``` ```
### Type variables with a default ### Type variables with a default

View File

@ -101,17 +101,22 @@ def f[T: (int,)]():
### No explicit specialization ### No explicit specialization
A type variable itself cannot be explicitly specialized; the result of the specialization is A type variable itself cannot be explicitly specialized; the result of the specialization is
`Unknown`. `Unknown`. However, anything designated as a generic type alias by a type statement can be
explicitly specialized.
```py ```py
type Annotated[T] = T
def _[T]( def _[T](
# error: [invalid-type-form] "A type variable itself cannot be specialized" # error: [invalid-type-form] "A type variable itself cannot be specialized"
x: T[int], a: T[int],
# error: [invalid-type-form] "A type variable itself cannot be specialized" # error: [invalid-type-form] "A type variable itself cannot be specialized"
y: T[T], b: T[T],
c: Annotated[int],
): ):
reveal_type(x) # revealed: Unknown reveal_type(a) # revealed: Unknown
reveal_type(y) # revealed: Unknown reveal_type(b) # revealed: Unknown
reveal_type(c) # revealed: int
``` ```
## Invalid uses ## Invalid uses