Skip to content

Commit 3cb893c

Browse files
authored
Add typing to IIDManager (#468)
1 parent 4398128 commit 3cb893c

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

pyhap/iid_manager.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
"""Module for the IIDManager class."""
22
import logging
3+
from typing import TYPE_CHECKING, Dict, Optional, Union
4+
5+
if TYPE_CHECKING:
6+
from .characteristic import Characteristic
7+
from .service import Service
8+
9+
ServiceOrCharType = Union[Service, Characteristic]
310

411
logger = logging.getLogger(__name__)
512

613

714
class IIDManager:
815
"""Maintains a mapping between Service/Characteristic objects and IIDs."""
916

10-
def __init__(self):
17+
def __init__(self) -> None:
1118
"""Initialize an empty instance."""
1219
self.counter = 0
13-
self.iids = {}
14-
self.objs = {}
20+
self.iids: Dict["ServiceOrCharType", int] = {}
21+
self.objs: Dict[int, "ServiceOrCharType"] = {}
1522

16-
def assign(self, obj):
23+
def assign(self, obj: "ServiceOrCharType") -> None:
1724
"""Assign an IID to given object. Print warning if already assigned.
1825
1926
:param obj: The object that will be assigned an IID.
@@ -32,23 +39,23 @@ def assign(self, obj):
3239
self.iids[obj] = iid
3340
self.objs[iid] = obj
3441

35-
def get_iid_for_obj(self, obj):
42+
def get_iid_for_obj(self, obj: "ServiceOrCharType") -> int:
3643
"""Get the IID for the given object.
3744
3845
Override this method to provide custom IID assignment.
3946
"""
4047
self.counter += 1
4148
return self.counter
4249

43-
def get_obj(self, iid):
50+
def get_obj(self, iid: int) -> "ServiceOrCharType":
4451
"""Get the object that is assigned the given IID."""
4552
return self.objs.get(iid)
4653

47-
def get_iid(self, obj):
54+
def get_iid(self, obj: "ServiceOrCharType") -> int:
4855
"""Get the IID assigned to the given object."""
4956
return self.iids.get(obj)
5057

51-
def remove_obj(self, obj):
58+
def remove_obj(self, obj: "ServiceOrCharType") -> Optional[int]:
5259
"""Remove an object from the IID list."""
5360
iid = self.iids.pop(obj, None)
5461
if iid is None:
@@ -57,7 +64,7 @@ def remove_obj(self, obj):
5764
del self.objs[iid]
5865
return iid
5966

60-
def remove_iid(self, iid):
67+
def remove_iid(self, iid: int) -> Optional["ServiceOrCharType"]:
6168
"""Remove an object with an IID from the IID list."""
6269
obj = self.objs.pop(iid, None)
6370
if obj is None:

0 commit comments

Comments
 (0)