diff --git a/litestar/types/callable_types.py b/litestar/types/callable_types.py index e575e46bd0..d56c12f424 100644 --- a/litestar/types/callable_types.py +++ b/litestar/types/callable_types.py @@ -1,15 +1,6 @@ from __future__ import annotations -from typing import ( - TYPE_CHECKING, - Any, - AsyncGenerator, - Awaitable, - Callable, - Generator, - List, - Union, -) +from typing import TYPE_CHECKING, Any, AsyncGenerator, Awaitable, Callable, Generator if TYPE_CHECKING: from typing_extensions import TypeAlias @@ -25,43 +16,23 @@ from litestar.types.internal_types import LitestarType, PathParameterDefinition from litestar.types.protocols import Logger - AfterExceptionHookHandler: TypeAlias = Callable[[Exception, Scope], SyncOrAsyncUnion[None]] - AfterRequestHookHandler: TypeAlias = Union[ - Callable[[ASGIApp], SyncOrAsyncUnion[ASGIApp]], Callable[[Response], SyncOrAsyncUnion[Response]] - ] - AfterResponseHookHandler: TypeAlias = Callable[[Request], SyncOrAsyncUnion[None]] - AsyncAnyCallable: TypeAlias = Callable[..., Awaitable[Any]] - AnyCallable: TypeAlias = Callable[..., Any] - AnyGenerator: TypeAlias = Union[Generator[Any, Any, Any], AsyncGenerator[Any, Any]] - BeforeMessageSendHookHandler: TypeAlias = Callable[[Message, Scope], SyncOrAsyncUnion[None]] - BeforeRequestHookHandler: TypeAlias = Callable[[Request], Union[Any, Awaitable[Any]]] - CacheKeyBuilder: TypeAlias = Callable[[Request], str] - ExceptionHandler: TypeAlias = Callable[[Request, Exception], Response] - ExceptionLoggingHandler: TypeAlias = Callable[[Logger, Scope, List[str]], None] - GetLogger: TypeAlias = Callable[..., Logger] - Guard: TypeAlias = Callable[[ASGIConnection, BaseRouteHandler], SyncOrAsyncUnion[None]] - LifespanHook: TypeAlias = Union[ - Callable[[LitestarType], SyncOrAsyncUnion[Any]], - Callable[[], SyncOrAsyncUnion[Any]], - ] - OnAppInitHandler: TypeAlias = Callable[[AppConfig], AppConfig] - OperationIDCreator: TypeAlias = Callable[[HTTPRouteHandler, Method, List[Union[str, PathParameterDefinition]]], str] - Serializer: TypeAlias = Callable[[Any], Any] -else: - AfterExceptionHookHandler: TypeAlias = Any - AfterRequestHookHandler: TypeAlias = Any - AfterResponseHookHandler: TypeAlias = Any - AsyncAnyCallable: TypeAlias = Any - AnyCallable: TypeAlias = Any - AnyGenerator: TypeAlias = Any - BeforeMessageSendHookHandler: TypeAlias = Any - BeforeRequestHookHandler: TypeAlias = Any - CacheKeyBuilder: TypeAlias = Any - ExceptionHandler: TypeAlias = Any - ExceptionLoggingHandler: TypeAlias = Any - GetLogger: TypeAlias = Any - Guard: TypeAlias = Any - LifespanHook: TypeAlias = Any - OnAppInitHandler: TypeAlias = Any - OperationIDCreator: TypeAlias = Any - Serializer: TypeAlias = Any + +AfterExceptionHookHandler: TypeAlias = "Callable[[Exception, Scope], SyncOrAsyncUnion[None]]" +AfterRequestHookHandler: TypeAlias = ( + "Callable[[ASGIApp], SyncOrAsyncUnion[ASGIApp]] | Callable[[Response], SyncOrAsyncUnion[Response]]" +) +AfterResponseHookHandler: TypeAlias = "Callable[[Request], SyncOrAsyncUnion[None]]" +AsyncAnyCallable: TypeAlias = Callable[..., Awaitable[Any]] +AnyCallable: TypeAlias = Callable[..., Any] +AnyGenerator: TypeAlias = "Generator[Any, Any, Any] | AsyncGenerator[Any, Any]" +BeforeMessageSendHookHandler: TypeAlias = "Callable[[Message, Scope], SyncOrAsyncUnion[None]]" +BeforeRequestHookHandler: TypeAlias = "Callable[[Request], Any | Awaitable[Any]]" +CacheKeyBuilder: TypeAlias = "Callable[[Request], str]" +ExceptionHandler: TypeAlias = "Callable[[Request, Exception], Response]" +ExceptionLoggingHandler: TypeAlias = "Callable[[Logger, Scope, list[str]], None]" +GetLogger: TypeAlias = "Callable[..., Logger]" +Guard: TypeAlias = "Callable[[ASGIConnection, BaseRouteHandler], SyncOrAsyncUnion[None]]" +LifespanHook: TypeAlias = "Callable[[LitestarType], SyncOrAsyncUnion[Any]] | Callable[[], SyncOrAsyncUnion[Any]]" +OnAppInitHandler: TypeAlias = "Callable[[AppConfig], AppConfig]" +OperationIDCreator: TypeAlias = "Callable[[HTTPRouteHandler, Method, list[str | PathParameterDefinition]], str]" +Serializer: TypeAlias = Callable[[Any], Any] diff --git a/litestar/types/internal_types.py b/litestar/types/internal_types.py index c355dd82ca..cf5d95a485 100644 --- a/litestar/types/internal_types.py +++ b/litestar/types/internal_types.py @@ -1,14 +1,6 @@ from __future__ import annotations -from typing import ( - TYPE_CHECKING, - Any, - Callable, - Dict, - Literal, - NamedTuple, - Union, -) +from typing import TYPE_CHECKING, Any, Callable, Literal, NamedTuple __all__ = ( "ControllerRouterHandler", @@ -34,19 +26,12 @@ from litestar.router import Router from litestar.types import Method - ReservedKwargs: TypeAlias = Literal["request", "socket", "headers", "query", "cookies", "state", "data"] - LitestarType: TypeAlias = Litestar - RouteHandlerType: TypeAlias = Union[HTTPRouteHandler, WebsocketRouteHandler, ASGIRouteHandler] - ResponseType: TypeAlias = type[Response] - ControllerRouterHandler: TypeAlias = Union[type[Controller], RouteHandlerType, Router, Callable[..., Any]] - RouteHandlerMapItem: TypeAlias = Dict[Union[Method, Literal["websocket", "asgi"]], RouteHandlerType] -else: - ReservedKwargs: TypeAlias = Any - LitestarType: TypeAlias = Any - RouteHandlerType: TypeAlias = Any - ResponseType: TypeAlias = Any - ControllerRouterHandler: TypeAlias = Any - RouteHandlerMapItem: TypeAlias = Any +ReservedKwargs: TypeAlias = Literal["request", "socket", "headers", "query", "cookies", "state", "data"] +LitestarType: TypeAlias = "Litestar" +RouteHandlerType: TypeAlias = "HTTPRouteHandler | WebsocketRouteHandler | ASGIRouteHandler" +ResponseType: TypeAlias = "type[Response]" +ControllerRouterHandler: TypeAlias = "type[Controller] | RouteHandlerType | Router | Callable[..., Any]" +RouteHandlerMapItem: TypeAlias = 'dict[Method | Literal["websocket", "asgi"], RouteHandlerType]' class PathParameterDefinition(NamedTuple):