Skip to content

Commit

Permalink
Rename parameter to decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Sep 30, 2024
1 parent 04ff33c commit cba7315
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Doc/library/dataclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,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, dataclass_factory=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, decorator=dataclass)

Creates a new dataclass with name *cls_name*, fields as defined
in *fields*, base classes as given in *bases*, and initialized
Expand All @@ -415,7 +415,7 @@ Module contents
of the dataclass is set to that value.
By default, it is set to the module name of the caller.

The *dataclass_factory* is a callable that will be used to create the dataclass.
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 :func:`@dataclass <dataclass>`. By default, the :func:`@dataclass <dataclass>`
function is used.
Expand Down Expand Up @@ -444,7 +444,7 @@ Module contents
return self.x + 1

.. versionadded:: 3.14
Added the *dataclass_factory* parameter.
Added the *decorator* parameter.

.. function:: replace(obj, /, **changes)

Expand Down
10 changes: 5 additions & 5 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,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, dataclass_factory=dataclass):
weakref_slot=False, module=None, decorator=dataclass):
"""Return a new dynamically created dataclass.
The dataclass name will be 'cls_name'. 'fields' is an iterable
Expand Down Expand Up @@ -1631,10 +1631,10 @@ def exec_body_callback(ns):
cls.__module__ = module

# Apply the normal provided decorator.
return dataclass_factory(cls, init=init, repr=repr, eq=eq, order=order,
unsafe_hash=unsafe_hash, frozen=frozen,
match_args=match_args, kw_only=kw_only, slots=slots,
weakref_slot=weakref_slot)
return decorator(cls, init=init, repr=repr, eq=eq, order=order,
unsafe_hash=unsafe_hash, frozen=frozen,
match_args=match_args, kw_only=kw_only, slots=slots,
weakref_slot=weakref_slot)


def replace(obj, /, **changes):
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4317,18 +4317,18 @@ def test_funny_class_names_names(self):
C = make_dataclass(classname, ['a', 'b'])
self.assertEqual(C.__name__, classname)

def test_dataclass_factory_default(self):
C = make_dataclass('C', [('x', int)], dataclass_factory=dataclass)
def test_dataclass_decorator_default(self):
C = make_dataclass('C', [('x', int)], decorator=dataclass)
c = C(10)
self.assertEqual(c.x, 10)

def test_dataclass_custom_factory(self):
def test_dataclass_custom_decorator(self):
def custom_dataclass(cls, *args, **kwargs):
dc = dataclass(cls, *args, **kwargs)
dc.__custom__ = True
return dc

C = make_dataclass('C', [('x', int)], dataclass_factory=custom_dataclass)
C = make_dataclass('C', [('x', int)], decorator=custom_dataclass)
c = C(10)
self.assertEqual(c.x, 10)
self.assertEqual(c.__custom__, True)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Add ``dataclass_factory`` parameter to :func:`dataclasses.make_dataclass`
Add ``decorator`` parameter to :func:`dataclasses.make_dataclass`
to customize the functional creation of dataclasses.

0 comments on commit cba7315

Please sign in to comment.