Browse Source

🐛 Fix typing for `CustomORJSONResponse.media_type`

🐛 Fix typing for `CustomORJSONResponse.media_type`

* Declare `media_type` as `ClassVar[str]`, importing `ClassVar` from `typing`.
* Treats the attribute as a class-level constant, matching FastAPI’s built-in response classes and silencing mypy/ruff strict-mode errors.
* Prevents rare `AttributeError` scenarios when `Response` subclasses are instantiated before the attribute is set.
* No runtime behaviour change; purely a correctness/compatibility fix.
pull/13879/head
Bruno Tanabe 1 month ago
committed by GitHub
parent
commit
5ed7ca4071
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      docs_src/custom_response/tutorial009c.py

4
docs_src/custom_response/tutorial009c.py

@ -1,4 +1,4 @@
from typing import Any from typing import Any, ClassVar
import orjson import orjson
from fastapi import FastAPI, Response from fastapi import FastAPI, Response
@ -7,7 +7,7 @@ app = FastAPI()
class CustomORJSONResponse(Response): class CustomORJSONResponse(Response):
media_type = "application/json" media_type: ClassVar[str] = "application/json"
def render(self, content: Any) -> bytes: def render(self, content: Any) -> bytes:
assert orjson is not None, "orjson must be installed" assert orjson is not None, "orjson must be installed"

Loading…
Cancel
Save