Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-116126: Implement PEP 696#116129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
gh-116126: Implement PEP 696#116129
Changes from43 commits
e7ca092828679ed3b472b6e467eefa4858049b077b5cfd5e201b32707a69204a2650080c282978aa5b27bdd01bb2c28b9ab0d467fd7e91e235226b42e079eee0370527b15f1111bd102c54aeed71a348b900cd40c13420b8a08c62613159f4e0435e7d54ace12581d82b5a102a2c48c929b9435326de176f667757efa4ce29ad8432c04c147d4f3fd1a143675890d7919a7b48ef0616b9d4e8422686b9798d1decfbb6405e0ccfeb15aaff986f5aed3e0c0fcf5a3d4b92ea1085f6fdfdb3f053c8c3e0b4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1798,23 +1798,25 @@ Type parameters | ||
| :ref:`Type parameters <type-params>` can exist on classes, functions, and type | ||
| aliases. | ||
| .. class:: TypeVar(name, bound, default_value) | ||
| A :class:`typing.TypeVar`. *name* is the name of the type variable. | ||
| *bound* is the bound or constraints, if any. If *bound* is a :class:`Tuple`, | ||
| it represents constraints; otherwise it represents the bound. *default_value* | ||
| is the default value, if any. | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. doctest:: | ||
| >>> print(ast.dump(ast.parse("type Alias[T: int = bool] = list[T]"), indent=4)) | ||
| Module( | ||
| body=[ | ||
| TypeAlias( | ||
| name=Name(id='Alias', ctx=Store()), | ||
| type_params=[ | ||
| TypeVar( | ||
| name='T', | ||
| bound=Name(id='int', ctx=Load()), | ||
| default_value=Name(id='bool', ctx=Load()))], | ||
| value=Subscript( | ||
| value=Name(id='list', ctx=Load()), | ||
| slice=Name(id='T', ctx=Load()), | ||
| @@ -1823,19 +1825,29 @@ aliases. | ||
| .. versionadded:: 3.12 | ||
| .. versionchanged:: 3.13 | ||
| Added the *default_value* parameter. | ||
| .. class:: ParamSpec(name, default_value) | ||
| A :class:`typing.ParamSpec`. *name* is the name of the parameter specification. | ||
| *default_value* is the default value, if any. | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. doctest:: | ||
| >>> print(ast.dump(ast.parse("type Alias[**P = (int, str)] = Callable[P, int]"), indent=4)) | ||
| Module( | ||
| body=[ | ||
| TypeAlias( | ||
| name=Name(id='Alias', ctx=Store()), | ||
| type_params=[ | ||
| ParamSpec( | ||
| name='P', | ||
| default_value=Tuple( | ||
| elts=[ | ||
| Name(id='int', ctx=Load()), | ||
| Name(id='str', ctx=Load())], | ||
| ctx=Load()))], | ||
| value=Subscript( | ||
| value=Name(id='Callable', ctx=Load()), | ||
| slice=Tuple( | ||
| @@ -1848,19 +1860,25 @@ aliases. | ||
| .. versionadded:: 3.12 | ||
| .. versionchanged:: 3.13 | ||
| Added the *default_value* parameter. | ||
| .. class:: TypeVarTuple(name, default_value) | ||
| A :class:`typing.TypeVarTuple`. *name* is the name of the type variable tuple. | ||
| *default_value* is the default value, if any. | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| .. doctest:: | ||
| >>> print(ast.dump(ast.parse("type Alias[*Ts = ()] = tuple[*Ts]"), indent=4)) | ||
| Module( | ||
| body=[ | ||
| TypeAlias( | ||
| name=Name(id='Alias', ctx=Store()), | ||
| type_params=[ | ||
| TypeVarTuple( | ||
| name='Ts', | ||
| default_value=Tuple(elts=[], ctx=Load()))], | ||
| value=Subscript( | ||
| value=Name(id='tuple', ctx=Load()), | ||
| slice=Tuple( | ||
| @@ -1874,6 +1892,9 @@ aliases. | ||
| .. versionadded:: 3.12 | ||
| .. versionchanged:: 3.13 | ||
| Added the *default_value* parameter. | ||
| Function and class definitions | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1616,7 +1616,7 @@ without the dedicated syntax, as documented below. | ||
| .. _typevar: | ||
| .. class:: TypeVar(name, *constraints, bound=None, covariant=False, contravariant=False, infer_variance=False, default=<unrepresentable>) | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Type variable. | ||
| @@ -1754,15 +1754,27 @@ without the dedicated syntax, as documented below. | ||
| the constraints are evaluated only when the attribute is accessed, not when | ||
| the type variable is created (see :ref:`lazy-evaluation`). | ||
| .. attribute:: __default__ | ||
| The default value of the type variable, if any. This attribute is ``None`` | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page.
AlexWaygood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| if the type variable has no default, and :class:`types.NoneType` | ||
| if the default is set to ``None``. | ||
| .. versionadded:: 3.13 | ||
| .. versionchanged:: 3.12 | ||
| Type variables can now be declared using the | ||
| :ref:`type parameter <type-params>` syntax introduced by :pep:`695`. | ||
| The ``infer_variance`` parameter was added. | ||
| .. versionchanged:: 3.13 | ||
| Support for default values was added. | ||
| .. _typevartuple: | ||
| .. class:: TypeVarTuple(name, default=<unrepresentable>) | ||
| Type variable tuple. A specialized form of :ref:`type variable <typevar>` | ||
| that enables *variadic* generics. | ||
| @@ -1872,14 +1884,26 @@ without the dedicated syntax, as documented below. | ||
| The name of the type variable tuple. | ||
| .. attribute:: __default__ | ||
| The default value of the type variable tuple, if any. This attribute is ``None`` | ||
| if the type variable tuple has no default, and :class:`types.NoneType` | ||
| if the default is set to ``None``. | ||
| .. versionadded:: 3.13 | ||
| .. versionadded:: 3.11 | ||
| .. versionchanged:: 3.12 | ||
| Type variable tuples can now be declared using the | ||
| :ref:`type parameter <type-params>` syntax introduced by :pep:`695`. | ||
| .. versionchanged:: 3.13 | ||
| Support for default values was added. | ||
| .. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False, default=<unrepresentable>) | ||
| Parameter specification variable. A specialized version of | ||
| :ref:`type variables <typevar>`. | ||
| @@ -1948,6 +1972,14 @@ without the dedicated syntax, as documented below. | ||
| The name of the parameter specification. | ||
| .. attribute:: __default__ | ||
| The default value of the parameter specification, if any. This attribute is ``None`` | ||
| if the parameter specification has no default, and :class:`types.NoneType` | ||
| if the default is set to ``None``. | ||
| .. versionadded:: 3.13 | ||
| Parameter specification variables created with ``covariant=True`` or | ||
| ``contravariant=True`` can be used to declare covariant or contravariant | ||
| generic types. The ``bound`` argument is also accepted, similar to | ||
| @@ -1961,6 +1993,10 @@ without the dedicated syntax, as documented below. | ||
| Parameter specifications can now be declared using the | ||
| :ref:`type parameter <type-params>` syntax introduced by :pep:`695`. | ||
| .. versionchanged:: 3.13 | ||
| Support for default values was added. | ||
| .. note:: | ||
| Only parameter specification variables defined in global scope can | ||
| be pickled. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1620,15 +1620,18 @@ Type parameter lists | ||
| .. versionadded:: 3.12 | ||
| .. versionchanged:: 3.13 | ||
| Support for default values was added (see :pep:`696`). | ||
| .. index:: | ||
| single: type parameters | ||
| .. productionlist:: python-grammar | ||
| type_params: "[" `type_param` ("," `type_param`)* "]" | ||
| type_param: `typevar` | `typevartuple` | `paramspec` | ||
| typevar: `identifier` (":" `expression`)? ("=" `expression`)? | ||
| typevartuple: "*" `identifier` ("=" `expression`)? | ||
| paramspec: "**" `identifier` ("=" `expression`)? | ||
| :ref:`Functions <def>` (including :ref:`coroutines <async def>`), | ||
| :ref:`classes <class>` and :ref:`type aliases <type>` may | ||
| @@ -1694,19 +1697,29 @@ evaluated in a separate :ref:`annotation scope <annotation-scopes>`. | ||
| :data:`typing.TypeVarTuple`\ s and :data:`typing.ParamSpec`\ s cannot have bounds | ||
| or constraints. | ||
| All three flavors of type parameters can also have a *default value*, which is used | ||
| when the type parameter is not explicitly provided. This is added by appending | ||
| a single equals sign (``=``) followed by an expression. Like the bounds and | ||
| constraints of type variables, the default value is not evaluated when the | ||
| object is created, but only when the type parameter's ``__default__`` attribute | ||
| is accessed. To this end, the default value is evaluated in a separate | ||
| :ref:`annotation scope <annotation-scopes>`. | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| The following example indicates the full set of allowed type parameter declarations:: | ||
| def overly_generic[ | ||
| SimpleTypeVar, | ||
| TypeVarWithDefault = int, | ||
| TypeVarWithBound: int, | ||
| TypeVarWithConstraints: (str, bytes), | ||
| *SimpleTypeVarTuple = (int, float), | ||
| **SimpleParamSpec = (str, bytearray), | ||
| ]( | ||
| a: SimpleTypeVar, | ||
| b: TypeVarWithDefault, | ||
| c: TypeVarWithBound, | ||
| d: Callable[SimpleParamSpec, TypeVarWithConstraints], | ||
| *e: SimpleTypeVarTuple, | ||
| ): ... | ||
| .. _generic-functions: | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -205,7 +205,7 @@ Annotation scopes are used in the following contexts: | ||
| * Type parameter lists for :ref:`generic classes <generic-classes>`. | ||
| A generic class's base classes and | ||
| keyword arguments are executed within the annotation scope, but its decorators are not. | ||
| * The bounds, constraints,anddefault valuesfor typeparameters | ||
| (:ref:`lazily evaluated <lazy-evaluation>`). | ||
| * The value of type aliases (:ref:`lazily evaluated <lazy-evaluation>`). | ||
| @@ -232,13 +232,17 @@ Annotation scopes differ from function scopes in the following ways: | ||
| .. versionadded:: 3.12 | ||
| Annotation scopes were introduced in Python 3.12 as part of :pep:`695`. | ||
| .. versionchanged:: 3.13 | ||
| Annotation scopes are now also used for type parameter defaults, as | ||
JelleZijlstra marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| introduced by :pep:`696`. | ||
| .. _lazy-evaluation: | ||
| Lazy evaluation | ||
| --------------- | ||
| The values of type aliases created through the :keyword:`type` statement are | ||
| *lazily evaluated*. The same applies to the bounds, constraints,anddefault values of type | ||
| variables created through the :ref:`type parameter syntax <type-params>`. | ||
| This means that they are not evaluated when the type alias or type variable is | ||
| created. Instead, they are only evaluated when doing so is necessary to resolve | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -89,6 +89,8 @@ Interpreter improvements: | ||
| New typing features: | ||
| * :pep:`696`: Type parameters (:data:`typing.TypeVar`, :data:`typing.ParamSpec`, | ||
| and :data:`typing.TypeVarTuple`) now support defaults. | ||
Comment on lines +92 to +93 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This feels like a very short description of the changes, considering that it involves a change to CPython's grammar 😃 but we can add a more expansive description later; this is non-blocking | ||
| * :pep:`742`: :data:`typing.TypeIs` was added, providing more intuitive | ||
| type narrowing behavior. | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.