Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions peps/pep-0696.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <https://github.com/python/typing/issues/975>`__ is ``Generator``. I
propose changing the *stub definition* to something like::
Expand Down
Loading