mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Added kw_only parameter to make_dataclasses. (GH-29679)
(cherry picked from commit f7638dd0f9)
Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
This commit is contained in:
parent
3528df1258
commit
cf8c8788c9
3 changed files with 14 additions and 2 deletions
|
|
@ -1326,7 +1326,7 @@ def _astuple_inner(obj, tuple_factory):
|
||||||
|
|
||||||
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
|
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
|
||||||
repr=True, eq=True, order=False, unsafe_hash=False,
|
repr=True, eq=True, order=False, unsafe_hash=False,
|
||||||
frozen=False, match_args=True, slots=False):
|
frozen=False, match_args=True, kw_only=False, slots=False):
|
||||||
"""Return a new dynamically created dataclass.
|
"""Return a new dynamically created dataclass.
|
||||||
|
|
||||||
The dataclass name will be 'cls_name'. 'fields' is an iterable
|
The dataclass name will be 'cls_name'. 'fields' is an iterable
|
||||||
|
|
@ -1393,7 +1393,7 @@ def exec_body_callback(ns):
|
||||||
# Apply the normal decorator.
|
# Apply the normal decorator.
|
||||||
return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
|
return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
|
||||||
unsafe_hash=unsafe_hash, frozen=frozen,
|
unsafe_hash=unsafe_hash, frozen=frozen,
|
||||||
match_args=match_args, slots=slots)
|
match_args=match_args, kw_only=kw_only, slots=slots)
|
||||||
|
|
||||||
|
|
||||||
def replace(obj, /, **changes):
|
def replace(obj, /, **changes):
|
||||||
|
|
|
||||||
|
|
@ -3864,5 +3864,16 @@ class A:
|
||||||
c: int = 1
|
c: int = 1
|
||||||
d: int
|
d: int
|
||||||
|
|
||||||
|
def test_make_dataclass(self):
|
||||||
|
A = make_dataclass("A", ['a'], kw_only=True)
|
||||||
|
self.assertTrue(fields(A)[0].kw_only)
|
||||||
|
|
||||||
|
B = make_dataclass("B",
|
||||||
|
['a', ('b', int, field(kw_only=False))],
|
||||||
|
kw_only=True)
|
||||||
|
self.assertTrue(fields(B)[0].kw_only)
|
||||||
|
self.assertFalse(fields(B)[1].kw_only)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
Added missing kw_only parameter to dataclasses.make_dataclass().
|
||||||
Loading…
Add table
Add a link
Reference in a new issue