Skip to content

Commit 1ef0a13

Browse files
authored
Merge pull request #902 from jackburridge/update-reasoncodes-types
refactor: add types to reason codes
2 parents 73d2cae + e3f1780 commit 1ef0a13

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

src/paho/mqtt/reasoncodes.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def __init__(self, packetType: int, aName: str = "Success", identifier: int = -1
130130
self.value = identifier
131131
self.getName() # check it's good
132132

133-
def __getName__(self, packetType, identifier):
133+
def __getName__(self, packetType: int, identifier: int) -> str:
134134
"""
135135
Get the reason code string name for a specific identifier.
136136
The name can vary by packet type for the same identifier, which
@@ -146,7 +146,7 @@ def __getName__(self, packetType, identifier):
146146
raise ValueError(f"Expected exactly one name, found {namelist!r}")
147147
return namelist[0]
148148

149-
def getId(self, name):
149+
def getId(self, name: str) -> int:
150150
"""
151151
Get the numeric id corresponding to a reason code name.
152152
@@ -159,20 +159,20 @@ def getId(self, name):
159159
return code
160160
raise KeyError(f"Reason code name not found: {name}")
161161

162-
def set(self, name):
162+
def set(self, name: str) -> None:
163163
self.value = self.getId(name)
164164

165-
def unpack(self, buffer):
165+
def unpack(self, buffer: bytearray) -> int:
166166
c = buffer[0]
167167
name = self.__getName__(self.packetType, c)
168168
self.value = self.getId(name)
169169
return 1
170170

171-
def getName(self):
171+
def getName(self) -> str:
172172
"""Returns the reason code name corresponding to the numeric value which is set."""
173173
return self.__getName__(self.packetType, self.value)
174174

175-
def __eq__(self, other):
175+
def __eq__(self, other: Any) -> bool:
176176
if isinstance(other, int):
177177
return self.value == other
178178
if isinstance(other, str):
@@ -181,28 +181,28 @@ def __eq__(self, other):
181181
return self.value == other.value
182182
return False
183183

184-
def __lt__(self, other):
184+
def __lt__(self, other: Any) -> bool:
185185
if isinstance(other, int):
186186
return self.value < other
187187
if isinstance(other, ReasonCode):
188188
return self.value < other.value
189189
return NotImplemented
190190

191-
def __repr__(self):
191+
def __repr__(self) -> str:
192192
try:
193193
packet_name = PacketTypes.Names[self.packetType]
194194
except IndexError:
195195
packet_name = "Unknown"
196196

197197
return f"ReasonCode({packet_name}, {self.getName()!r})"
198198

199-
def __str__(self):
199+
def __str__(self) -> str:
200200
return self.getName()
201201

202-
def json(self):
202+
def json(self) -> str:
203203
return self.getName()
204204

205-
def pack(self):
205+
def pack(self) -> bytearray:
206206
return bytearray([self.value])
207207

208208
@property

0 commit comments

Comments
 (0)