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

[Bug]: DjangoOpenAPIResponse no longer type compatible with unmarshal_response #938

Open
jceipek opened this issue Nov 12, 2024 · 1 comment
Labels
kind/bug Indicates an issue

Comments

@jceipek
Copy link

jceipek commented Nov 12, 2024

Actual Behavior

There is a type error here when using werkzeug Version 3.1.2 or newer:

from django.conf import settings
from openapi_core import unmarshal_response
from openapi_core.contrib.django import DjangoOpenAPIRequest, DjangoOpenAPIResponse

def test(openapi_request: DjangoOpenAPIRequest, openapi_response: DjangoOpenAPIResponse):
        return unmarshal_response(
            request=openapi_request,
            response=openapi_response, # type error on this line
            spec=settings.OPENAPI_SPEC,
        )

Expected Behavior

No type errors in the provided sample code.

Steps to Reproduce

Typecheck the provided sample code using mypy.

OpenAPI Core Version

3.0.0

OpenAPI Core Integration

django

Affected Area(s)

casting, dependencies, unmarshalling

References

Anything else we need to know?

werkzeug Version 3.1.2 changed the definition of Headers because it isn't technically a MutableMapping.

The Response protocol in openapi_core here has Mapping[str, Any]:

@property
def headers(self) -> Mapping[str, Any]: ...

DjangoOpenAPIResponse has headers typed as Headers from werkzeug:

@property
def headers(self) -> Headers:
return Headers(self.response.headers.items())

Since these are no longer compatible, unmarshal_response can no longer accept DjangoOpenAPIResponse as a response parameter without failing typechecking.

Would you like to implement a fix?

None

@andersk
Copy link
Contributor

andersk commented Nov 19, 2024

MockResponse is also incompatible with Response for the same reason.

self.headers = Headers(headers or {})

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

No branches or pull requests

2 participants