Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



What 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.


Doesnt work recursively?


At that point, you probably want proper validation with something like Pydantic, where you can use MyType.model_validate(dict).


i mean if `arg2` wasn't defined in MyDataclass, there would an error. You have to exclude fields first.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: