From 4c4401754e7f2edd48ad380b1d0d5cd04556943e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Todorovich?= Date: Mon, 3 Nov 2025 09:38:36 -0300 Subject: [PATCH] [FIX] fastapi: support both starlette < and >= `0.48` Starting from `starlette >= 0.48`, the `status.HTTP_422_UNPROCESSABLE_ENTITY` emits a deprecation warning, as it has been replaced by `status.HTTP_422_UNPROCESSABLE_CONTENT`. To be compatible with past and future versions, we simply use the integer value `422`, aligning also with the updated `fastapi` documentation: See: - https://github.com/fastapi/fastapi/pull/14077 --- fastapi/error_handlers.py | 5 ++++- fastapi/tests/test_fastapi.py | 4 +--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/fastapi/error_handlers.py b/fastapi/error_handlers.py index 192ef4064..52a32119a 100644 --- a/fastapi/error_handlers.py +++ b/fastapi/error_handlers.py @@ -29,7 +29,10 @@ def convert_exception_to_status_body(exc: Exception) -> tuple[int, dict]: status_code = exc.status_code details = exc.detail elif isinstance(exc, RequestValidationError): - status_code = status.HTTP_422_UNPROCESSABLE_CONTENT + # Use integer status code to supress starlette >= 0.48 deprecation warning + # See: https://github.com/fastapi/fastapi/pull/14077 + # status_code = status.HTTP_422_UNPROCESSABLE_CONTENT + status_code = 422 details = jsonable_encoder(exc.errors()) elif isinstance(exc, WebSocketRequestValidationError): status_code = status.WS_1008_POLICY_VIOLATION diff --git a/fastapi/tests/test_fastapi.py b/fastapi/tests/test_fastapi.py index c9eae09b8..35fa7ac99 100644 --- a/fastapi/tests/test_fastapi.py +++ b/fastapi/tests/test_fastapi.py @@ -156,9 +156,7 @@ def test_request_validation_error(self) -> None: route = "/fastapi_demo/demo/exception?exception_type=BAD&error_message=" response = self.url_open(route, timeout=200) mocked_commit.assert_not_called() - self.assertEqual( - response.status_code, status.HTTP_422_UNPROCESSABLE_CONTENT - ) + self.assertEqual(response.status_code, 422) def test_no_commit_on_exception(self) -> None: # this test check that the way we mock the cursor is working as expected