Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistency in typing DRF-Stubs in AutoSchema regarding Serializer #1249

Open
sshishov opened this issue May 30, 2024 · 0 comments
Open

Inconsistency in typing DRF-Stubs in AutoSchema regarding Serializer #1249

sshishov opened this issue May 30, 2024 · 0 comments

Comments

@sshishov
Copy link

sshishov commented May 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant