Skip to content

Commit 5fef0fe

Browse files
jackburridgeJamesParrott
authored andcommitted
refactor: add types to reason codes
1 parent 1e230ea commit 5fef0fe

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
@@ -128,7 +128,7 @@ def __init__(self, packetType: int, aName: str ="Success", identifier: int =-1):
128128
self.value = identifier
129129
self.getName() # check it's good
130130

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

147-
def getId(self, name):
147+
def getId(self, name: str) -> int:
148148
"""
149149
Get the numeric id corresponding to a reason code name.
150150
@@ -157,21 +157,21 @@ def getId(self, name):
157157
return code
158158
raise KeyError(f"Reason code name not found: {name}")
159159

160-
def set(self, name):
160+
def set(self, name: str) -> None:
161161
self.value = self.getId(name)
162162

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

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

174-
def __eq__(self, other):
174+
def __eq__(self, other: Any) -> bool:
175175
if isinstance(other, int):
176176
return self.value == other
177177
if isinstance(other, str):
@@ -180,28 +180,28 @@ def __eq__(self, other):
180180
return self.value == other.value
181181
return False
182182

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

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

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

198-
def __str__(self):
198+
def __str__(self) -> str:
199199
return self.getName()
200200

201-
def json(self):
201+
def json(self) -> str:
202202
return self.getName()
203203

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

207207
@property

0 commit comments

Comments
 (0)