From 5ed7ca4071bb56c0f4582fb6f370edffa8917397 Mon Sep 17 00:00:00 2001 From: Bruno Tanabe Date: Wed, 9 Jul 2025 17:33:36 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20typing=20for=20`CustomORJS?= =?UTF-8?q?ONResponse.media=5Ftype`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit šŸ› 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. --- docs_src/custom_response/tutorial009c.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs_src/custom_response/tutorial009c.py b/docs_src/custom_response/tutorial009c.py index de6b6688e..9046dbeb4 100644 --- a/docs_src/custom_response/tutorial009c.py +++ b/docs_src/custom_response/tutorial009c.py @@ -1,4 +1,4 @@ -from typing import Any +from typing import Any, ClassVar import orjson from fastapi import FastAPI, Response @@ -7,7 +7,7 @@ app = FastAPI() class CustomORJSONResponse(Response): - media_type = "application/json" + media_type: ClassVar[str] = "application/json" def render(self, content: Any) -> bytes: assert orjson is not None, "orjson must be installed"