Skip to content

Commit 50fa604

Browse files
committed
Add type hints
1 parent bd72878 commit 50fa604

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

adafruit_pyoa.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
from adafruit_button import Button
4949
import terminalio
5050

51+
try:
52+
from typing import Dict, Optional, List
53+
except ImportError:
54+
pass
55+
5156
__version__ = "0.0.0-auto.0"
5257
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PYOA.git"
5358

@@ -56,7 +61,7 @@ class PYOA_Graphics:
5661
# pylint: disable=too-many-instance-attributes
5762
"""A choose your own adventure game framework."""
5863

59-
def __init__(self):
64+
def __init__(self) -> None:
6065
self.root_group = displayio.Group()
6166
self._display = board.DISPLAY
6267
self._background_group = displayio.Group()
@@ -113,7 +118,7 @@ def __init__(self):
113118
self._right_button = None
114119
self._middle_button = None
115120

116-
def load_game(self, game_directory):
121+
def load_game(self, game_directory: str) -> None:
117122
"""Load a game.
118123
119124
:param str game_directory: where the game files are stored
@@ -183,7 +188,7 @@ def load_game(self, game_directory):
183188
except OSError as err:
184189
raise OSError("Could not open game file " + self._gamefilename) from err
185190

186-
def _fade_to_black(self):
191+
def _fade_to_black(self) -> None:
187192
"""Turn down the lights."""
188193
if self.mouse_cursor:
189194
self.mouse_cursor.is_hidden = True
@@ -196,7 +201,7 @@ def _fade_to_black(self):
196201
if self.mouse_cursor:
197202
self.mouse_cursor.is_hidden = False
198203

199-
def _display_buttons(self, card):
204+
def _display_buttons(self, card: Dict[str, str]) -> None:
200205
"""Display the buttons of a card.
201206
202207
:param card: The active card
@@ -214,15 +219,15 @@ def _display_buttons(self, card):
214219
self._button_group.append(self._right_button)
215220
self._button_group.append(self._left_button)
216221

217-
def _display_background_for(self, card):
222+
def _display_background_for(self, card: Dict[str, str]) -> None:
218223
"""If there's a background on card, display it.
219224
220225
:param card: The active card
221226
:type card: dict(str, str)
222227
"""
223228
self.set_background(card.get("background_image", None), with_fade=False)
224229

225-
def _display_text_for(self, card):
230+
def _display_text_for(self, card: Dict[str, str]) -> None:
226231
"""Display the main text of a card.
227232
228233
:param card: The active card
@@ -248,7 +253,7 @@ def _display_text_for(self, card):
248253

249254
self.set_text(text, text_color, background_color=text_background_color)
250255

251-
def _play_sound_for(self, card):
256+
def _play_sound_for(self, card: Dict[str, str]) -> None:
252257
"""If there's a sound, start playing it.
253258
254259
:param card: The active card
@@ -261,7 +266,7 @@ def _play_sound_for(self, card):
261266
print("Loop:", loop)
262267
self.play_sound(sound, wait_to_finish=False, loop=loop)
263268

264-
def _wait_for_press(self, card):
269+
def _wait_for_press(self, card: Dict[str, str]) -> str:
265270
"""Wait for a button to be pressed.
266271
267272
:param card: The active card
@@ -299,7 +304,7 @@ def _wait_for_press(self, card):
299304
return card.get("button02_goto_card_id", None)
300305
time.sleep(0.1)
301306

302-
def display_card(self, card_num):
307+
def display_card(self, card_num: int) -> int:
303308
"""Display and handle input on a card.
304309
305310
:param int card_num: the index of the card to process
@@ -339,7 +344,7 @@ def display_card(self, card_num):
339344
"Could not find card with matching 'card_id': ", destination_card_id
340345
)
341346

342-
def play_sound(self, filename, *, wait_to_finish=True, loop=False):
347+
def play_sound(self, filename: Optional[str], *, wait_to_finish: bool = True, loop: bool = False) -> None:
343348
"""Play a sound
344349
345350
:param filename: The filename of the sound to play. Use `None` to stop
@@ -375,7 +380,7 @@ def play_sound(self, filename, *, wait_to_finish=True, loop=False):
375380
self._wavfile = None
376381
self._speaker_enable.value = False
377382

378-
def set_text(self, text, color, background_color=None):
383+
def set_text(self, text: Optional[str], color: Optional[str], background_color: Optional[int] = None) -> None:
379384
"""Display the test for a card.
380385
381386
:param text: the text to display
@@ -412,7 +417,7 @@ def set_text(self, text, color, background_color=None):
412417
self._text.background_color = background_color
413418
self._text_group.append(self._text)
414419

415-
def set_background(self, filename, *, with_fade=True):
420+
def set_background(self, filename: Optional[str], *, with_fade: bool=True) -> None:
416421
"""The background image to a bitmap file.
417422
418423
:param filename: The filename of the chosen background
@@ -437,7 +442,7 @@ def set_background(self, filename, *, with_fade=True):
437442
self._display.refresh(target_frames_per_second=60)
438443
self.backlight_fade(1.0)
439444

440-
def backlight_fade(self, to_light):
445+
def backlight_fade(self, to_light: float) -> None:
441446
"""
442447
Adjust the TFT backlight. Fade from the current value to the ``to_light`` value
443448
@@ -457,7 +462,7 @@ def backlight_fade(self, to_light):
457462

458463
# return a list of lines with wordwrapping
459464
@staticmethod
460-
def wrap_nicely(string, max_chars):
465+
def wrap_nicely(string: str, max_chars: int) -> List[str]:
461466
"""A helper that will return a list of lines with word-break wrapping.
462467
463468
:param str string: The text to be wrapped.

0 commit comments

Comments
 (0)