Open
Description
Description
I am creating a very strict model layer where any undefined value should raise an exception. Due to this, all my dataclasses inherit from one that sets undefined=Undefined.RAISE
. However, when using a list[TypeA | TypeB]
(both types are also dataclasses with the same Undefined.RAISE
behavior), it looks like it evaluates it only for the first type.
On the first glance, it looks like this except
could also capture UndefinedParameterError
but I am not sure what the further implications would be.
Code snippet that reproduces the issue
Consider following code:
from abc import ABC
from dataclasses import dataclass
from dataclasses_json import Undefined, dataclass_json
@dataclass_json(undefined=Undefined.RAISE)
@dataclass
class JSONData(ABC):
pass
@dataclass
class TypeA(JSONData):
value: str
# some additional fields
@dataclass
class TypeB(JSONData):
values: list[TypeA]
# some additional fields
@dataclass
class Response(JSONData):
values: list[TypeA | TypeB]
# some additional fields
# This works because it matches the first type.
response = Response.from_dict({
"values" : [ {
"value" : "....."
} ]
})
# This fails because UndefinedParameterError is raised from first type match attempt.
response = Response.from_dict({
"values" : [ {
"values" : [ {
"value" : "..."
} ]
} ]
})
Describe the results you expected
Deserialization failure for the first type in the union (not only due to undefined) shouldn't interrupt the whole deserialization routine.
Python version you are using
3.10.12
Environment description
dataclasses-json==0.6.7