Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,8 @@ def _astuple_inner(obj, tuple_factory):
return deepcopy(obj)


def make_dataclass(cls_name, fields, *, bases=(), namespace=None):
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
repr=True, eq=True, order=False, hash=None, frozen=False):
"""Return a new dynamically created dataclass.

The dataclass name will be 'cls_name'. 'fields' is an iterable
Expand All @@ -723,6 +724,9 @@ class C(Base):
b: int = field(init=False)

For the bases and namespace paremeters, see the builtin type() function.

The parameters init, repr, eq, order, hash, and frozen are passed to
dataclass().
"""

if namespace is None:
Expand All @@ -745,8 +749,8 @@ class C(Base):

namespace['__annotations__'] = anns
cls = type(cls_name, bases, namespace)
return dataclass(cls)

return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
hash=hash, frozen=frozen)

def replace(obj, **changes):
"""Return a new object replacing specified fields with new values.
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,23 @@ def test_helper_make_dataclass_class_var(self):
self.assertEqual(C.y, 10)
self.assertEqual(C.z, 20)

def test_helper_make_dataclass_other_params(self):
C = make_dataclass('C',
[('x', int),
('y', ClassVar[int], 10),
('z', ClassVar[int], field(default=20)),
],
init=False)
# Make sure we have a repr, but no init.
self.assertNotIn('__init__', vars(C))
self.assertIn('__repr__', vars(C))

# Make sure random other params don't work.
with self.assertRaisesRegex(TypeError, 'unexpected keyword argument'):
C = make_dataclass('C',
[],
xxinit=False)

def test_helper_make_dataclass_no_types(self):
C = make_dataclass('Point', ['x', 'y', 'z'])
c = C(1, 2, 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add params to dataclasses.make_dataclasses(): init, repr, eq, order, hash,
and frozen. Pass them through to dataclass().