Will Django Channels ever have type hints? #1819
Replies: 7 comments 1 reply
-
Maybe... There was a PR working on it here: #1299 Given that maybe of the annotations are not very pretty, and that type hints have currently been rejected from Django, it's not a priority. |
Beta Was this translation helpful? Give feedback.
-
Perhaps the types already defined in Examples: class WebSocketConnectEvent(TypedDict):
type: Literal["websocket.connect"]
class WebSocketAcceptEvent(TypedDict):
type: Literal["websocket.accept"]
subprotocol: Optional[str]
headers: Iterable[Tuple[bytes, bytes]] |
Beta Was this translation helpful? Give feedback.
-
For those new to Channels looking for an example of how to type hint your own code, I updated the tutorial final example with type annotations and also switched to the The example below should work on Python 3.10+ (add from typing import Any, Literal, TypedDict
from asgiref.typing import WebSocketScope
from channels.generic.websocket import AsyncJsonWebsocketConsumer
class UrlRoute(TypedDict):
args: tuple[str | int, ...]
kwargs: dict[str, str | int]
class ChannelsWebSocketScope(WebSocketScope):
url_route: UrlRoute
class ChatMessage(TypedDict):
type: Literal["chat.message"]
message: str
class Message(TypedDict):
message: str
class ChatConsumer(AsyncJsonWebsocketConsumer):
async def connect(self) -> None:
scope: ChannelsWebSocketScope = self.scope
self.room_name = scope["url_route"]["kwargs"]["room_name"]
self.room_group_name = f"chat_{self.room_name}"
# Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
await self.accept()
async def disconnect(self, close_code: int) -> None:
# Leave room group
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
# Receive message from WebSocket
async def receive_json(self, content: Message, **kwargs: Any) -> None:
message: ChatMessage = {
"type": "chat.message",
"message": content["message"]
}
# Send message to room group
await self.channel_layer.group_send(self.room_group_name, message)
# Receive message from room group
async def chat_message(self, event: ChatMessage) -> None:
message: Message = {
"message": event["message"],
}
# Send message to WebSocket
await self.send_json(content=message) |
Beta Was this translation helpful? Give feedback.
-
Another use case: |
Beta Was this translation helpful? Give feedback.
-
Any news on adding typing? It would be great if |
Beta Was this translation helpful? Give feedback.
-
Bumping this also, I just enabled type checking after not seeing type hints in VS Code and spent longer than I wanted trying to figure out why such a core library with a very large community around it would not have type hints implemented... but I guess it don't. Not a big issue but definitely would be appreciated 👍 |
Beta Was this translation helpful? Give feedback.
-
Bumps and "any news" comments are not helpful. They're just noise on the repo. I'm going to lock this thread to avoid that. It's understood that folks who are using static typing in Python want this, and that folks who aren't don't want the noise. Currently following Django until there's a change on the policy on type hints there. |
Beta Was this translation helpful? Give feedback.
-
Will Django Channels ever have type hints?
Beta Was this translation helpful? Give feedback.
All reactions