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 from1 commit
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
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| 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=None) | ||
AlexWaygood marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| Type variable. | ||
| @@ -1754,15 +1754,25 @@ 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. | ||
| .. 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=None) | ||
| Type variable tuple. A specialized form of :ref:`type variable <typevar>` | ||
| that enables *variadic* generics. | ||
| @@ -1872,14 +1882,24 @@ without the dedicated syntax, as documented below. | ||
| The name of the type variable tuple. | ||
| .. attribute:: __default__ | ||
| The default value of the type variable, if any. | ||
| .. 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=None) | ||
| Parameter specification variable. A specialized version of | ||
| :ref:`type variables <typevar>`. | ||
| @@ -1948,6 +1968,12 @@ without the dedicated syntax, as documented below. | ||
| The name of the parameter specification. | ||
| .. attribute:: __default__ | ||
| The default value of the type variable, if any. | ||
| .. 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 +1987,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 |
|---|---|---|
| @@ -204,7 +204,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>`). | ||
| @@ -231,13 +231,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 | ||