update variables.md

This commit is contained in:
Shunsuke Shibayama 2025-12-12 21:27:32 +09:00
parent 09f0756ac1
commit a4585f774b
2 changed files with 9 additions and 10 deletions

View File

@ -107,15 +107,15 @@ 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`. However, anything designated as a generic type alias by `typing.TypeAlias` can be `Unknown`. However, generic PEP 613 type aliases that point to type variables can be explicitly
explicitly specialized. specialized.
```py ```py
from typing import TypeVar, TypeAlias from typing import TypeVar, TypeAlias
T = TypeVar("T") T = TypeVar("T")
BareAnnotated = T ImplicitPositive = T
Annotated: TypeAlias = T Positive: 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"
@ -123,8 +123,8 @@ def _(
# error: [invalid-type-form] "A type variable itself cannot be specialized" # error: [invalid-type-form] "A type variable itself cannot be specialized"
b: T[T], b: T[T],
# error: [invalid-type-form] "A type variable itself cannot be specialized" # error: [invalid-type-form] "A type variable itself cannot be specialized"
c: BareAnnotated[int], c: ImplicitPositive[int],
d: Annotated[int], d: Positive[int],
): ):
reveal_type(a) # revealed: Unknown reveal_type(a) # revealed: Unknown
reveal_type(b) # revealed: Unknown reveal_type(b) # revealed: Unknown

View File

@ -101,18 +101,17 @@ 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`. However, anything designated as a generic type alias by a type statement can be `Unknown`. However, generic type aliases that point to type variables can be explicitly specialized.
explicitly specialized.
```py ```py
type Annotated[T] = T type Positive[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"
a: 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"
b: T[T], b: T[T],
c: Annotated[int], c: Positive[int],
): ):
reveal_type(a) # revealed: Unknown reveal_type(a) # revealed: Unknown
reveal_type(b) # revealed: Unknown reveal_type(b) # revealed: Unknown