konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 106 forks source link

[request] as_dict method #190

Closed kiflowb777 closed 1 year ago

kiflowb777 commented 2 years ago

Have you considered adding a method .as_dict() that creates a dictionary from a dataclass object.

Example code:

from dataclasses import dataclass
import dacite

@dataclass
class User:
    name: str
    age: int
    is_active: bool

user_dict = {
    'name': 'John',
    'age': 30,
    'is_active': True,
}

user = User(name='John', age=30, is_active=True)
another_user_dict = dacite.as_dict(data_class_object=user)

assert user_dict == another_user_dict
gunnarx commented 2 years ago

It is built into dataclasses already, if I am not misunderstanding you: https://docs.python.org/3/library/dataclasses.html#dataclasses.asdict

konradhalas commented 1 year ago

@gunnarx your answer is correct. This feature is out of scope of this library.