From 981ea9476e7719f4005b0701c68a176c01afa83f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janek=20Nouvertn=C3=A9?= Date: Sat, 15 Jun 2024 19:02:25 +0200 Subject: [PATCH] feat!: Remove deprecated `litestar.middleware.exceptions` module and deprecated params of internal `ExceptionHandlerMiddleware` (#3435) * refactor!: Remove deprecated `app` param of `Response.to_asgi_response` (#3393) * Remove 'app' parameter from `.to_asgi_response` * Remove debug param * Remove exception_handlers param * Remove litestar.middleware.exceptions --- docs/release-notes/whats-new-3.rst | 16 ++++++++ litestar/_asgi/utils.py | 2 +- .../_internal/exceptions/middleware.py | 41 +------------------ litestar/middleware/exceptions/__init__.py | 19 --------- .../middleware/exceptions/_debug_response.py | 20 --------- litestar/middleware/exceptions/middleware.py | 41 ------------------- litestar/security/session_auth/middleware.py | 2 +- .../test_exception_handler_middleware.py | 4 +- 8 files changed, 22 insertions(+), 123 deletions(-) delete mode 100644 litestar/middleware/exceptions/__init__.py delete mode 100644 litestar/middleware/exceptions/_debug_response.py delete mode 100644 litestar/middleware/exceptions/middleware.py diff --git a/docs/release-notes/whats-new-3.rst b/docs/release-notes/whats-new-3.rst index a34d6df294..6b64c7ca22 100644 --- a/docs/release-notes/whats-new-3.rst +++ b/docs/release-notes/whats-new-3.rst @@ -178,3 +178,19 @@ After: @get(handler_class=MyHTTPRouteHandler) async def handler() -> Any: ... + + +Deprecated ``app`` parameter for ``Response.to_asgi_response`` has been removed +------------------------------------------------------------------------------- + +The parameter ``app`` for :meth:`~response.Response.to_asgi_response` has been removed. +If you need access to the app instance inside a custom ``to_asgi_response`` method, +replace the usages of ``app`` with ``request.app``. + + +Removal of deprecated ``litestar.middleware.exceptions`` module and ``ExceptionHandlerMiddleware`` +-------------------------------------------------------------------------------------------------- + +The deprecated ``litestar.middleware.exceptions`` module and the +``ExceptionHandlerMiddleware`` have been removed. Since ``ExceptionHandlerMiddleware`` +has been applied automatically behind the scenes if necessary, no action is required. diff --git a/litestar/_asgi/utils.py b/litestar/_asgi/utils.py index 88dd7690e6..d9b6deb42a 100644 --- a/litestar/_asgi/utils.py +++ b/litestar/_asgi/utils.py @@ -21,7 +21,7 @@ def wrap_in_exception_handler(app: ASGIApp) -> ASGIApp: """ from litestar.middleware._internal.exceptions import ExceptionHandlerMiddleware - return ExceptionHandlerMiddleware(app=app, debug=None) + return ExceptionHandlerMiddleware(app=app) def get_route_handlers(route: BaseRoute) -> list[RouteHandlerType]: diff --git a/litestar/middleware/_internal/exceptions/middleware.py b/litestar/middleware/_internal/exceptions/middleware.py index 9d867c1720..69cf74097d 100644 --- a/litestar/middleware/_internal/exceptions/middleware.py +++ b/litestar/middleware/_internal/exceptions/middleware.py @@ -13,7 +13,6 @@ create_debug_response, ) from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR -from litestar.utils.deprecation import warn_deprecation from litestar.utils.empty import value_or_raise from litestar.utils.scope.state import ScopeState @@ -91,46 +90,14 @@ class ExceptionHandlerMiddleware: This used in multiple layers of Litestar. """ - def __init__( - self, app: ASGIApp, debug: bool | None, exception_handlers: ExceptionHandlersMap | None = None - ) -> None: + def __init__(self, app: ASGIApp) -> None: """Initialize ``ExceptionHandlerMiddleware``. Args: app: The ``next`` ASGI app to call. - debug: Whether ``debug`` mode is enabled. Deprecated. Debug mode will be inferred from the request scope - exception_handlers: A dictionary mapping status codes and/or exception types to handler functions. - - .. deprecated:: 2.0.0 - The ``debug`` parameter is deprecated. It will be inferred from the request scope - .. deprecated:: 2.9.0 - The ``exception_handlers`` parameter is deprecated. It will be inferred from the application or the - route handler. """ self.app = app - self.exception_handlers = exception_handlers - self.debug = debug - - if debug is not None: - warn_deprecation( - "2.0.0", - deprecated_name="debug", - kind="parameter", - info="Debug mode will be inferred from the request scope", - removal_in="3.0.0", - ) - - if exception_handlers is not None: - warn_deprecation( - "2.9.0", - deprecated_name="exception_handlers", - kind="parameter", - info="It will be inferred from the application or the route handler", - removal_in="3.0.0", - ) - - self._get_debug = self._get_debug_scope if debug is None else lambda *a: debug @staticmethod def _get_debug_scope(scope: Scope) -> bool: @@ -194,11 +161,7 @@ async def handle_request_exception( None. """ - exception_handlers = ( - value_or_raise(ScopeState.from_scope(scope).exception_handlers) - if self.exception_handlers is None - else self.exception_handlers - ) + exception_handlers = value_or_raise(ScopeState.from_scope(scope).exception_handlers) exception_handler = get_exception_handler(exception_handlers, exc) or self.default_http_exception_handler request: Request[Any, Any, Any] = litestar_app.request_class(scope=scope, receive=receive, send=send) response = exception_handler(request, exc) diff --git a/litestar/middleware/exceptions/__init__.py b/litestar/middleware/exceptions/__init__.py deleted file mode 100644 index 6bbe58e7ad..0000000000 --- a/litestar/middleware/exceptions/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -from __future__ import annotations - -from typing import Any - -from litestar.middleware._internal.exceptions import middleware -from litestar.utils.deprecation import warn_deprecation - - -def __getattr__(name: str) -> Any: - if name == "ExceptionHandlerMiddleware": - warn_deprecation( - version="2.9", - deprecated_name=name, - kind="class", - removal_in="3.0", - info="ExceptionHandlerMiddleware has been removed from the public API.", - ) - return middleware.ExceptionHandlerMiddleware - raise AttributeError(f"module {__name__} has no attribute {name}") diff --git a/litestar/middleware/exceptions/_debug_response.py b/litestar/middleware/exceptions/_debug_response.py deleted file mode 100644 index fa075dccfe..0000000000 --- a/litestar/middleware/exceptions/_debug_response.py +++ /dev/null @@ -1,20 +0,0 @@ -from __future__ import annotations - -from typing import Any - -from litestar.exceptions import responses -from litestar.utils.deprecation import warn_deprecation - - -def __getattr__(name: str) -> Any: - if name == "create_debug_response": - warn_deprecation( - version="2.9", - deprecated_name=name, - kind="function", - removal_in="3.0", - alternative="litestar.exceptions.responses.create_debug_response", - ) - return responses.create_debug_response - - raise AttributeError(f"module {__name__} has no attribute {name}") diff --git a/litestar/middleware/exceptions/middleware.py b/litestar/middleware/exceptions/middleware.py deleted file mode 100644 index 3d653ebac1..0000000000 --- a/litestar/middleware/exceptions/middleware.py +++ /dev/null @@ -1,41 +0,0 @@ -from __future__ import annotations - -from typing import Any - -from litestar.exceptions import responses -from litestar.middleware._internal.exceptions import middleware -from litestar.utils.deprecation import warn_deprecation - - -def __getattr__(name: str) -> Any: - if name == "ExceptionHandlerMiddleware": - warn_deprecation( - version="2.9", - deprecated_name=name, - kind="class", - removal_in="3.0", - info="ExceptionHandlerMiddleware has been removed from the public API.", - ) - return middleware.ExceptionHandlerMiddleware - - if name == "create_exception_response": - warn_deprecation( - version="2.9", - deprecated_name=name, - kind="function", - removal_in="3.0", - alternative="litestar.exceptions.responses.create_exception_response", - ) - return responses.create_exception_response - - if name == "ExceptionResponseContent": - warn_deprecation( - version="2.9", - deprecated_name=name, - kind="class", - removal_in="3.0", - alternative="litestar.exceptions.responses.ExceptionResponseContent", - ) - return responses.ExceptionResponseContent - - raise AttributeError(f"module {__name__} has no attribute {name}") diff --git a/litestar/security/session_auth/middleware.py b/litestar/security/session_auth/middleware.py index 1835dd31b5..1062fc7485 100644 --- a/litestar/security/session_auth/middleware.py +++ b/litestar/security/session_auth/middleware.py @@ -54,7 +54,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: scopes=self.config.scopes, retrieve_user_handler=self.config.retrieve_user_handler, # type: ignore[arg-type] ) - exception_middleware = ExceptionHandlerMiddleware(app=auth_middleware, debug=None) + exception_middleware = ExceptionHandlerMiddleware(app=auth_middleware) self.app = self.config.session_backend_config.middleware.middleware( app=exception_middleware, backend=self.config.session_backend, diff --git a/tests/unit/test_middleware/test_exception_handler_middleware.py b/tests/unit/test_middleware/test_exception_handler_middleware.py index a1540d353c..b664c57762 100644 --- a/tests/unit/test_middleware/test_exception_handler_middleware.py +++ b/tests/unit/test_middleware/test_exception_handler_middleware.py @@ -47,7 +47,7 @@ def app() -> Litestar: @pytest.fixture() def middleware() -> ExceptionHandlerMiddleware: - return ExceptionHandlerMiddleware(dummy_app, None) + return ExceptionHandlerMiddleware(dummy_app) @pytest.fixture() @@ -375,7 +375,7 @@ async def asgi_app(scope: Scope, receive: Receive, send: Send) -> None: await send(start_message) raise RuntimeError("Test exception") - mw = ExceptionHandlerMiddleware(asgi_app, None) + mw = ExceptionHandlerMiddleware(asgi_app) with pytest.raises(LitestarException): await mw(scope, mock_receive, mock_send)