]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-79413: Add `qualname` parameter to `dataclass.make_dataclass`. (GH-150026)
authorDan Shernicoff <dan@brassnet.biz>
Mon, 18 May 2026 23:55:47 +0000 (16:55 -0700)
committerGitHub <noreply@github.com>
Mon, 18 May 2026 23:55:47 +0000 (19:55 -0400)
Added `qualname` parameter to `dataclasses.make_dataclass` in order to allow user to set `__qualname__` for the generated class.

Doc/library/dataclasses.rst
Lib/dataclasses.py
Lib/test/test_dataclasses/__init__.py
Misc/NEWS.d/next/Library/2026-05-18-13-43-06.gh-issue-79413.OpTXbV.rst [new file with mode: 0644]

index a09c28ad9791584dc994de39e0e032afe8d11d14..954edc4506df1a302fdf425e71f1636a0546852e 100644 (file)
@@ -418,7 +418,7 @@ Module contents
    :func:`!astuple` raises :exc:`TypeError` if *obj* is not a dataclass
    instance.
 
-.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False, module=None, decorator=dataclass)
+.. function:: make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False, match_args=True, kw_only=False, slots=False, weakref_slot=False, module=None, qualname=None, decorator=dataclass)
 
    Creates a new dataclass with name *cls_name*, fields as defined
    in *fields*, base classes as given in *bases*, and initialized
@@ -434,6 +434,9 @@ Module contents
    of the dataclass is set to that value.
    By default, it is set to the module name of the caller.
 
+   If *qualname* is defined, the :attr:`~type.__qualname__` attribute of the dataclass
+   is set to that value. By default, it is set to the value passed to *cls_name*.
+
    The *decorator* parameter is a callable that will be used to create the dataclass.
    It should take the class object as a first argument and the same keyword arguments
    as :deco:`dataclass`. By default, the :deco:`dataclass`
@@ -464,6 +467,8 @@ Module contents
 
    .. versionadded:: 3.14
       Added the *decorator* parameter.
+   .. versionadded:: next
+      Added the *qualname* parameter.
 
 .. function:: replace(obj, /, **changes)
 
index dbfabded2e47aa2b8b10225fdf15fa919eee4e71..035678d902adaf90ccc09ad2c687344c7a4661ec 100644 (file)
@@ -1644,7 +1644,7 @@ def _astuple_inner(obj, tuple_factory):
 def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
                    repr=True, eq=True, order=False, unsafe_hash=False,
                    frozen=False, match_args=True, kw_only=False, slots=False,
-                   weakref_slot=False, module=None, decorator=dataclass):
+                   weakref_slot=False, module=None, qualname=None, decorator=dataclass):
     """Return a new dynamically created dataclass.
 
     The dataclass name will be 'cls_name'.  'fields' is an iterable
@@ -1669,6 +1669,9 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
 
     If module parameter is defined, the '__module__' attribute of the dataclass is
     set to that value.
+
+    If qualname parameter is defined, the '__qualname__' attribute of the dataclass is set
+    to that value.
     """
 
     if namespace is None:
@@ -1758,6 +1761,9 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
     if module is not None:
         cls.__module__ = module
 
+    if qualname:
+        cls.__qualname__ = qualname
+
     # Apply the normal provided decorator.
     cls = decorator(cls, init=init, repr=repr, eq=eq, order=order,
                     unsafe_hash=unsafe_hash, frozen=frozen,
index dcd6a3ef9abfab6e135bdd40678bc0e4243a1c39..2468e3e64dd621c06a44f0cce3d8642145b7c6c7 100644 (file)
@@ -5289,6 +5289,15 @@ class TestKeywordArgs(unittest.TestCase):
         self.assertEqual(len(fs), 1)
         self.assertEqual(fs[0].name, 'x')
 
+    def test_makedataclass_with_qualname(self):
+        A = make_dataclass("A", ['a'], qualname='ClassA')
+        self.assertEqual(A.__qualname__, 'ClassA')
+
+        B = make_dataclass("B", ['b'], qualname='module1.ClassB')
+        self.assertEqual(B.__qualname__, 'module1.ClassB')
+
+        C = make_dataclass("C", ['c'])
+        self.assertEqual(C.__qualname__, 'C')
 
 class TestZeroArgumentSuperWithSlots(unittest.TestCase):
     def test_zero_argument_super(self):
diff --git a/Misc/NEWS.d/next/Library/2026-05-18-13-43-06.gh-issue-79413.OpTXbV.rst b/Misc/NEWS.d/next/Library/2026-05-18-13-43-06.gh-issue-79413.OpTXbV.rst
new file mode 100644 (file)
index 0000000..8d62fda
--- /dev/null
@@ -0,0 +1,3 @@
+Update :func:`dataclasses.make_dataclass` to add a *qualname* parameter. The
+*qualname* parameter will be used to set the :attr:`!__qualname__` of the
+created ``dataclass``.