Skip to content

bpo-33129: Add kwarg support for dataclass' generated init method #19206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def _init_fn(fields, frozen, has_post_init, self_name, globals):
body_lines = ['pass']

return _create_fn('__init__',
[self_name] + [_init_param(f) for f in fields if f.init],
[self_name] + [_init_param(f) for f in fields if f.init] + ['**kwargs'],
body_lines,
locals=locals,
globals=globals,
Expand Down
35 changes: 18 additions & 17 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,8 @@ def validate_class(cls):
self.assertIs (param.annotation, float)
# Don't test for the default, since it's set to MISSING.
self.assertEqual(param.kind, inspect.Parameter.POSITIONAL_OR_KEYWORD)
param = next(params)
self.assertEqual(param.name, 'kwargs')
self.assertRaises(StopIteration, next, params)


Expand Down Expand Up @@ -1990,22 +1992,22 @@ def test_docstring_no_fields(self):
class C:
pass

self.assertDocStrEqual(C.__doc__, "C()")
self.assertDocStrEqual(C.__doc__, "C(**kwargs)")

def test_docstring_one_field(self):
@dataclass
class C:
x: int

self.assertDocStrEqual(C.__doc__, "C(x:int)")
self.assertDocStrEqual(C.__doc__, "C(x:int, **kwargs)")

def test_docstring_two_fields(self):
@dataclass
class C:
x: int
y: int

self.assertDocStrEqual(C.__doc__, "C(x:int, y:int)")
self.assertDocStrEqual(C.__doc__, "C(x:int, y:int, **kwargs)")

def test_docstring_three_fields(self):
@dataclass
Expand All @@ -2014,49 +2016,49 @@ class C:
y: int
z: str

self.assertDocStrEqual(C.__doc__, "C(x:int, y:int, z:str)")
self.assertDocStrEqual(C.__doc__, "C(x:int, y:int, z:str, **kwargs)")

def test_docstring_one_field_with_default(self):
@dataclass
class C:
x: int = 3

self.assertDocStrEqual(C.__doc__, "C(x:int=3)")
self.assertDocStrEqual(C.__doc__, "C(x:int=3, **kwargs)")

def test_docstring_one_field_with_default_none(self):
@dataclass
class C:
x: Union[int, type(None)] = None

self.assertDocStrEqual(C.__doc__, "C(x:Union[int, NoneType]=None)")
self.assertDocStrEqual(C.__doc__, "C(x:Union[int, NoneType]=None, **kwargs)")

def test_docstring_list_field(self):
@dataclass
class C:
x: List[int]

self.assertDocStrEqual(C.__doc__, "C(x:List[int])")
self.assertDocStrEqual(C.__doc__, "C(x:List[int], **kwargs)")

def test_docstring_list_field_with_default_factory(self):
@dataclass
class C:
x: List[int] = field(default_factory=list)

self.assertDocStrEqual(C.__doc__, "C(x:List[int]=<factory>)")
self.assertDocStrEqual(C.__doc__, "C(x:List[int]=<factory>, **kwargs)")

def test_docstring_deque_field(self):
@dataclass
class C:
x: deque

self.assertDocStrEqual(C.__doc__, "C(x:collections.deque)")
self.assertDocStrEqual(C.__doc__, "C(x:collections.deque, **kwargs)")

def test_docstring_deque_field_with_default_factory(self):
@dataclass
class C:
x: deque = field(default_factory=deque)

self.assertDocStrEqual(C.__doc__, "C(x:collections.deque=<factory>)")
self.assertDocStrEqual(C.__doc__, "C(x:collections.deque=<factory>, **kwargs)")


class TestInit(unittest.TestCase):
Expand Down Expand Up @@ -3162,9 +3164,9 @@ class C:
# if we're also replacing one that does exist. Test this
# here, because setting attributes on frozen instances is
# handled slightly differently from non-frozen ones.
with self.assertRaisesRegex(TypeError, r"__init__\(\) got an unexpected "
"keyword argument 'a'"):
with self.assertRaisesRegex(AttributeError, r"'C' object has no attribute 'a'"):
c1 = replace(c, x=20, a=5)
result = c1.a

def test_invalid_field_name(self):
@dataclass(frozen=True)
Expand All @@ -3173,9 +3175,9 @@ class C:
y: int

c = C(1, 2)
with self.assertRaisesRegex(TypeError, r"__init__\(\) got an unexpected "
"keyword argument 'z'"):
with self.assertRaisesRegex(AttributeError, r"'C' object has no attribute 'z'"):
c1 = replace(c, z=3)
result = c1.z

def test_invalid_object(self):
@dataclass(frozen=True)
Expand Down Expand Up @@ -3222,9 +3224,8 @@ class C:
self.assertEqual(c.y, 1000)

# Trying to replace y is an error: can't replace ClassVars.
with self.assertRaisesRegex(TypeError, r"__init__\(\) got an "
"unexpected keyword argument 'y'"):
replace(c, y=30)
c1 = replace(c, y=30)
self.assertEqual(c, c1)

replace(c, x=5)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added kwargs support for dataclass' generated init method so that we can more expressively interact with HTTP response body objects by extracting the object members we want and ignoring the rest without raising an TypeError exception.