[Python-checkins] gh-99957: Add `frozen_default` parameter on `dataclass_transform` (#99958)

JelleZijlstra webhook-mailer at python.org
Mon Dec 5 22:35:48 EST 2022


https://github.com/python/cpython/commit/5c19050546e3e37a8889a0baa2954e1444e803d3
commit: 5c19050546e3e37a8889a0baa2954e1444e803d3
branch: main
author: Erik De Bonte <erikd at microsoft.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-12-05T19:35:43-08:00
summary:

gh-99957: Add `frozen_default` parameter on `dataclass_transform` (#99958)

files:
A Misc/NEWS.d/next/Library/2022-12-03-05-58-48.gh-issue-99957.jLYYgN.rst
M Doc/library/typing.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst
index 94c9cb11f02d..356f919a1897 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 da602b0199d5..1cae1b0de714 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},
             }
@@ -7765,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): ...
@@ -7780,6 +7782,7 @@ class CustomerModel(ModelBase, init=False):
                 "eq_default": True,
                 "order_default": True,
                 "kw_only_default": False,
+                "frozen_default": True,
                 "field_specifiers": (Field,),
                 "kwargs": {},
             }
diff --git a/Lib/typing.py b/Lib/typing.py
index 38e227e3c55d..d9d6fbcdb8f0 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,8 @@ 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.
     - ``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 +3435,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,
         }
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 000000000000..4fd7b6b85cef
--- /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`.



More information about the Python-checkins mailing list