I'm working with a JSON data structure and am trying to represent it as a dataclass. The data structure is (partly) circular and I want the nested data structures to be neatly represented as dataclasses as well.
I am having some trouble getting the dataclasses to parse correctly. See the simplified example below:
from typing import List, Optional, Union
class SchemaTypeName(Enum):
LONG = "long"
NULL = "null",
RECORD = "record"
STRING = "string"
@dataclass_json
@dataclass
class SchemaType():
type: Union[
SchemaTypeName,
'SchemaType',
List[
Union[
SchemaTypeName,
'SchemaType'
]
]
]
fields: Optional[List['SchemaType']] = None
name: Optional[str] = None
Below is a printout of the object returned after calling from_dict
with some sample data. Notice that the nested object (indicated with the arrow) is not parsed as a dataclass correctly.
SchemaType(
type=[
'null',
------> {
'fields': [
{'name': 'id', 'type': 'string'},
{'name': 'date', 'type': ['null', 'long']},
{'name': 'name', 'type': ['null', 'string']}
],
'type': 'record'
}
]
)
Am I declaring the type hint for the type
field incorrectly?
I'm using Python 3.9
with dataclasses_json==0.5.2
and marshmallow==3.11.1
.