From 82aeb3dca433d7236bd0820323166158854a62f9 Mon Sep 17 00:00:00 2001 From: Peter Schutt Date: Fri, 29 Sep 2023 10:28:55 +1000 Subject: [PATCH] fix: remove runtime assignment of type aliases to `Any`. (#2378) * fix: remove runtime assignment of type aliases to `Any`. This PR replaces this pattern: ```py from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from typing_extensions import TypeAlias from litestar import Thing ThingAlias: TypeAlias = Thing else: ThingAlias: TypeAlias = Any ``` My best guess is that this pattern was some workaround for circular dependencies before we introduced `__future__.annotations`. I believe these changes will be beneficial as: - the current pattern is non-standard (at least in my experience) - the current pattern causes the types to be documented as `Any` in the API docs. * Use modern generic types. * Refactor internal_types.py * Address review. De-stringizes type aliases where possible by importing components from typing at runtime. --- litestar/types/callable_types.py | 71 ++++++++++---------------------- litestar/types/internal_types.py | 29 ++++--------- 2 files changed, 28 insertions(+), 72 deletions(-) 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):