tfranzel/drf-spectacular

Inconsistency in typing DRF-Stubs in AutoSchema regarding Serializer

Opened this issue · 0 comments

Describe the bug
This is typing issue, not affecting anyhow the execution of code.
When we override the AutoSchema and provide it proper types, then we get some inconsistency between what DRF-stubs returns and what drf-spectacular expects

To Reproduce
The example is from blueprints of RW Serialziers:

class RWAutoSchema(openapi.AutoSchema):
    @typing_extensions.override
    def get_request_serializer(self) -> drf_serializers.BaseSerializer[_Mo_co]:  # <-- this is what caused issues as Spectacular expects `Serializer | type[Serializer] | None` when DRF-stubs return it as `BaseSerializer`
        if isinstance(self.view, drf_rw_serializers_generics.GenericAPIView):
            return self.view.get_write_serializer()
        return t.cast(drf_serializers.BaseSerializer[_Mo_co], self._get_serializer())  # <-- need cast here as `_get_serializer` is untyped

    @typing_extensions.override
    def get_response_serializers(self) -> drf_serializers.BaseSerializer[_Mo_co]:  # <-- this is what caused issues as Spectacular expects `Serializer | type[Serializer] | None` when DRF-stubs return it as `BaseSerializer`
        if isinstance(self.view, drf_rw_serializers_generics.GenericAPIView):
            return self.view.get_read_serializer()
        return t.cast(drf_serializers.BaseSerializer[_Mo_co], self._get_serializer())  # <-- need cast here as `_get_serializer` is untyped

Expected behavior
There should not be typing errors, maybe something should be updated on DRF-stubs side?

The link for stubs file: https://github.com/typeddjango/djangorestframework-stubs/blob/master/rest_framework-stubs/generics.pyi#L34