8000 Added kw_only parameter to make_dataclasses. (GH-29679) · python/cpython@cf8c878 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf8c878

Browse files
Added kw_only parameter to make_dataclasses. (GH-29679)
(cherry picked from commit f7638dd) Co-authored-by: Eric V. Smith <ericvsmith@users.noreply.github.com>
1 parent 3528df1 commit cf8c878

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

Lib/dataclasses.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ def _astuple_inner(obj, tuple_factory):
13261326

13271327
def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
13281328
repr=True, eq=True, order=False, unsafe_hash=False,
1329-
frozen=False, match_args=True, slots=False):
1329+
frozen=False, match_args=True, kw_only=False, slots=False):
13301330
"""Return a new dynamically created dataclass.
13311331
13321332
The dataclass name will be 'cls_name'. 'fields' is an iterable
@@ -1393,7 +1393,7 @@ def exec_body_callback(ns):
13931393
# Apply the normal decorator.
13941394
return dataclass(cls, init=init, repr=repr, eq=eq, order=order,
13951395
unsafe_hash=unsafe_hash, frozen=frozen,
1396-
match_args=match_args, slots=slots)
1396+
match_args=match_args, kw_only=kw_only, slots=slots)
13971397

13981398

13991399
def replace(obj, /, **changes):

Lib/test/test_dataclasses.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3864,5 +3864,16 @@ class A:
38643864
c: int = 1
38653865
d: int
38663866

3867+
def test_make_dataclass(self):
3868+
A = make_dataclass("A", ['a'], kw_only=True)
3869+
self.assertTrue(fields(A)[0].kw_only)
3870+
3871+
B = make_dataclass("B",
3872+
['a', ('b', int, field(kw_only=False))],
3873+
kw_only=True)
758E 3874+
self.assertTrue(fields(B)[0].kw_only)
3875+
self.assertFalse(fields(B)[1].kw_only)
3876+
3877+
38673878
if __name__ == '__main__':
38683879
unittest.main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added missing kw_only parameter to dataclasses.make_dataclass().

0 commit comments

Comments
 (0)
0