Skip to content

Commit efe37e0

Browse files
committed
add type hints
1 parent 0e5bb0c commit efe37e0

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

adafruit_minimqtt/matcher.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
* Author(s): Yoch (https://github.com/yoch)
1212
"""
1313

14+
try:
15+
from typing import Dict, Any
16+
except ImportError:
17+
pass
18+
19+
from collections.abc import Callable, Iterator
20+
1421

1522
class MQTTMatcher:
1623
"""Intended to manage topic filters including wildcards.
@@ -27,22 +34,22 @@ class Node:
2734

2835
__slots__ = "children", "content"
2936

30-
def __init__(self):
31-
self.children = {}
37+
def __init__(self) -> None:
38+
self.children: Dict[str, MQTTMatcher.Node] = {}
3239
self.content = None
3340

34-
def __init__(self):
41+
def __init__(self) -> None:
3542
self._root = self.Node()
3643

37-
def __setitem__(self, key, value):
44+
def __setitem__(self, key: str, value: Callable[..., Any]) -> None:
3845
"""Add a topic filter :key to the prefix tree
3946
and associate it to :value"""
4047
node = self._root
4148
for sym in key.split("/"):
4249
node = node.children.setdefault(sym, self.Node())
4350
node.content = value
4451

45-
def __getitem__(self, key):
52+
def __getitem__(self, key: str) -> Callable[..., Any]:
4653
"""Retrieve the value associated with some topic filter :key"""
4754
try:
4855
node = self._root
@@ -54,7 +61,7 @@ def __getitem__(self, key):
5461
except KeyError:
5562
raise KeyError(key) from None
5663

57-
def __delitem__(self, key):
64+
def __delitem__(self, key: str) -> None:
5865
"""Delete the value associated with some topic filter :key"""
5966
lst = []
6067
try:
@@ -71,13 +78,13 @@ def __delitem__(self, key):
7178
break
7279
del parent.children[k]
7380

74-
def iter_match(self, topic):
81+
def iter_match(self, topic: str) -> Iterator[Callable[..., Any]]:
7582
"""Return an iterator on all values associated with filters
7683
that match the :topic"""
7784
lst = topic.split("/")
7885
normal = not topic.startswith("$")
7986

80-
def rec(node, i=0):
87+
def rec(node: MQTTMatcher.Node, i: int = 0) -> Iterator[Callable[..., Any]]:
8188
if i == len(lst):
8289
if node.content is not None:
8390
yield node.content

0 commit comments

Comments
 (0)