I have a lot of dataclasses that are nested using post_init
from dataclasses import dataclass
from typing import List
from typing import Optional
from typing import Union
@dataclass
class MyClass:
signed_key: str
signature: str
@dataclass
class Message:
signature: str
my_class: Union[MyClass, dict]
protocol_version: str
signed_message: str
def __post_init__(self):
self.my_class = MyClass(**self.my_class)
It work nice but the problem with this is that if i want to convert Message to dict like so:
#create a message object before
print(message.__dict__)
The output i get:
{'signature': 'test', 'my_class': Myclass(signed_key='test', signature='test'), 'protocol_version': 'test', 'signed_message': 'test'}
What i want (nested dict):
{'signature': 'test', 'my_class': {'signed_key': 'test', 'signature': 'test'}, 'protocol_version': 'test', 'signed_message': 'test'}
I could have use libs like attrs or pydantic but the goal here is to only use pure python