Skip to content

Commit 948c509

Browse files
authored
Merge pull request #24 from tekktrik/feature/add-typing
Add type hints
2 parents 4a2868c + 5bb0fb1 commit 948c509

File tree

5 files changed

+89
-60
lines changed

5 files changed

+89
-60
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ repos:
2424
name: pylint (library code)
2525
types: [python]
2626
args:
27-
- --disable=consider-using-f-string
27+
- --disable=consider-using-f-string,duplicate-code
2828
exclude: "^(docs/|examples/|tests/|setup.py$)"
2929
- id: pylint
3030
name: pylint (example code)

adafruit_funhouse/__init__.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
from adafruit_funhouse.graphics import Graphics
3535
from adafruit_funhouse.peripherals import Peripherals
3636

37+
try:
38+
from typing import Optional, Dict, Union, Callable, Sequence, List
39+
from adafruit_dotstar import DotStar
40+
except ImportError:
41+
pass
42+
3743
__version__ = "0.0.0-auto.0"
3844
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FunHouse.git"
3945

@@ -65,17 +71,17 @@ class FunHouse(PortalBase):
6571
def __init__(
6672
self,
6773
*,
68-
url=None,
69-
headers=None,
70-
json_path=None,
71-
regexp_path=None,
72-
default_bg=0,
73-
status_dotstar=None,
74-
json_transform=None,
75-
rotation=270,
76-
scale=1,
77-
debug=False,
78-
):
74+
url: Optional[str] = None,
75+
headers: Dict[str, str] = None,
76+
json_path: Optional[Union[List[str], List[List[str]]]] = None,
77+
regexp_path: Optional[Sequence[str]] = None,
78+
default_bg: int = 0,
79+
status_dotstar: Optional[DotStar] = None,
80+
json_transform: Optional[Union[Callable, List[Callable]]] = None,
81+
rotation: int = 270,
82+
scale: int = 1,
83+
debug: bool = False,
84+
) -> None:
7985

