diff --git a/adafruit_minimqtt/matcher.py b/adafruit_minimqtt/matcher.py index 5d641ccb..141a4f05 100644 --- a/adafruit_minimqtt/matcher.py +++ b/adafruit_minimqtt/matcher.py @@ -11,6 +11,11 @@ * Author(s): Yoch (https://github.com/yoch) """ +try: + from typing import Dict +except ImportError: + pass + class MQTTMatcher: """Intended to manage topic filters including wildcards. @@ -27,14 +32,14 @@ class Node: __slots__ = "children", "content" - def __init__(self): - self.children = {} + def __init__(self) -> None: + self.children: Dict[str, MQTTMatcher.Node] = {} self.content = None - def __init__(self): + def __init__(self) -> None: self._root = self.Node() - def __setitem__(self, key, value): + def __setitem__(self, key: str, value) -> None: """Add a topic filter :key to the prefix tree and associate it to :value""" node = self._root @@ -42,7 +47,7 @@ def __setitem__(self, key, value): node = node.children.setdefault(sym, self.Node()) node.content = value - def __getitem__(self, key): + def __getitem__(self, key: str): """Retrieve the value associated with some topic filter :key""" try: node = self._root @@ -54,7 +59,7 @@ def __getitem__(self, key): except KeyError: raise KeyError(key) from None - def __delitem__(self, key): + def __delitem__(self, key: str) -> None: """Delete the value associated with some topic filter :key""" lst = [] try: @@ -71,13 +76,13 @@ def __delitem__(self, key): break del parent.children[k] - def iter_match(self, topic): + def iter_match(self, topic: str): """Return an iterator on all values associated with filters that match the :topic""" lst = topic.split("/") normal = not topic.startswith("$") - def rec(node, i=0): + def rec(node: MQTTMatcher.Node, i: int = 0): if i == len(lst): if node.content is not None: yield node.content