Skip to content

Question: Using handler decorators on methods #3173

Answered by provinzkraut
byte-bot-app[bot] bot asked this question in Q&A
Discussion options

You must be logged in to vote

The issue here is that at the time the @websocket decorator is passed Something.handler, the function is still unbound (i.e. the self argument won't receive the instance you created automatically, because it doesn't exist yet).

You can fix this by changing how you pass the handlers:

from litestar import Litestar, WebSocket, websocket

class Something:
    async def handler(socket: WebSocket) -> None:
        await socket.accept()

app = Litestar([websocket("/ws")(Something().handler)])

Litestar itself does something similar in the ChannelsPlugin:

if self._arbitrary_channels_allowed:
path = self._handler_root…

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by JacobCoffee
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
Question This is a question and further information is requested
3 participants
Converted from issue

This discussion was converted from issue #3146 on March 05, 2024 17:08.