You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dataclass kw_only attribute incorrectly structured without detailed validation enabled #637
A dataclass class is defined, with the first attribute possibly positional, and the second attribute with kw_only=True. The first attribute has a default value, so it is optional.
When structuring the passed dict, cattrs is expected to honor the kw_only attribute setting. It does not.
Suggested root cause
The _compat module has functions to support dataclasses by converting some features to the corresponding attrs concepts. The function adapted_fields() does this for class attribute descriptors by creating cattrs Attribute instances from dataclass fields.
The Attribute creation lacks the kw_only argument and therefore uses the default kw_only=False.
"Unfortunately" the defect hits only in one scenario:
When using BaseConverter, this does not hit, as structure_attrs_fromdict always uses kw-based creation.
When using Converter with detailed validation enabled, the generated instantiation script is also always using kw-based creation.
Only when using Converterwithout detailed validation support, the generated instantion script depends on the kw_only setting of an attribute.
The latter distinction is part of Converter.make_dict_structure_n_from_attrs, where you find code like:
As only the non-detailed-validation case is currently respecting kw_only at all, this could could be removed to even in that case always use kw-based class creation in the instantiation script.
If that code should be kept, adapted_fields can possibly be fixed easily by propagating the kw_only setting:
changed the title [-]Dataclass kw_only attribute incorrectly structured without detailed validation enbaled[/-][+]Dataclass kw_only attribute incorrectly structured without detailed validation enabled[/+]on Mar 25, 2025
Activity
[-]Dataclass kw_only attribute incorrectly structured without detailed validation enbaled[/-][+]Dataclass kw_only attribute incorrectly structured without detailed validation enabled[/+]Tinche commentedon Mar 25, 2025
Mmm good catch. Let me see if I can release this as 24.1.3 soon.
Tinche commentedon Mar 25, 2025
New version is out! Great job on the analysis.
moltob commentedon Mar 26, 2025
Thank you for releasing this so quickly! This is a terrific library.