8086
network = Network(
8187
status_dotstar=status_dotstar,
@@ -105,7 +111,7 @@ def __init__(
105111

106112
gc.collect()
107113

108-
def enter_light_sleep(self, sleep_time):
114+
def enter_light_sleep(self, sleep_time: float) -> None:
109115
"""
110116
Enter light sleep and resume the program after a certain period of time.
111117

adafruit_funhouse/graphics.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ class Graphics(GraphicsBase):
4545
"""
4646

4747
# pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements, too-few-public-methods
48-
def __init__(self, *, default_bg=0, rotation=270, scale=1, debug=False):
48+
def __init__(
49+
self,
50+
*,
51+
default_bg: int = 0,
52+
rotation: int = 270,
53+
scale: int = 1,
54+
debug: bool = False
55+
) -> None:
4956
self._debug = debug
5057
self.display = board.DISPLAY
5158
self.display.rotation = rotation

adafruit_funhouse/network.py

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
from adafruit_portalbase.network import NetworkBase
3434
from adafruit_portalbase.wifi_esp32s2 import WiFi
3535

36+
try:
37+
from typing import Optional, Union, Callable
38+
from adafruit_dotstar import DotStar
39+
except ImportError:
40+
pass
41+
3642
__version__ = "0.0.0-auto.0"
3743
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FunHouse.git"
3844

@@ -54,18 +60,18 @@ class Network(NetworkBase):
5460
def __init__(
5561
self,
5662
*,
57-
status_dotstar=None,
58-
extract_values=True,
59-
debug=False,
60-
):
63+
status_dotstar: Optional[DotStar] = None,
64+
extract_values: bool = True,
65+
debug: bool = False,
66+
) -> None:
6167
super().__init__(
6268
WiFi(status_led=status_dotstar),
6369
extract_values=extract_values,
6470
debug=debug,
6571
)
6672
self._mqtt_client = None
6773

68-
def init_io_mqtt(self):
74+
def init_io_mqtt(self) -> IO_MQTT:
6975
"""Initialize MQTT for Adafruit IO"""
7076
try:
7177
aio_username = self._secrets["aio_username"]
@@ -80,12 +86,12 @@ def init_io_mqtt(self):
8086
# pylint: disable=too-many-arguments
8187
def init_mqtt(
8288
self,
83-
broker,
84-
port=8883,
85-
username=None,
86-
password=None,
87-
use_io=False,
88-
):
89+
broker: str,
90+
port: int = 8883,
91+
username: str = None,
92+
password: str = None,
93+
use_io: bool = False,
94+
) -> Union[MQTT.MQTT, IO_MQTT]:
8995
"""Initialize MQTT"""
9096
self.connect()
9197
self._mqtt_client = MQTT.MQTT(
@@ -103,12 +109,14 @@ def init_mqtt(
103109

104110
# pylint: enable=too-many-arguments
105111

106-
def _get_mqtt_client(self):
112+
def _get_mqtt_client(self) -> Union[MQTT.MQTT, IO_MQTT]:
107113
if self._mqtt_client is not None:
108114
return self._mqtt_client
109115
raise RuntimeError("Please initialize MQTT before using")
110116

111-
def mqtt_loop(self, *args, suppress_mqtt_errors=True, **kwargs):
117+
def mqtt_loop(
118+
self, *args: int, suppress_mqtt_errors: bool = True, **kwargs: int
119+
) -> None:
112120
"""Run the MQTT Loop"""
113121
self._get_mqtt_client()
114122
if suppress_mqtt_errors:
@@ -123,7 +131,12 @@ def mqtt_loop(self, *args, suppress_mqtt_errors=True, **kwargs):
123131
if self._mqtt_client is not None:
124132
self._mqtt_client.loop(*args, **kwargs)
125133

126-
def mqtt_publish(self, *args, suppress_mqtt_errors=True, **kwargs):
134+
def mqtt_publish(
135+
self,
136+
*args: Union[str, int, float],
137+
suppress_mqtt_errors: bool = True,
138+
**kwargs: Union[str, int, float]
139+
) -> None:
127140
"""Publish to MQTT"""
128141
self._get_mqtt_client()
129142
if suppress_mqtt_errors:
@@ -136,14 +149,16 @@ def mqtt_publish(self, *args, suppress_mqtt_errors=True, **kwargs):
136149
if self._mqtt_client is not None:
137150
self._mqtt_client.publish(*args, **kwargs)
138151

139-
def mqtt_connect(self, *args, **kwargs):
152+
def mqtt_connect(
153+
self, *args: Union[bool, str, int], **kwargs: Union[bool, str, int]
154+
) -> None:
140155
"""Connect to MQTT"""
141156
self._get_mqtt_client()
142157
if self._mqtt_client is not None:
143158
self._mqtt_client.connect(*args, **kwargs)
144159

145160
@property
146-
def on_mqtt_connect(self):
161+
def on_mqtt_connect(self) -> Optional[Callable]:
147162
"""
148163
Get or Set the MQTT Connect Handler
149164
@@ -153,12 +168,12 @@ def on_mqtt_connect(self):
153168
return None
154169

155170
@on_mqtt_connect.setter
156-
def on_mqtt_connect(self, value):
171+
def on_mqtt_connect(self, value: Callable) -> None:
157172
self._get_mqtt_client()
158173
self._mqtt_client.on_connect = value
159174

160175
@property
161-
def on_mqtt_disconnect(self):
176+
def on_mqtt_disconnect(self) -> Optional[Callable]:
162177
"""
163178
Get or Set the MQTT Disconnect Handler
164179
@@ -168,11 +183,11 @@ def on_mqtt_disconnect(self):
168183
return None
169184

170185
@on_mqtt_disconnect.setter
171-
def on_mqtt_disconnect(self, value):
186+
def on_mqtt_disconnect(self, value: Callable) -> None:
172187
self._get_mqtt_client().on_disconnect = value
173188

174189
@property
175-
def on_mqtt_subscribe(self):
190+
def on_mqtt_subscribe(self) -> Optional[Callable]:
176191
"""
177192
Get or Set the MQTT Subscribe Handler
178193
@@ -182,11 +197,11 @@ def on_mqtt_subscribe(self):
182197
return None
183198

184199
@on_mqtt_subscribe.setter
185-
def on_mqtt_subscribe(self, value):
200+
def on_mqtt_subscribe(self, value: Callable) -> None:
186201
self._get_mqtt_client().on_subscribe = value
187202

188203
@property
189-
def on_mqtt_unsubscribe(self):
204+
def on_mqtt_unsubscribe(self) -> Optional[Callable]:
190205
"""
191206
Get or Set the MQTT Unsubscribe Handler
192207
@@ -196,11 +211,11 @@ def on_mqtt_unsubscribe(self):
196211
return None
197212

198213
@on_mqtt_unsubscribe.setter
199-
def on_mqtt_unsubscribe(self, value):
214+
def on_mqtt_unsubscribe(self, value: Callable) -> None:
200215
self._get_mqtt_client().on_unsubscribe = value
201216

202217
@property
203-
def on_mqtt_message(self):
218+
def on_mqtt_message(self) -> Optional[Callable]:
204219
"""
205220
Get or Set the MQTT Message Handler
206221
@@ -210,17 +225,17 @@ def on_mqtt_message(self):
210225
return None
211226

212227
@on_mqtt_message.setter
213-
def on_mqtt_message(self, value):
228+
def on_mqtt_message(self, value: Callable) -> None:
214229
self._get_mqtt_client().on_message = value
215230

216231
@property
217-
def enabled(self):
232+
def enabled(self) -> bool:
218233
"""
219234
Get or Set whether the WiFi is enabled
220235
221236
"""
222237
return self._wifi.enabled
223238

224239
@enabled.setter
225-
def enabled(self, value):
240+
def enabled(self, value: bool) -> None:
226241
self._wifi.enabled = bool(value)

0 commit comments

Comments
 (0)