11import logging
2+ import re
23from datetime import timedelta
34from typing import Any , Protocol , overload
45
@@ -282,24 +283,25 @@ def decorator(fn: EventHandlerFnT) -> EventHandlerFnT:
282283
283284 def _topic_matches_subscriptions (self , topic : str ) -> bool :
284285 """Check if a topic matches any of our subscribed patterns."""
285- import re as _re
286-
287286 for pattern in self ._subscribed_patterns :
288287 parts = pattern .split ("/" )
289288 regex_parts : list [str ] = []
290289 for i , part in enumerate (parts ):
291290 if part == "#" :
292- regex = "^" + "/" .join (regex_parts ) + "(/.*)?$"
293- if _re .match (regex , topic ):
291+ if regex_parts :
292+ regex = "^" + "/" .join (regex_parts ) + "(/.*)?$"
293+ else :
294+ regex = "^.*$"
295+ if re .match (regex , topic ):
294296 return True
295297 break
296298 elif part == "+" :
297299 regex_parts .append ("[^/]+" )
298300 else :
299- regex_parts .append (_re .escape (part ))
301+ regex_parts .append (re .escape (part ))
300302 else :
301303 regex = "^" + "/" .join (regex_parts ) + "$"
302- if _re .match (regex , topic ):
304+ if re .match (regex , topic ):
303305 return True
304306 return False
305307
@@ -312,23 +314,24 @@ async def _handle_event(self, params: types.EventParams) -> None:
312314 return
313315
314316 if self ._event_topic_filter is not None :
315- import re as _re
316-
317317 parts = self ._event_topic_filter .split ("/" )
318318 regex_parts : list [str ] = []
319319 matched = False
320320 for i , part in enumerate (parts ):
321321 if part == "#" :
322- regex = "^" + "/" .join (regex_parts ) + "(/.*)?$"
323- matched = bool (_re .match (regex , params .topic ))
322+ if regex_parts :
323+ regex = "^" + "/" .join (regex_parts ) + "(/.*)?$"
324+ else :
325+ regex = "^.*$"
326+ matched = bool (re .match (regex , params .topic ))
324327 break
325328 elif part == "+" :
326329 regex_parts .append ("[^/]+" )
327330 else :
328- regex_parts .append (_re .escape (part ))
331+ regex_parts .append (re .escape (part ))
329332 else :
330333 regex = "^" + "/" .join (regex_parts ) + "$"
331- matched = bool (_re .match (regex , params .topic ))
334+ matched = bool (re .match (regex , params .topic ))
332335 if not matched :
333336 return
334337
0 commit comments