Skip to content

Commit bf5585d

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

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-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=getattr(attr, "kw_only", False),
173174
)
174175
for attr in attrs
175176
]

tests/test_dataclasses.py

+20
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
from typing import List
33

44
import attr
5+
import pytest
56

67
from cattrs import BaseConverter
78

9+
from ._compat import is_py310_plus
10+
811

912
@dataclasses.dataclass
1013
class Foo:
@@ -41,3 +44,20 @@ def test_dataclasses(converter: BaseConverter):
4144

4245
assert converter.unstructure(struct) == unstruct
4346
assert converter.structure(unstruct, Foo) == struct
47+
48+
49+
@pytest.mark.skipif(not is_py310_plus)
50+
def test_kw_only_propagation(converter: BaseConverter):
51+
"""KW-only args work.
52+
53+
Reproducer from https://github.com/python-attrs/cattrs/issues/637.
54+
"""
55+
56+
@dataclasses.dataclass
57+
class PartialKeywords:
58+
a1: str = "Default"
59+
a2: str = dataclasses.field(kw_only=True)
60+
61+
assert converter.structure({"a2": "Value"}, PartialKeywords) == PartialKeywords(
62+
a1="Default", a2="Value"
63+
)

0 commit comments

Comments
 (0)