Suppose that a dataclass field is type-annotated like
Union[Class1, Class2]. Unless the input
data is specifically either a
Class2 instance, the data won’t be
de-serialized as expected. However, the good news is that there is a simple enough workaround
in this scenario.
As of the v0.14.0 release, the
dataclass-wizard supports declaring dataclasses
Union types. Previously, it did not support dataclasses within
at all, which was kind of a glaring omission, and something on my “to-do” list of things
to (eventually) add support for.
There is now full support for defining dataclasses as
Union type arguments. The
reason it did not generally work before, is because the data being de-serialized is
often a JSON object, which only knows simple types such as arrays and dictionaries,
for example. A
dict type would not otherwise match any of the
types, even if the object had all the correct dataclass fields as keys. This is simply
because it doesn’t attempt to de-serialize the
dict object, in a round robin fashion,
into each of the dataclass models in the
Union arguments – though that might
change in a future release.