-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-116126: Implement PEP 696 #116129
Changes from 50 commits
e7ca092
828679e
d3b472b
6e467ee
fa48580
49b077b
5cfd5e2
01b3270
7a69204
a265008
0c28297
8aa5b27
bdd01bb
2c28b9a
b0d467f
d7e91e2
35226b4
2e079ee
e037052
7b15f11
11bd102
c54aeed
71a348b
900cd40
c13420b
8a08c62
613159f
4e0435e
7d54ace
12581d8
2b5a102
a2c48c9
29b9435
326de17
6f66775
7efa4ce
29ad843
2c04c14
7d4f3fd
1a14367
5890d79
19a7b48
ef0616b
9d4e842
2686b97
98d1dec
fbb6405
e0ccfeb
15aaff9
86f5aed
3e0c0fc
f5a3d4b
92ea108
5f6fdfd
b3f053c
8c3e0b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
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) | ||||||||||||||||||||||||||||||
.. class:: TypeVar(name, *constraints, bound=None, covariant=False, contravariant=False, infer_variance=False, default=typing.NoDefault) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Type variable. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
@@ -1754,15 +1754,35 @@ 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, or :data:`typing.NoDefault` if it | ||||||||||||||||||||||||||||||
has no default. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. versionadded:: 3.13 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. method:: has_default() | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Return whether or not the type variable has a default value. This is equivalent | ||||||||||||||||||||||||||||||
to checking whether :attr:`__default__` is not equal to :data:`typing.NoDefault`, | ||||||||||||||||||||||||||||||
except that it does not force evaluation of the | ||||||||||||||||||||||||||||||
:ref:`lazily evaluated <lazy-evaluation>` default value. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. 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) | ||||||||||||||||||||||||||||||
.. class:: TypeVarTuple(name, default=typing.NoDefault) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Type variable tuple. A specialized form of :ref:`type variable <typevar>` | ||||||||||||||||||||||||||||||
that enables *variadic* generics. | ||||||||||||||||||||||||||||||
|
@@ -1872,14 +1892,34 @@ without the dedicated syntax, as documented below. | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
The name of the type variable tuple. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. attribute:: __default__ | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
The default value of the type variable tuple, or :data:`typing.NoDefault` if it | ||||||||||||||||||||||||||||||
has no default. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. versionadded:: 3.13 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. method:: has_default() | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Return whether or not the type variable tuple has a default value. This is equivalent | ||||||||||||||||||||||||||||||
to checking whether :attr:`__default__` is not equal to :data:`typing.NoDefault`, | ||||||||||||||||||||||||||||||
except that it does not force evaluation of the | ||||||||||||||||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
:ref:`lazily evaluated <lazy-evaluation>` default value. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. 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`. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False) | ||||||||||||||||||||||||||||||
.. versionchanged:: 3.13 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Support for default values was added. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False, default=typing.NoDefault) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Parameter specification variable. A specialized version of | ||||||||||||||||||||||||||||||
:ref:`type variables <typevar>`. | ||||||||||||||||||||||||||||||
|
@@ -1948,6 +1988,22 @@ without the dedicated syntax, as documented below. | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
The name of the parameter specification. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. attribute:: __default__ | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
The default value of the parameter specification, or :data:`typing.NoDefault` if it | ||||||||||||||||||||||||||||||
has no default. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. versionadded:: 3.13 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. method:: has_default() | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Return whether or not the parameter specification has a default value. This is equivalent | ||||||||||||||||||||||||||||||
to checking whether :attr:`__default__` is not equal to :data:`typing.NoDefault`, | ||||||||||||||||||||||||||||||
except that it does not force evaluation of the | ||||||||||||||||||||||||||||||
JelleZijlstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
:ref:`lazily evaluated <lazy-evaluation>` default value. | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. 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 +2017,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. | ||||||||||||||||||||||||||||||
|
@@ -3173,6 +3233,22 @@ Introspection helpers | |||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. versionadded:: 3.7.4 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. data:: NoDefault | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
A sentinel object used to indicate that a type parameter has no default | ||||||||||||||||||||||||||||||
value. For example: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. doctest:: | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
>>> T = TypeVar("T") | ||||||||||||||||||||||||||||||
>>> T.__default__ is typing.NoDefault | ||||||||||||||||||||||||||||||
True | ||||||||||||||||||||||||||||||
>>> S = TypeVar("S", default=None) | ||||||||||||||||||||||||||||||
>>> S.__default__ is None | ||||||||||||||||||||||||||||||
True | ||||||||||||||||||||||||||||||
Comment on lines
+3244
to
+3248
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.
Suggested change
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. I thought of that but it felt out of place in the docs for 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. Fair enough -- I thought it was quite nice to see together in one example how the two concepts interrelate, but I definitely don't feel strongly! |
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
.. versionadded:: 3.13 | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
Constant | ||||||||||||||||||||||||||||||
-------- | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
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
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. | ||
|
||
|
@@ -829,6 +831,10 @@ typing | |
an item of a :class:`typing.TypedDict` as read-only for type checkers. | ||
See :pep:`705` for more details. | ||
|
||
* Add :data:`typing.NoDefault`, a sentinel object used to represent the defaults | ||
of some parameters in the :mod:`typing` module. (Contributed by Jelle Zijlstra in | ||
:gh:`116126`.) | ||
|
||
unicodedata | ||
----------- | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.