Skip to content

Commit

Permalink
fix: remove runtime assignment of type aliases to Any. (#2378)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
peterschutt authored Sep 29, 2023
1 parent 1dca41f commit 82aeb3d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 72 deletions.
71 changes: 21 additions & 50 deletions litestar/types/callable_types.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
29 changes: 7 additions & 22 deletions litestar/types/internal_types.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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):
Expand Down

0 comments on commit 82aeb3d

Please sign in to comment.