From ab0ee4da7059e5cc43fe1c48120ecc1f018f2d9d Mon Sep 17 00:00:00 2001 From: Erik De Bonte <erikd@microsoft.com> Date: Fri, 2 Dec 2022 14:52:03 -0800 Subject: [PATCH 1/5] Update docs, typing.py, tests --- Doc/library/typing.rst | 4 ++++ Lib/test/test_typing.py | 3 +++ Lib/typing.py | 5 +++++ 3 files changed, 12 insertions(+) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 94c9cb11f02d6d..356f919a1897b2 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2575,6 +2575,10 @@ Functions and decorators assumed to be True or False if it is omitted by the caller. * ``kw_only_default`` indicates whether the ``kw_only`` parameter is assumed to be True or False if it is omitted by the caller. + * ``frozen_default`` indicates whether the ``frozen`` parameter is + assumed to be True or False if it is omitted by the caller. + + .. versionadded:: 3.12 * ``field_specifiers`` specifies a static list of supported classes or functions that describe fields, similar to ``dataclasses.field()``. * Arbitrary other keyword arguments are accepted in order to allow for diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index da602b0199d52c..36d290d4f3c569 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7719,6 +7719,7 @@ class CustomerModel: "eq_default": True, "order_default": False, "kw_only_default": True, + "frozen_default": False, "field_specifiers": (), "kwargs": {}, } @@ -7749,6 +7750,7 @@ class CustomerModel(Decorated, frozen=True): "eq_default": True, "order_default": True, "kw_only_default": False, + "frozen_default": False, "field_specifiers": (), "kwargs": {"make_everything_awesome": True}, } @@ -7780,6 +7782,7 @@ class CustomerModel(ModelBase, init=False): "eq_default": True, "order_default": True, "kw_only_default": False, + "frozen_default": False, "field_specifiers": (Field,), "kwargs": {}, } diff --git a/Lib/typing.py b/Lib/typing.py index 38e227e3c55d59..8a7f4edddfef21 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3363,6 +3363,7 @@ def dataclass_transform( eq_default: bool = True, order_default: bool = False, kw_only_default: bool = False, + frozen_default: bool = False, field_specifiers: tuple[type[Any] | Callable[..., Any], ...] = (), **kwargs: Any, ) -> Callable[[T], T]: @@ -3416,6 +3417,9 @@ class CustomerModel(ModelBase): assumed to be True or False if it is omitted by the caller. - ``kw_only_default`` indicates whether the ``kw_only`` parameter is assumed to be True or False if it is omitted by the caller. + - ``frozen_default`` indicates whether the ``frozen`` parameter is + assumed to be True or False if it is omitted by the caller. Only + supported in Python 3.12+. - ``field_specifiers`` specifies a static list of supported classes or functions that describe fields, similar to ``dataclasses.field()``. - Arbitrary other keyword arguments are accepted in order to allow for @@ -3432,6 +3436,7 @@ def decorator(cls_or_fn): "eq_default": eq_default, "order_default": order_default, "kw_only_default": kw_only_default, + "frozen_default": frozen_default, "field_specifiers": field_specifiers, "kwargs": kwargs, } From 44e38d94a0bc6e67ac12129b264558900e96a737 Mon Sep 17 00:00:00 2001 From: Erik De Bonte <erikd@microsoft.com> Date: Fri, 2 Dec 2022 17:20:50 -0800 Subject: [PATCH 2/5] Jelle's suggestion Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> --- Lib/typing.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/typing.py b/Lib/typing.py index 8a7f4edddfef21..d9d6fbcdb8f068 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3418,8 +3418,7 @@ class CustomerModel(ModelBase): - ``kw_only_default`` indicates whether the ``kw_only`` parameter is assumed to be True or False if it is omitted by the caller. - ``frozen_default`` indicates whether the ``frozen`` parameter is - assumed to be True or False if it is omitted by the caller. Only - supported in Python 3.12+. + assumed to be True or False if it is omitted by the caller. - ``field_specifiers`` specifies a static list of supported classes or functions that describe fields, similar to ``dataclasses.field()``. - Arbitrary other keyword arguments are accepted in order to allow for From 85ea95094b34312de8cb318dc2c4a017f3062697 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 3 Dec 2022 05:58:49 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst diff --git a/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst b/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst new file mode 100644 index 00000000000000..761e28acac3932 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst @@ -0,0 +1 @@ +Add `frozen_default` parameter to :func:`typing.dataclass_transform`. From c6158c1677d6c7da1a99a328bf3cb3a8fd2bb0c8 Mon Sep 17 00:00:00 2001 From: Erik De Bonte <erikd@microsoft.com> Date: Fri, 2 Dec 2022 22:42:25 -0800 Subject: [PATCH 4/5] Double quotes around `frozen_default` in news --- .../next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst b/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst index 761e28acac3932..4fd7b6b85cef68 100644 --- a/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst +++ b/Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst @@ -1 +1 @@ -Add `frozen_default` parameter to :func:`typing.dataclass_transform`. +Add ``frozen_default`` parameter to :func:`typing.dataclass_transform`. From c949233a7daa33d6734e839d06320a707e9dd055 Mon Sep 17 00:00:00 2001 From: Erik De Bonte <erikd@microsoft.com> Date: Mon, 5 Dec 2022 13:09:55 -0800 Subject: [PATCH 5/5] Modify tests to validate that __dataclass_transform__ actually reflects frozen_default value --- Lib/test/test_typing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 36d290d4f3c569..1cae1b0de7140f 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -7767,7 +7767,7 @@ def __new__( return super().__new__(cls, name, bases, namespace) Decorated = dataclass_transform( - order_default=True, field_specifiers=(Field,) + order_default=True, frozen_default=True, field_specifiers=(Field,) )(ModelMeta) class ModelBase(metaclass=Decorated): ... @@ -7782,7 +7782,7 @@ class CustomerModel(ModelBase, init=False): "eq_default": True, "order_default": True, "kw_only_default": False, - "frozen_default": False, + "frozen_default": True, "field_specifiers": (Field,), "kwargs": {}, }