From dd823995e8d54cdd37f2d18e0fb577f723fd1880 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 29 Apr 2022 17:06:20 -0600 Subject: [PATCH 1/5] gh-91491: Add several typing features to What's New This gets all the major items in #91491. However, I didn't get around to adding what's new entries for the large clump of changes in the last bullet point in the issue. --- Doc/whatsnew/3.11.rst | 64 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 5f1f995a0fe2f8..e2fb98e1ca4820 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -297,6 +297,38 @@ See :pep:`675` for more details. (Contributed by Jelle Zijlstra in :issue:`47088`. PEP written by Pradeep Kumar Srinivasan and Graham Bleaney.) +PEP 681: Data Class Transforms +------------------------------ + +The new :data:`~typing.dataclass_transform` annotation may be used to +decorate a function that is itself a decorator, a class, or a metaclass. +The presence of dataclass_transform tells a static type checker that the +decorated function, class, or metaclass performs runtime "magic" that +transforms a class, endowing it with dataclass-like behaviors. + +For example:: + + # The ``create_model`` decorator is defined by a library. + @typing.dataclass_transform() + def create_model(cls: Type[_T]) -> Type[_T]: + cls.__init__ = ... + cls.__eq__ = ... + cls.__ne__ = ... + return cls + + # The ``create_model`` decorator can now be used to create new model + # classes, like this: + @create_model + class CustomerModel: + id: int + name: str + + c = CustomerModel(id=327, name="John Smith") + +See :pep:`681` for more details. + +(Contributed by Jelle Zijlstra in :issue:`91860`. PEP written by +Erik De Bonte and Eric Traut.) Other Language Changes ====================== @@ -649,6 +681,38 @@ time it had a resolution of 1 millisecond (10\ :sup:`-3` seconds). (Contributed by Benjamin Szőke, Dong-hee Na, Eryk Sun and Victor Stinner in :issue:`21302` and :issue:`45429`.) +typing +------ + +For major changes, see :ref:`new-feat-related-type-hints-311`. + +* Add :func:`typing.assert_never` and :class:`typing.Never`. + :func:`typing.assert_never` is useful for asking a type checker to confirm + that a line of code is not reachable. At runtime, it raises an + :exc:`AssertionError`. + (Contributed by Jelle Zijlstra in :issue:`90633`.) + +* Add :func:`typing.reveal_type`. This is useful for asking a type checker + what type it has inferred for a given expression. At runtime it prints + the type of the received value. + (Contributed by Jelle Zijlstra in :issue:`90572`.) + +* Add :func:`typing.assert_type`. This is useful for asking a type checker + to confirm that the type it has inferred for a given expression matches + the given type. At runtime it simply returns the received value. + (Contributed by Jelle Zijlstra in :issue:`90638`.) + +* Allow subclassing of :class:`typing.Any`. This is useful for avoiding + type checker errors related to highly dynamic class, such as mocks. + (Contributed by Shantanu Jain in :issue:`91154`.) + +* The :func:`typing.final` decorator now sets the ``__final__`` attributed on + the decorated object. + (Contributed by Jelle Zijlstra in :issue:`90500`.) + +* The :func:`typing.get_overloads` function can be used for introspecting + the overloads of a function. + (Contributed by Jelle Zijlstra in :issue:`89263`.) unicodedata ----------- From 0095c0cf40a8416dae483146066490b42c278cde Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 29 Apr 2022 17:22:03 -0600 Subject: [PATCH 2/5] use gh role --- Doc/whatsnew/3.11.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index e2fb98e1ca4820..dfcf060ef40540 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -690,29 +690,29 @@ For major changes, see :ref:`new-feat-related-type-hints-311`. :func:`typing.assert_never` is useful for asking a type checker to confirm that a line of code is not reachable. At runtime, it raises an :exc:`AssertionError`. - (Contributed by Jelle Zijlstra in :issue:`90633`.) + (Contributed by Jelle Zijlstra in :gh:`90633`.) * Add :func:`typing.reveal_type`. This is useful for asking a type checker what type it has inferred for a given expression. At runtime it prints the type of the received value. - (Contributed by Jelle Zijlstra in :issue:`90572`.) + (Contributed by Jelle Zijlstra in :gh:`90572`.) * Add :func:`typing.assert_type`. This is useful for asking a type checker to confirm that the type it has inferred for a given expression matches the given type. At runtime it simply returns the received value. - (Contributed by Jelle Zijlstra in :issue:`90638`.) + (Contributed by Jelle Zijlstra in :gh:`90638`.) * Allow subclassing of :class:`typing.Any`. This is useful for avoiding type checker errors related to highly dynamic class, such as mocks. - (Contributed by Shantanu Jain in :issue:`91154`.) + (Contributed by Shantanu Jain in :gh:`91154`.) * The :func:`typing.final` decorator now sets the ``__final__`` attributed on the decorated object. - (Contributed by Jelle Zijlstra in :issue:`90500`.) + (Contributed by Jelle Zijlstra in :gh:`90500`.) * The :func:`typing.get_overloads` function can be used for introspecting the overloads of a function. - (Contributed by Jelle Zijlstra in :issue:`89263`.) + (Contributed by Jelle Zijlstra in :gh:`89263`.) unicodedata ----------- From 93a5ba39e5976e140bda0f48171e8ca31f1cac4d Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Fri, 29 Apr 2022 17:23:58 -0600 Subject: [PATCH 3/5] Update Doc/whatsnew/3.11.rst Co-authored-by: Jelle Zijlstra --- Doc/whatsnew/3.11.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index dfcf060ef40540..6f03d3c2b5fe45 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -302,7 +302,7 @@ PEP 681: Data Class Transforms The new :data:`~typing.dataclass_transform` annotation may be used to decorate a function that is itself a decorator, a class, or a metaclass. -The presence of dataclass_transform tells a static type checker that the +The presence of ``@dataclass_transform()`` tells a static type checker that the decorated function, class, or metaclass performs runtime "magic" that transforms a class, endowing it with dataclass-like behaviors. From 6386dce5d97478dbd943fd19926aedb79d34f33f Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 29 Apr 2022 17:26:19 -0600 Subject: [PATCH 4/5] mention clear_overloads --- Doc/whatsnew/3.11.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 6f03d3c2b5fe45..c2cf49c068d6e5 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -711,7 +711,8 @@ For major changes, see :ref:`new-feat-related-type-hints-311`. (Contributed by Jelle Zijlstra in :gh:`90500`.) * The :func:`typing.get_overloads` function can be used for introspecting - the overloads of a function. + the overloads of a function. :func:`typing.clear_overloads` can be used + to clear all registered overloads of a function. (Contributed by Jelle Zijlstra in :gh:`89263`.) unicodedata From f5a0286f522a85bb8964da641f10e0357945b469 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 29 Apr 2022 17:31:51 -0600 Subject: [PATCH 5/5] missed a spot --- Doc/whatsnew/3.11.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index c2cf49c068d6e5..61fe08fbf3a05d 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -327,7 +327,7 @@ For example:: See :pep:`681` for more details. -(Contributed by Jelle Zijlstra in :issue:`91860`. PEP written by +(Contributed by Jelle Zijlstra in :gh:`91860`. PEP written by Erik De Bonte and Eric Traut.) Other Language Changes