From d897f7b3e84e8e0f1eccd2efceed8a7d6201e328 Mon Sep 17 00:00:00 2001 From: Erik De Bonte Date: Thu, 31 Mar 2022 12:35:25 -0700 Subject: [PATCH 1/3] Decorator placement, overloads, future params, unstated semantics --- pep-0681.rst | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/pep-0681.rst b/pep-0681.rst index 1d3dc0a32fc..e7b99873d45 100644 --- a/pep-0681.rst +++ b/pep-0681.rst @@ -78,6 +78,7 @@ sync will make it easier for dataclass users to understand and use ``dataclass_transform`` and will simplify the maintenance of dataclass support in type checkers. + Specification ============= @@ -94,6 +95,9 @@ a class, endowing it with dataclass-like behaviors. If ``dataclass_transform`` is applied to a function, using the decorated function as a decorator is assumed to apply dataclass-like semantics. +If the function has overloads, the ``dataclass_transform`` decorator can +be applied to any one overload or the implementation of the function. + If ``dataclass_transform`` is applied to a class, dataclass-like semantics will be assumed for any class that derives from the decorated class or uses the decorated class as a metaclass. @@ -239,6 +243,11 @@ customization of default behaviors: describing the stdlib dataclass behavior, we would provide the tuple argument ``(dataclasses.Field, dataclasses.field)``. +In the future, we will add additional parameters to +``dataclass_transform`` as needed to support common behaviors in user +code. These additions will be made after reaching consensus on +typing-sig rather than via additional PEPs. + The following sections provide additional examples showing how these parameters are used. @@ -473,8 +482,13 @@ For example: Dataclass semantics ------------------- -The following dataclass semantics are implied when a function or class -decorated with ``dataclass_transform`` is in use. +Except where stated otherwise in this PEP, classes impacted by +``dataclass_transform``, either by inheriting from a class that is +decorated with ``dataclass_transform`` or by being decorated with +a function decorated with ``dataclass_transform``, behave like +stdlib ``dataclass``. + +This includes, but is not limited to, the following semantics: * Frozen dataclasses cannot inherit from non-frozen dataclasses. A class that has been decorated with ``dataclass_transform`` is @@ -553,8 +567,9 @@ Undefined behavior ------------------ If multiple ``dataclass_transform`` decorators are found, either on a -single function/class or within a class hierarchy, the resulting -behavior is undefined. Library authors should avoid these scenarios. +single function (including its overloads), a single class, or within a +class hierarchy, the resulting behavior is undefined. Library authors +should avoid these scenarios. Reference Implementation @@ -684,7 +699,6 @@ support descriptor-typed fields. In fact it does, but type checkers led to our misunderstanding. For more details, see the `Pyright bug <#pyright-descriptor-bug_>`__. - ``converter`` field descriptor parameter ---------------------------------------- @@ -724,6 +738,7 @@ References .. _#class-var: https://docs.python.org/3/library/dataclasses.html#class-variables .. _#pyright-descriptor-bug: https://github.com/microsoft/pyright/issues/3245 + Copyright ========= From 5f13894cc7859cbd9d4edcfca7394d24482947f4 Mon Sep 17 00:00:00 2001 From: Erik De Bonte Date: Fri, 1 Apr 2022 09:22:14 -0700 Subject: [PATCH 2/3] Language tweaks --- pep-0681.rst | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pep-0681.rst b/pep-0681.rst index e7b99873d45..4c9d8268092 100644 --- a/pep-0681.rst +++ b/pep-0681.rst @@ -96,7 +96,8 @@ a class, endowing it with dataclass-like behaviors. If ``dataclass_transform`` is applied to a function, using the decorated function as a decorator is assumed to apply dataclass-like semantics. If the function has overloads, the ``dataclass_transform`` decorator can -be applied to any one overload or the implementation of the function. +be applied to the implementation of the function or any one, but not more +than one, of the overloads. If ``dataclass_transform`` is applied to a class, dataclass-like semantics will be assumed for any class that derives from the @@ -485,8 +486,8 @@ Dataclass semantics Except where stated otherwise in this PEP, classes impacted by ``dataclass_transform``, either by inheriting from a class that is decorated with ``dataclass_transform`` or by being decorated with -a function decorated with ``dataclass_transform``, behave like -stdlib ``dataclass``. +a function decorated with ``dataclass_transform``, are assumed to +behave like stdlib ``dataclass``. This includes, but is not limited to, the following semantics: From 6ad63019f3ed63165cc50def06041eb674b18dd8 Mon Sep 17 00:00:00 2001 From: Erik De Bonte Date: Fri, 1 Apr 2022 09:37:46 -0700 Subject: [PATCH 3/3] will -> may Co-authored-by: Jelle Zijlstra --- pep-0681.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pep-0681.rst b/pep-0681.rst index 4c9d8268092..6fad2ddca8a 100644 --- a/pep-0681.rst +++ b/pep-0681.rst @@ -244,7 +244,7 @@ customization of default behaviors: describing the stdlib dataclass behavior, we would provide the tuple argument ``(dataclasses.Field, dataclasses.field)``. -In the future, we will add additional parameters to +In the future, we may add additional parameters to ``dataclass_transform`` as needed to support common behaviors in user code. These additions will be made after reaching consensus on typing-sig rather than via additional PEPs.