Skip to content

Commit 9a2f603

Browse files
committed
Dataclasses: fix kwonly arg recognition
1 parent a8d4514 commit 9a2f603

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/cattrs/_compat.py

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ def adapted_fields(cl) -> List[Attribute]:
170170
True,
171171
type=type_hints.get(attr.name, attr.type),
172172
alias=attr.name,
173+
kw_only=attr.kw_only,
173174
)
174175
for attr in attrs
175176
]

tests/test_dataclasses.py

+16
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,19 @@ def test_dataclasses(converter: BaseConverter):
4141

4242
assert converter.unstructure(struct) == unstruct
4343
assert converter.structure(unstruct, Foo) == struct
44+
45+
46+
def test_kw_only_propagation(converter: BaseConverter):
47+
"""KW-only args work.
48+
49+
Reproducer from https://github.com/python-attrs/cattrs/issues/637.
50+
"""
51+
52+
@dataclasses.dataclass
53+
class PartialKeywords:
54+
a1: str = "Default"
55+
a2: str = dataclasses.field(kw_only=True)
56+
57+
assert converter.structure({"a2": "Value"}, PartialKeywords) == PartialKeywords(
58+
a1="Default", a2="Value"
59+
)

0 commit comments

Comments
 (0)