[BUG] Generic Collections fields - not respecting default_factory
field
specifications
#505
Labels
bug
Something isn't working
default_factory
field
specifications
#505
Description
At times, a
dataclass
field
may be type-annotated with a generic type (e.g.MutableMapping
) and defined with adefault_factory
. In such case, when deserializing, the code will try to create an instance of the annotated type which is illegal and causes an error.Code snippet that reproduces the issue
This will produce the following error:
P.S. There is a workaround for this - to define a decoder that builds the expected actual type (i.e.
dict
above) which circumvents this situation, but it is kind of a drag:Describe the results you expected
Ideally, the deserialization should instantiate a instance using the specified
default_factory
and not try to create the generic type from the annotation.From some playing around with the code, it seems that in core.py:220 (inside
_decode_dataclass
) there is an opportunity to check whether the field includes adefault_factory
and pass it into_decode_generic
, or call an alternative function that will respect thedefault_factory
.I am not familiar with the codebase, standards etc but following some discussion and decision on approach, I can possibly contribute a fix...
Python version you are using
Python 3.10.9
Environment description
dataclasses-json==0.6.3
marshmallow==3.20.1
mypy-extensions==1.0.0
packaging==23.2
typing-inspect==0.9.0
typing_extensions==4.9.0
The text was updated successfully, but these errors were encountered: