From efe37e09d5531b140ff7b98aa2f698552e9ecc52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kotal?= Date: Mon, 26 Dec 2022 17:16:24 +0100 Subject: [PATCH 1/5] add type hints --- adafruit_minimqtt/matcher.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/adafruit_minimqtt/matcher.py b/adafruit_minimqtt/matcher.py index 5d641ccb..10d784c7 100644 --- a/adafruit_minimqtt/matcher.py +++ b/adafruit_minimqtt/matcher.py @@ -11,6 +11,13 @@ * Author(s): Yoch (https://github.com/yoch) """ +try: + from typing import Dict, Any +except ImportError: + pass + +from collections.abc import Callable, Iterator + class MQTTMatcher: """Intended to manage topic filters including wildcards. @@ -27,14 +34,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: Callable[..., Any]) -> None: """Add a topic filter :key to the prefix tree and associate it to :value""" node = self._root @@ -42,7 +49,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) -> Callable[..., Any]: """Retrieve the value associated with some topic filter :key""" try: node = self._root @@ -54,7 +61,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 +78,13 @@ def __delitem__(self, key): break del parent.children[k] - def iter_match(self, topic): + def iter_match(self, topic: str) -> Iterator[Callable[..., Any]]: """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) -> Iterator[Callable[..., Any]]: if i == len(lst): if node.content is not None: yield node.content From 4f76bb1593dcedc10d84b02fdc7b21bb02be84c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kotal?= Date: Mon, 26 Dec 2022 17:17:14 +0100 Subject: [PATCH 2/5] apply isort --- adafruit_minimqtt/matcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/matcher.py b/adafruit_minimqtt/matcher.py index 10d784c7..4a546c65 100644 --- a/adafruit_minimqtt/matcher.py +++ b/adafruit_minimqtt/matcher.py @@ -12,7 +12,7 @@ """ try: - from typing import Dict, Any + from typing import Any, Dict except ImportError: pass From 20965e3f90ca03e80b74f9e02cc84055b9358a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kotal?= Date: Fri, 6 Jan 2023 20:52:34 +0100 Subject: [PATCH 3/5] use Callable, Iterator from typing --- adafruit_minimqtt/matcher.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_minimqtt/matcher.py b/adafruit_minimqtt/matcher.py index 4a546c65..83025918 100644 --- a/adafruit_minimqtt/matcher.py +++ b/adafruit_minimqtt/matcher.py @@ -12,12 +12,10 @@ """ try: - from typing import Any, Dict + from typing import Any, Dict, Callable, Iterator except ImportError: pass -from collections.abc import Callable, Iterator - class MQTTMatcher: """Intended to manage topic filters including wildcards. From 1ea551c2c06609003c78c982fe011e885e9ef104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Kotal?= Date: Fri, 6 Jan 2023 20:53:20 +0100 Subject: [PATCH 4/5] apply isort --- adafruit_minimqtt/matcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_minimqtt/matcher.py b/adafruit_minimqtt/matcher.py index 83025918..cf693044 100644 --- a/adafruit_minimqtt/matcher.py +++ b/adafruit_minimqtt/matcher.py @@ -12,7 +12,7 @@ """ try: - from typing import Any, Dict, Callable, Iterator + from typing import Any, Callable, Dict, Iterator except ImportError: pass From 414debb11879edbc77ecc7023a46ed2d8aa96288 Mon Sep 17 00:00:00 2001 From: Vladimir Kotal Date: Mon, 16 Jan 2023 18:28:59 +0100 Subject: [PATCH 5/5] remove Callable in type hints --- adafruit_minimqtt/matcher.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_minimqtt/matcher.py b/adafruit_minimqtt/matcher.py index cf693044..141a4f05 100644 --- a/adafruit_minimqtt/matcher.py +++ b/adafruit_minimqtt/matcher.py @@ -12,7 +12,7 @@ """ try: - from typing import Any, Callable, Dict, Iterator + from typing import Dict except ImportError: pass @@ -39,7 +39,7 @@ def __init__(self) -> None: def __init__(self) -> None: self._root = self.Node() - def __setitem__(self, key: str, value: Callable[..., Any]) -> None: + def __setitem__(self, key: str, value) -> None: """Add a topic filter :key to the prefix tree and associate it to :value""" node = self._root @@ -47,7 +47,7 @@ def __setitem__(self, key: str, value: Callable[..., Any]) -> None: node = node.children.setdefault(sym, self.Node()) node.content = value - def __getitem__(self, key: str) -> Callable[..., Any]: + def __getitem__(self, key: str): """Retrieve the value associated with some topic filter :key""" try: node = self._root @@ -76,13 +76,13 @@ def __delitem__(self, key: str) -> None: break del parent.children[k] - def iter_match(self, topic: str) -> Iterator[Callable[..., Any]]: + 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: MQTTMatcher.Node, i: int = 0) -> Iterator[Callable[..., Any]]: + def rec(node: MQTTMatcher.Node, i: int = 0): if i == len(lst): if node.content is not None: yield node.content