3 ms·
I tried using python dataclasses in some projects, the only complaint is that I can not directly pass any dict to a dataclass but have to lint only supported ke
by est 1y ago
I tried using python dataclasses in some projects, the only complaint is that I can not directly pass any dict to a dataclass but have to lint only supported keys.
I think a better way to init a dataclass would be something like `mydataclass(my_dict, lint=True)` instead of passing values as kwargs.
- x187463 1y agoWhat do you mean pass a dict? As in kwargs? kwargs = {'arg1':'val1', 'arg2':'val2'} my_obj = MyDataclass(**kwargs) Works fine for a dataclass with arg1 and arg2 attributes.
- never_inline 1y agoDoesnt work recursively?
- maleldil 1y agoAt that point, you probably want proper validation with something like Pydantic, where you can use MyType.model_validate(dict).
- est 1y agoi mean if `arg2` wasn't defined in MyDataclass, there would an error. You have to exclude fields first.