diff --git a/peps/pep-0696.rst b/peps/pep-0696.rst index 79ccf5b6639..82361239465 100644 --- a/peps/pep-0696.rst +++ b/peps/pep-0696.rst @@ -20,7 +20,9 @@ Abstract This PEP introduces the concept of type defaults for type parameters, including ``TypeVar``, ``ParamSpec``, and ``TypeVarTuple``, -which act as defaults for type parameters for which no type is specified. +which act as defaults for type parameters for which no type arguments +are specified explicitly with `'[]'`, or implicitly without `'[]'` +but with the type inference by an assigned value. Default type argument support is available in some popular languages such as C++, TypeScript, and Rust. A survey of type parameter syntax in @@ -34,15 +36,27 @@ Motivation :: - T = TypeVar("T", default=int) # This means that if no type is specified T = int - + T = TypeVar("T", default=int) # This means that T = int if no type argument is specified explicitly + # with `'[]'`, or implicitly without `'[]'` but with the type + # inference by an assigned value. @dataclass class Box(Generic[T]): value: T | None = None - reveal_type(Box()) # type is Box[int] + # The type argument isn't specified. + reveal_type(Box()) # type is Box[int] + + # The type argument is specified with '[]'. + reveal_type(Box[str]()) # type is Box[str] + + # The type argument is specified with + # the type inference by an assigned value. reveal_type(Box(value="Hello World!")) # type is Box[str] + # The type argument is specified with '[]' but not with the type + # inference by an assigned value because '[]' is prioritized. + reveal_type(Box[str](value="Hello World!")) # type is Box[str] + One place this `regularly comes up `__ is ``Generator``. I propose changing the *stub definition* to something like::