Skip to content

Commit 1086033

Browse files
committed
🚨 Fixing mypy's complaints
1 parent bbb1c12 commit 1086033

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

‎broadcaster/_base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
from typing import TYPE_CHECKING, Any, AsyncGenerator, AsyncIterator, cast
66
from urllib.parse import urlparse
77

8+
from pydantic import BaseModel
9+
810
if TYPE_CHECKING: # pragma: no cover
911
from broadcaster.backends.base import BroadcastBackend
1012

1113

1214
class Event:
13-
def __init__(self, channel: str, message: str) -> None:
15+
def __init__(self, channel: str, message: str | BaseModel) -> None:
1416
self.channel = channel
1517
self.message = message
1618

‎broadcaster/backends/redis.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,34 @@ async def next_published(self) -> Event:
116116
class RedisPydanticStreamBackend(RedisStreamBackend):
117117
"""Redis Stream backend for broadcasting messages using Pydantic models."""
118118

119-
def __init__(self: typing.Self, url: str) -> None:
119+
def __init__(self, url: str) -> None:
120120
"""Create a new Redis Stream backend."""
121121
url = url.replace("redis-pydantic-stream", "redis", 1)
122122
self.streams: dict[bytes | str | memoryview, int | bytes | str | memoryview] = {}
123123
self._ready = asyncio.Event()
124124
self._producer = redis.Redis.from_url(url)
125125
self._consumer = redis.Redis.from_url(url)
126-
self._module_cache: dict[str, type(BaseModel)] = {}
126+
self._module_cache: dict[str, type[BaseModel]] = {}
127127

128-
def _build_module_cache(self: typing.Self) -> None:
128+
def _build_module_cache(self) -> None:
129129
"""Build a cache of Pydantic models."""
130130
modules = list(sys.modules.keys())
131131
for module_name in modules:
132132
for _, obj in inspect.getmembers(sys.modules[module_name]):
133133
if inspect.isclass(obj) and issubclass(obj, BaseModel):
134134
self._module_cache[obj.__name__] = obj
135135

136-
async def publish(self: typing.Self, channel: str, message: BaseModel) -> None:
136+
async def publish(self, channel: str, message: BaseModel) -> None:
137137
"""Publish a message to a channel."""
138138
msg_type: str = message.__class__.__name__
139+
140+
if msg_type not in self._module_cache:
141+
self._module_cache[msg_type] = message.__class__
142+
139143
message_json: str = message.model_dump_json()
140144
await self._producer.xadd(channel, {"msg_type": msg_type, "message": message_json})
141145

142-
async def wait_for_messages(self: typing.Self) -> list[StreamMessageType]:
146+
async def wait_for_messages(self) -> list[StreamMessageType]:
143147
"""Wait for messages to be published."""
144148
await self._ready.wait()
145149
self._build_module_cache()
@@ -148,7 +152,7 @@ async def wait_for_messages(self: typing.Self) -> list[StreamMessageType]:
148152
messages = await self._consumer.xread(self.streams, count=1, block=100)
149153
return messages
150154

151-
async def next_published(self: typing.Self) -> Event | None:
155+
async def next_published(self) -> Event:
152156
"""Get the next published message."""
153157
messages = await self.wait_for_messages()
154158
stream, events = messages[0]
@@ -160,7 +164,7 @@ async def next_published(self: typing.Self) -> Event | None:
160164
if msg_type in self._module_cache:
161165
message_obj = self._module_cache[msg_type].model_validate_json(message_data)
162166
if not message_obj:
163-
return None
167+
return Event(stream.decode("utf-8"), message_data)
164168
return Event(
165169
channel=stream.decode("utf-8"),
166170
message=message_obj,

0 commit comments

Comments
 (0)