Doist / todoist-api-python

A python wrapper for the Todoist REST API.
MIT License
185 stars 20 forks source link

Enhancement: Implement Object Mapping Using Pydantic / Msgspec #128

Open BoggerByte opened 6 months ago

BoggerByte commented 6 months ago

Enhancement description

There are a lot of hardcoded values used for object mapping for models (from_dict method) within the library. For instance, this is the Section class mapping from dict:

@dataclass
class Section:
    id: str
    name: str
    order: int
    project_id: str

    @classmethod
    def from_dict(cls, obj):
        return cls(
            id=obj["id"],
            name=obj["name"],
            order=obj["order"],
            project_id=obj["project_id"],
        )

I suggest allowing some third-party object mapping library like pydantic or msgspec to handle this logic.

The problem it solves

The current implementation with hardcoded values in the from_dict method makes the code less maintainable and flexible. By integrating a third-party library for object mapping, we can improve readability, maintainability, and reduce the chances of errors.

Alternatives

Currently, we could continue with the hardcoded approach, but it may lead to increased complexity and maintenance overhead as the project grows. Alternatively, we can manually implement more flexible object mapping methods, but this may also introduce additional boilerplate code and potential errors.

Use case / screenshots

N/A

Additional information

Integrating a third-party library for object mapping will not only streamline the codebase but also potentially introduce additional features such as validation and serialization, which can enhance the robustness and usability of the library.

BoggerByte commented 6 months ago

I've submitted a PR addressing the issue! #129 🎉 Check it out and let me know your thoughts! Your feedback is highly valued.