Skip to content

Commit

Permalink
remove resolve_websocket_class
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut committed Dec 14, 2024
1 parent 8d5111c commit f4f8dc6
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
3 changes: 1 addition & 2 deletions litestar/handlers/websocket_handlers/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ def __init__(
self.on_disconnect = ensure_async_callable(on_disconnect) if on_disconnect else None
self.type_decoders = type_decoders
self.type_encoders = type_encoders
self.websocket_class = websocket_class

listener_dependencies = dict(dependencies or {})

Expand Down Expand Up @@ -251,7 +250,7 @@ def merge(self, other: Controller | Router) -> WebsocketListenerRouteHandler:
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},
websocket_class=self.websocket_class or other.websocket_class,
websocket_class=self._websocket_class or other.websocket_class,
parameters={**other.parameters, **self.parameters},
receive_mode=self._receive_mode,
send_mode=self._send_mode,
Expand Down
19 changes: 10 additions & 9 deletions litestar/handlers/websocket_handlers/route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
from litestar.connection import WebSocket
from litestar.exceptions import ImproperlyConfiguredException
from litestar.handlers import BaseRouteHandler
from litestar.plugins import PluginRegistry
from litestar.types import AsyncAnyCallable
from litestar.types import Empty
from litestar.types import ParametersMap
from litestar.types.builtin_types import NoneType
from litestar.utils import join_paths
from litestar.utils import join_paths, deprecated
from litestar.utils.empty import value_or_default
from litestar.utils.predicates import is_async_callable
from litestar.utils.signature import merge_signature_namespaces
Expand All @@ -24,7 +23,7 @@


class WebsocketRouteHandler(BaseRouteHandler):
__slots__ = ("_kwargs_model", "websocket_class")
__slots__ = ("_kwargs_model", "_websocket_class")

def __init__(
self,
Expand Down Expand Up @@ -64,7 +63,7 @@ def __init__(
default websocket class.
**kwargs: Any additional kwarg - will be set in the opt dictionary.
"""
self.websocket_class = websocket_class
self._websocket_class = websocket_class
self._kwargs_model: KwargsModel | EmptyType = Empty

super().__init__(
Expand Down Expand Up @@ -97,10 +96,11 @@ def merge(self, other: Controller | Router) -> WebsocketRouteHandler:
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},
websocket_class=self.websocket_class or other.websocket_class,
websocket_class=self._websocket_class or other.websocket_class,
parameters={**other.parameters, **self.parameters},
)

@deprecated("3.0", removal_in="4.0", alternative=".websocket_class property")
def resolve_websocket_class(self) -> type[WebSocket]:
"""Return the closest custom WebSocket class in the owner graph or the default Websocket class.
Expand All @@ -109,10 +109,11 @@ def resolve_websocket_class(self) -> type[WebSocket]:
Returns:
The default :class:`WebSocket <.connection.WebSocket>` class for the route handler.
"""
return next(
(layer.websocket_class for layer in reversed(self._ownership_layers) if layer.websocket_class is not None),
WebSocket,
)
return self.websocket_class

Check warning on line 112 in litestar/handlers/websocket_handlers/route_handler.py

View check run for this annotation

Codecov / codecov/patch

litestar/handlers/websocket_handlers/route_handler.py#L112

Added line #L112 was not covered by tests

@property
def websocket_class(self) -> type[WebSocket]:
return self._websocket_class or WebSocket

def _validate_handler_function(self, app: Litestar | None = None) -> None:
"""Validate the route handler function once it's set by inspecting its return annotations."""
Expand Down
2 changes: 1 addition & 1 deletion litestar/routes/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ async def handle(self, scope: WebSocketScope, receive: Receive, send: Send) -> N
Returns:
None
"""
socket = self.route_handler.resolve_websocket_class()(scope=scope, receive=receive, send=send)
socket = self.route_handler.websocket_class(scope=scope, receive=receive, send=send)
await self.route_handler.handle(connection=socket)
4 changes: 2 additions & 2 deletions tests/unit/test_websocket_class_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def handler(self, data: str) -> None:

route_handler = app.routes[0].route_handler # type: ignore[union-attr]

websocket_class = route_handler.resolve_websocket_class() # type: ignore[union-attr]
websocket_class = route_handler.websocket_class # type: ignore[union-attr]
assert websocket_class is expected


Expand Down Expand Up @@ -93,5 +93,5 @@ def on_receive(self, data: str) -> str: # pyright: ignore
app = Litestar(route_handlers=[router], websocket_class=app_websocket_class)
route_handler = app.routes[0].route_handler # type: ignore[union-attr]

websocket_class = route_handler.resolve_websocket_class() # type: ignore[union-attr]
websocket_class = route_handler.websocket_class # type: ignore[union-attr]
assert websocket_class is expected

0 comments on commit f4f8dc6

Please sign in to comment.