Using a simple code example. There is a simple class, and a child:
from dataclasses import dataclass
@dataclass
class TestMain:
one_test: int = None
two_test: int = None
@dataclass
class SecondTest(TestMain):
three_test: int = None
four_test: int = None
I am creating an object specifying a dictionary, but not all attributes are specified in the dictionary:
test_data = {'one_test': 1,
'two_test': 2,
'three_test': 3}
test_object = SecondTest(**test_data)
If I continue to interact with the object, or look through test_data.__dict__, then I will see the attribute four_test = None.
Please specify how to do it correctly so that when creating objects, all the attributes that are not specified are not taken into account in any way? What is the best way to get rid of them, not to take them into account in the object?
I tried using asdict:
dict_object = asdict(test_object, dict_factory=lambda x: {k: v for (k, v) in x if v is not None})
I think this is redundant. There should be something simpler, without unnecessary transformations.
Here is the solution to your problem: