typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.58k stars 436 forks source link

Annotate Python serializer objects by `TypedDict` #2081

Open paduszyk opened 5 months ago

paduszyk commented 5 months ago

I was recently working with django.core.serializers.python module. It implements both serializer and deserializer classes that enable export/import the Django ORM objects into/from "Python objects". Such an object is actually as dict with a hard-coded set of keys: model (lower-case model label, str), pk (the actual object's database PK value) and fields (a dict with fields names mapped onto their serialized values). Currently, the objects are annotated by dict[str, Any].

If one considered adding some wrappers around the mentioned classes (those in django.core.serializers.json are examples) from django.core.serializers.python, it would be nice to have those "objects" fully annotated, e.g. using TypedDict, something like:

from typing import Any, NotRequired, TypedDict

class PythonSerializedObject(TypedDict):
    model: str
    pk: NotRequired[Any]  # python >= (3, 11)
    fields: dict[str, Any]

I hope you will find this idea interesting.

Anyway, I thank all the developers and maintainers of the projects. This is truly a great job done! ❤️

sobolevn commented 5 months ago

We can always try! If you want to work on this :)