Skip to content

Implement ModelSerializer.__class_getitem__ #7384

Closed
@antonagestam

Description

@antonagestam

To allow type checking efforts to properly type the methods of ModelSerializer it needs to be made generic with regards to the model it serializes. Since django-rest-framework has chosen to not own any annotations themselves the only way to achieve that will be to implement __class_getitem__. Stub implementors can then make the class generic and calling ModelSerializer[Author] will not yield subscriptability errors at runtime.

Note that this is the unfortunate direction Django has chosen as well.

This is the relevant issue in djangorestframework-stubs for reference.

This is the relevant Python docs on generic classes.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions