Skip to content

Commit

Permalink
remove resolve_signature_namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut committed Dec 13, 2024
1 parent 9dd4843 commit 36fb48d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 32 deletions.
31 changes: 8 additions & 23 deletions litestar/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class BaseRouteHandler:
"_resolved_data_dto",
"_parameter_field_definitions",
"_resolved_return_dto",
"_resolved_signature_namespace",
"_resolved_signature_model",
"_registered",
"dependencies",
Expand Down Expand Up @@ -128,7 +127,6 @@ def __init__(
self._resolved_data_dto: type[AbstractDTO] | None | EmptyType = Empty
self._parameter_field_definitions: dict[str, FieldDefinition] | EmptyType = Empty
self._resolved_return_dto: type[AbstractDTO] | None | EmptyType = Empty
self._resolved_signature_namespace: dict[str, Any] | EmptyType = Empty
self._resolved_signature_model: type[SignatureModel] | EmptyType = Empty
self._registered = False

Expand Down Expand Up @@ -171,7 +169,7 @@ def merge(self, other: Controller | Router) -> Self:
middleware=[*(other.middleware or ()), *self.middleware],
name=self.name,
opt={**other.opt, **self.opt},
signature_namespace={**other.signature_namespace, **self.signature_namespace},
signature_namespace=merge_signature_namespaces(other.signature_namespace, self.signature_namespace),
signature_types=other.signature_types,
type_decoders=(*(other.type_decoders or ()), *self.type_decoders),
type_encoders={**(other.type_encoders or {}), **self.type_encoders},
Expand Down Expand Up @@ -234,9 +232,7 @@ def parsed_fn_signature(self) -> ParsedSignature:
A ParsedSignature instance
"""
if self._parsed_fn_signature is Empty:
self._parsed_fn_signature = ParsedSignature.from_fn(
unwrap_partial(self.fn), self._resolve_signature_namespace()
)
self._parsed_fn_signature = ParsedSignature.from_fn(unwrap_partial(self.fn), self.signature_namespace)

return self._parsed_fn_signature

Expand Down Expand Up @@ -358,9 +354,7 @@ def _finalize_dependencies(self, app: Litestar | None = None):
signature, init_type_hints = plugin.get_typed_init(dependency)
provider.parsed_fn_signature = ParsedSignature.from_signature(signature, init_type_hints)
else:
provider.parsed_fn_signature = ParsedSignature.from_fn(
dependency, self._resolve_signature_namespace()
)
provider.parsed_fn_signature = ParsedSignature.from_fn(dependency, self.signature_namespace)

if not getattr(provider, "signature_model", None):
provider.signature_model = SignatureModel.create(
Expand All @@ -387,20 +381,11 @@ def resolve_exception_handlers(self) -> ExceptionHandlersMap:
self._check_registered()
return self.exception_handlers

def _resolve_signature_namespace(self) -> dict[str, Any]:
"""Build the route handler signature namespace dictionary by going from top to bottom.
When merging keys from multiple layers, if the same key is defined by multiple layers, the value from the
layer closest to the response handler will take precedence.
"""
if self._resolved_signature_namespace is Empty:
ns: dict[str, Any] = {}
for layer in self._ownership_layers:
merge_signature_namespaces(
signature_namespace=ns, additional_signature_namespace=layer.signature_namespace
)
self._resolved_signature_namespace = ns
return self._resolved_signature_namespace
@deprecated("3.0", removal_in="4.0", alternative=".signature_namespace attribute")
def resolve_signature_namespace(self) -> dict[str, Any]:
"""Build the route handler signature namespace dictionary by going from top to bottom"""
self._check_registered()
return self.signature_namespace

def resolve_data_dto(self, app: Litestar | None = None) -> type[AbstractDTO] | None:
"""Resolve the data_dto by starting from the route handler and moving up.
Expand Down
3 changes: 2 additions & 1 deletion litestar/handlers/http_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
from litestar.utils.predicates import is_async_callable
from litestar.utils.predicates import is_class_and_subclass
from litestar.utils.scope.state import ScopeState
from litestar.utils.signature import merge_signature_namespaces
from litestar.utils.warnings import warn_implicit_sync_to_thread, warn_sync_to_thread_with_async_callable

if TYPE_CHECKING:
Expand Down Expand Up @@ -389,7 +390,7 @@ def merge(self, other: Controller | Router) -> HTTPRouteHandler:
middleware=[*(other.middleware or ()), *self.middleware],
name=self.name,
opt={**(other.opt or {}), **(self.opt or {})},
signature_namespace={**other.signature_namespace, **self.signature_namespace},
signature_namespace=merge_signature_namespaces(other.signature_namespace, self.signature_namespace),
# signature_types=getattr(other, "signature_types", None),
type_decoders=(*(other.type_decoders or ()), *self.type_decoders),
type_encoders={**(other.type_encoders or {}), **self.type_encoders},
Expand Down
12 changes: 6 additions & 6 deletions litestar/handlers/websocket_handlers/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
TypeEncodersMap,
)
from litestar.utils import ensure_async_callable, join_paths
from litestar.utils.signature import ParsedSignature, get_fn_type_hints
from litestar.utils.signature import ParsedSignature, get_fn_type_hints, merge_signature_namespaces

from ._utils import (
ListenerHandler,
Expand Down Expand Up @@ -247,7 +247,7 @@ def merge(self, other: Controller | Router) -> WebsocketListenerRouteHandler:
middleware=[*(other.middleware or ()), *self.middleware],
name=self.name,
opt={**(other.opt or {}), **(self.opt or {})},
signature_namespace={**other.signature_namespace, **self.signature_namespace},
signature_namespace=merge_signature_namespaces(other.signature_namespace, self.signature_namespace),
signature_types=getattr(other, "signature_types", None),
type_decoders=(*(other.type_decoders or ()), *self.type_decoders),
type_encoders={**(other.type_encoders or {}), **self.type_encoders},
Expand All @@ -266,7 +266,7 @@ def on_registration(self, route: BaseRoute, app: Litestar) -> None:
super().on_registration(route, app)

def _prepare_fn(self) -> ListenerHandler:
parsed_signature = ParsedSignature.from_fn(self.fn, self._resolve_signature_namespace())
parsed_signature = ParsedSignature.from_fn(self.fn, self.signature_namespace)

if "data" not in parsed_signature.parameters:
raise ImproperlyConfiguredException("Websocket listeners must accept a 'data' parameter")
Expand All @@ -282,13 +282,13 @@ def _prepare_fn(self) -> ListenerHandler:
self._parsed_fn_signature = ParsedSignature.from_signature(
create_handler_signature(parsed_signature.original_signature),
fn_type_hints={
**get_fn_type_hints(self.fn, namespace=self._resolve_signature_namespace()),
**get_fn_type_hints(ListenerHandler.__call__, namespace=self._resolve_signature_namespace()),
**get_fn_type_hints(self.fn, namespace=self.signature_namespace),
**get_fn_type_hints(ListenerHandler.__call__, namespace=self.signature_namespace),
},
)

return ListenerHandler(
listener=self, fn=self.fn, parsed_signature=parsed_signature, namespace=self._resolve_signature_namespace()
listener=self, fn=self.fn, parsed_signature=parsed_signature, namespace=self.signature_namespace
)

def _validate_handler_function(self, app: Litestar | None = None) -> None:
Expand Down
3 changes: 2 additions & 1 deletion litestar/handlers/websocket_handlers/route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from litestar.utils import join_paths
from litestar.utils.empty import value_or_default
from litestar.utils.predicates import is_async_callable
from litestar.utils.signature import merge_signature_namespaces

if TYPE_CHECKING:
from litestar import Controller, Router, Litestar
Expand Down Expand Up @@ -92,7 +93,7 @@ def merge(self, other: Controller | Router) -> WebsocketRouteHandler:
middleware=[*(other.middleware or ()), *self.middleware],
name=self.name,
opt={**(other.opt or {}), **(self.opt or {})},
signature_namespace={**other.signature_namespace, **self.signature_namespace},
signature_namespace=merge_signature_namespaces(other.signature_namespace, self.signature_namespace),
signature_types=getattr(other, "signature_types", None),
type_decoders=(*(other.type_decoders or ()), *self.type_decoders),
type_encoders={**(other.type_encoders or {}), **self.type_encoders},
Expand Down
2 changes: 1 addition & 1 deletion litestar/handlers/websocket_handlers/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def on_registration(self, route: BaseRoute, app: Litestar) -> None:
self._ws_stream_options = self.opt["stream_options"]

parsed_handler_signature = parsed_stream_fn_signature = ParsedSignature.from_fn(
self.fn, self._resolve_signature_namespace()
self.fn, self.signature_namespace
)

if not parsed_stream_fn_signature.return_type.is_subclass_of(AsyncGenerator):
Expand Down

0 comments on commit 36fb48d

Please sign in to comment.