From 3f5aec158d1694ab827f4aca797589797b44c8b1 Mon Sep 17 00:00:00 2001 From: UnicycleDumpTruck Date: Sat, 19 Mar 2022 13:13:15 -0400 Subject: [PATCH 1/6] Added type annotations. --- adafruit_magtag/graphics.py | 28 +++++++++++++++++++---- adafruit_magtag/magtag.py | 42 +++++++++++++++++++++------------- adafruit_magtag/network.py | 17 +++++++++----- adafruit_magtag/peripherals.py | 35 ++++++++++++++++------------ 4 files changed, 81 insertions(+), 41 deletions(-) diff --git a/adafruit_magtag/graphics.py b/adafruit_magtag/graphics.py index ba71c14..6e3de65 100755 --- a/adafruit_magtag/graphics.py +++ b/adafruit_magtag/graphics.py @@ -32,6 +32,11 @@ import board from adafruit_portalbase.graphics import GraphicsBase +try: + from typing import Union, Optional, Tuple +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git" @@ -50,8 +55,13 @@ class Graphics(GraphicsBase): # pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements def __init__( - self, *, default_bg=0xFFFFFF, auto_refresh=True, rotation=270, debug=False - ): + self, + *, + default_bg: Union[str, int] = 0xFFFFFF, + auto_refresh: bool = True, + rotation: int = 270, + debug: bool = False + ) -> None: self._debug = debug self.display = board.DISPLAY self.display.rotation = rotation @@ -60,7 +70,9 @@ def __init__( super().__init__(board.DISPLAY, default_bg=default_bg, debug=debug) - def set_background(self, file_or_color, position=None): + def set_background( + self, file_or_color: Union[str, int], position: Optional[Tuple] = None + ) -> None: """The background image to a bitmap file. :param file_or_color: The filename of the chosen background image, or a hex color. @@ -73,8 +85,14 @@ def set_background(self, file_or_color, position=None): gc.collect() def qrcode( - self, qr_data, *, qr_size=1, x=0, y=0, qr_color=0x000000 - ): # pylint: disable=invalid-name + self, + qr_data: Union[bytes, str], + *, + qr_size: int = 1, + x: int = 0, + y: int = 0, + qr_color: int = 0x000000 + ) -> None: # pylint: disable=invalid-name """Display a QR code on the eInk :param qr_data: The data for the QR code. diff --git a/adafruit_magtag/magtag.py b/adafruit_magtag/magtag.py index cbc0890..289e0a8 100755 --- a/adafruit_magtag/magtag.py +++ b/adafruit_magtag/magtag.py @@ -35,6 +35,11 @@ from adafruit_magtag.graphics import Graphics from adafruit_magtag.peripherals import Peripherals +try: + from typing import Optional, Union, Sequence, Dict, Callable, Any +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git" @@ -65,16 +70,16 @@ class MagTag(PortalBase): def __init__( self, *, - url=None, - headers=None, - json_path=None, - regexp_path=None, - default_bg=0xFFFFFF, - status_neopixel=None, - json_transform=None, - rotation=270, - debug=False, - ): + url: Optional[str] = None, + headers: Optional[Dict] = None, + json_path: Optional[Sequence] = None, + regexp_path: Optional[Sequence] = None, + default_bg: Union[str, int] = 0xFFFFFF, + status_neopixel: Optional[str] = None, + json_transform: Union[Sequence, Callable] = None, + rotation: int = 270, + debug: bool = False, + ) -> None: self.peripherals = Peripherals() @@ -107,7 +112,7 @@ def __init__( gc.collect() - def exit_and_deep_sleep(self, sleep_time): + def exit_and_deep_sleep(self, sleep_time: float) -> None: """ Stops the current program and enters deep sleep. The program is restarted from the beginning after a certain period of time. @@ -123,7 +128,7 @@ def exit_and_deep_sleep(self, sleep_time): self.peripherals.speaker_disable = True super().exit_and_deep_sleep(sleep_time) - def enter_light_sleep(self, sleep_time): + def enter_light_sleep(self, sleep_time: float) -> None: """ Enter light sleep and resume the program after a certain period of time. @@ -147,7 +152,7 @@ def enter_light_sleep(self, sleep_time): gc.collect() # pylint: disable=arguments-differ - def set_text(self, val, index=0, auto_refresh=True): + def set_text(self, val: str, index: int = 0, auto_refresh: bool = True) -> None: """Display text, with indexing into our list of text boxes. :param str val: The text to be displayed @@ -162,11 +167,16 @@ def set_text(self, val, index=0, auto_refresh=True): # pylint: enable=arguments-differ - def _fetch_set_text(self, val, index=0): + def _fetch_set_text(self, val: str, index: int = 0) -> None: self.set_text(val, index=index, auto_refresh=False) # pylint: disable=arguments-differ - def fetch(self, refresh_url=None, timeout=10, auto_refresh=True): + def fetch( + self, + refresh_url: Optional[str] = None, + timeout: int = 10, + auto_refresh: bool = True, + ) -> Any: """Fetch data from the url we initialized with, perfom any parsing, and display text or graphics. This function does pretty much everything Optionally update the URL @@ -183,7 +193,7 @@ def fetch(self, refresh_url=None, timeout=10, auto_refresh=True): # pylint: enable=arguments-differ - def refresh(self): + def refresh(self) -> None: """ Refresh the display """ diff --git a/adafruit_magtag/network.py b/adafruit_magtag/network.py index 2cf73f4..dc6122f 100755 --- a/adafruit_magtag/network.py +++ b/adafruit_magtag/network.py @@ -31,6 +31,11 @@ from adafruit_portalbase.network import NetworkBase from adafruit_portalbase.wifi_esp32s2 import WiFi +try: + from typing import Any, Optional +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git" @@ -50,10 +55,10 @@ class Network(NetworkBase): def __init__( self, *, - status_neopixel=None, - extract_values=True, - debug=False, - ): + status_neopixel: Optional[str] = None, + extract_values: bool = True, + debug: bool = False, + ) -> None: if status_neopixel: if isinstance(status_neopixel, neopixel.NeoPixel): status_led = status_neopixel @@ -68,7 +73,7 @@ def __init__( ) @property - def enabled(self): + def enabled(self) -> bool: """ Get or Set whether the WiFi is enabled @@ -76,5 +81,5 @@ def enabled(self): return self._wifi.enabled @enabled.setter - def enabled(self, value): + def enabled(self, value: Any) -> None: self._wifi.enabled = bool(value) diff --git a/adafruit_magtag/peripherals.py b/adafruit_magtag/peripherals.py index 17d8ed5..26b07d0 100755 --- a/adafruit_magtag/peripherals.py +++ b/adafruit_magtag/peripherals.py @@ -33,6 +33,11 @@ import neopixel import simpleio +try: + from typing import Union +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git" @@ -41,7 +46,7 @@ class Peripherals: """Peripherals Helper Class for the MagTag Library""" # pylint: disable=too-many-instance-attributes, too-many-locals, too-many-branches, too-many-statements - def __init__(self): + def __init__(self) -> None: # Neopixel power try: self._neopixel_disable = DigitalInOut(board.NEOPIXEL_POWER) @@ -71,7 +76,9 @@ def __init__(self): switch.pull = Pull.UP self.buttons.append(switch) - def play_tone(self, frequency, duration): + def play_tone( + self, frequency: Union[float, int], duration: Union[float, int] + ) -> None: """Automatically Enable/Disable the speaker and play a tone at the specified frequency for the specified duration It will attempt to play the sound up to 3 times in the case of @@ -91,7 +98,7 @@ def play_tone(self, frequency, duration): attempt += 1 self._speaker_enable.value = False - def deinit(self): + def deinit(self) -> None: """Call deinit on all resources to free them""" self.neopixels.deinit() if self._neopixel_disable is not None: @@ -103,12 +110,12 @@ def deinit(self): self._light.deinit() @property - def battery(self): + def battery(self) -> float: """Return the voltage of the battery""" return (self._batt_monitor.value / 65535.0) * 3.3 * 2 @property - def neopixel_disable(self): + def neopixel_disable(self) -> bool: """ Enable or disable the neopixels for power savings """ @@ -117,58 +124,58 @@ def neopixel_disable(self): return False @neopixel_disable.setter - def neopixel_disable(self, value): + def neopixel_disable(self, value: bool) -> None: if self._neopixel_disable is not None: self._neopixel_disable.value = value @property - def speaker_disable(self): + def speaker_disable(self) -> bool: """ Enable or disable the speaker for power savings """ return not self._speaker_enable.value @speaker_disable.setter - def speaker_disable(self, value): + def speaker_disable(self, value: bool) -> None: self._speaker_enable.value = not value @property - def button_a_pressed(self): + def button_a_pressed(self) -> bool: """ Return whether Button A is pressed """ return not self.buttons[0].value @property - def button_b_pressed(self): + def button_b_pressed(self) -> bool: """ Return whether Button B is pressed """ return not self.buttons[1].value @property - def button_c_pressed(self): + def button_c_pressed(self) -> bool: """ Return whether Button C is pressed """ return not self.buttons[2].value @property - def button_d_pressed(self): + def button_d_pressed(self) -> bool: """ Return whether Button D is pressed """ return not self.buttons[3].value @property - def any_button_pressed(self): + def any_button_pressed(self) -> bool: """ Return whether any button is pressed """ return False in [self.buttons[i].value for i in range(0, 4)] @property - def light(self): + def light(self) -> int: """ Return the value of the light sensor. The neopixel_disable property must be false to get a value. From 04ce83a6b49b9cf154a1d0d50c063d3a767916d0 Mon Sep 17 00:00:00 2001 From: UnicycleDumpTruck Date: Sat, 19 Mar 2022 20:39:21 -0400 Subject: [PATCH 2/6] Fixed generics in type annotations --- adafruit_magtag/graphics.py | 2 +- adafruit_magtag/magtag.py | 8 ++++---- adafruit_magtag/network.py | 7 ++++--- adafruit_magtag/peripherals.py | 9 +-------- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/adafruit_magtag/graphics.py b/adafruit_magtag/graphics.py index 6e3de65..8291b2d 100755 --- a/adafruit_magtag/graphics.py +++ b/adafruit_magtag/graphics.py @@ -71,7 +71,7 @@ def __init__( super().__init__(board.DISPLAY, default_bg=default_bg, debug=debug) def set_background( - self, file_or_color: Union[str, int], position: Optional[Tuple] = None + self, file_or_color: Union[str, int], position: Optional[Tuple[int, int]] = None ) -> None: """The background image to a bitmap file. diff --git a/adafruit_magtag/magtag.py b/adafruit_magtag/magtag.py index 289e0a8..e4b67aa 100755 --- a/adafruit_magtag/magtag.py +++ b/adafruit_magtag/magtag.py @@ -71,12 +71,12 @@ def __init__( self, *, url: Optional[str] = None, - headers: Optional[Dict] = None, - json_path: Optional[Sequence] = None, - regexp_path: Optional[Sequence] = None, + headers: Optional[Dict[str, str]] = None, + json_path: Optional[Sequence[Union[Sequence[object], object]]] = None, + regexp_path: Optional[Sequence[str]] = None, default_bg: Union[str, int] = 0xFFFFFF, status_neopixel: Optional[str] = None, - json_transform: Union[Sequence, Callable] = None, + json_transform: Union[Sequence[Callable], Callable] = None, rotation: int = 270, debug: bool = False, ) -> None: diff --git a/adafruit_magtag/network.py b/adafruit_magtag/network.py index dc6122f..828e08b 100755 --- a/adafruit_magtag/network.py +++ b/adafruit_magtag/network.py @@ -28,11 +28,12 @@ """ import neopixel +import board from adafruit_portalbase.network import NetworkBase from adafruit_portalbase.wifi_esp32s2 import WiFi try: - from typing import Any, Optional + from typing import Optional, Union except ImportError: pass @@ -55,7 +56,7 @@ class Network(NetworkBase): def __init__( self, *, - status_neopixel: Optional[str] = None, + status_neopixel: Optional[Union[board.Pin, neopixel.NeoPixel]] = None, extract_values: bool = True, debug: bool = False, ) -> None: @@ -81,5 +82,5 @@ def enabled(self) -> bool: return self._wifi.enabled @enabled.setter - def enabled(self, value: Any) -> None: + def enabled(self, value: bool) -> None: self._wifi.enabled = bool(value) diff --git a/adafruit_magtag/peripherals.py b/adafruit_magtag/peripherals.py index 26b07d0..2e82f27 100755 --- a/adafruit_magtag/peripherals.py +++ b/adafruit_magtag/peripherals.py @@ -33,11 +33,6 @@ import neopixel import simpleio -try: - from typing import Union -except ImportError: - pass - __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MagTag.git" @@ -76,9 +71,7 @@ def __init__(self) -> None: switch.pull = Pull.UP self.buttons.append(switch) - def play_tone( - self, frequency: Union[float, int], duration: Union[float, int] - ) -> None: + def play_tone(self, frequency: float, duration: float) -> None: """Automatically Enable/Disable the speaker and play a tone at the specified frequency for the specified duration It will attempt to play the sound up to 3 times in the case of From 029dbe54c3895d4607cd08a5a5139a90c2719fe5 Mon Sep 17 00:00:00 2001 From: UnicycleDumpTruck Date: Sat, 19 Mar 2022 20:52:27 -0400 Subject: [PATCH 3/6] Fixed wrong module for microcontroller.Pin class --- adafruit_magtag/network.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_magtag/network.py b/adafruit_magtag/network.py index 828e08b..94d385c 100755 --- a/adafruit_magtag/network.py +++ b/adafruit_magtag/network.py @@ -28,7 +28,7 @@ """ import neopixel -import board +import microcontroller from adafruit_portalbase.network import NetworkBase from adafruit_portalbase.wifi_esp32s2 import WiFi @@ -56,7 +56,7 @@ class Network(NetworkBase): def __init__( self, *, - status_neopixel: Optional[Union[board.Pin, neopixel.NeoPixel]] = None, + status_neopixel: Optional[Union[microcontroller.Pin, neopixel.NeoPixel]] = None, extract_values: bool = True, debug: bool = False, ) -> None: From 664a70af150850a306ca02c57f893050e0ab15fa Mon Sep 17 00:00:00 2001 From: UnicycleDumpTruck Date: Sun, 20 Mar 2022 11:00:22 -0400 Subject: [PATCH 4/6] Fix: type annotation on MagTag.__init__() --- adafruit_magtag/magtag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_magtag/magtag.py b/adafruit_magtag/magtag.py index e4b67aa..72e48cc 100755 --- a/adafruit_magtag/magtag.py +++ b/adafruit_magtag/magtag.py @@ -72,7 +72,7 @@ def __init__( *, url: Optional[str] = None, headers: Optional[Dict[str, str]] = None, - json_path: Optional[Sequence[Union[Sequence[object], object]]] = None, + json_path: Optional[Sequence[Any]] = None, regexp_path: Optional[Sequence[str]] = None, default_bg: Union[str, int] = 0xFFFFFF, status_neopixel: Optional[str] = None, From 5c8ae78b446da075577adb310d21e057eadba0ed Mon Sep 17 00:00:00 2001 From: UnicycleDumpTruck Date: Sun, 20 Mar 2022 14:37:20 -0400 Subject: [PATCH 5/6] Fix: type annotations --- adafruit_magtag/magtag.py | 4 +++- adafruit_magtag/network.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_magtag/magtag.py b/adafruit_magtag/magtag.py index 72e48cc..2cd5e12 100755 --- a/adafruit_magtag/magtag.py +++ b/adafruit_magtag/magtag.py @@ -37,6 +37,8 @@ try: from typing import Optional, Union, Sequence, Dict, Callable, Any + import microcontroller + import neopixel except ImportError: pass @@ -75,7 +77,7 @@ def __init__( json_path: Optional[Sequence[Any]] = None, regexp_path: Optional[Sequence[str]] = None, default_bg: Union[str, int] = 0xFFFFFF, - status_neopixel: Optional[str] = None, + status_neopixel: Optional[Union[microcontroller.Pin], neopixel.NeoPixel] = None, json_transform: Union[Sequence[Callable], Callable] = None, rotation: int = 270, debug: bool = False, diff --git a/adafruit_magtag/network.py b/adafruit_magtag/network.py index 94d385c..d0065b1 100755 --- a/adafruit_magtag/network.py +++ b/adafruit_magtag/network.py @@ -28,12 +28,12 @@ """ import neopixel -import microcontroller from adafruit_portalbase.network import NetworkBase from adafruit_portalbase.wifi_esp32s2 import WiFi try: from typing import Optional, Union + import microcontroller except ImportError: pass From 7db37ae63bc78eac20dbbd16d294deb9c1928d9e Mon Sep 17 00:00:00 2001 From: Jeff Highsmith Date: Sun, 20 Mar 2022 15:13:00 -0400 Subject: [PATCH 6/6] Update adafruit_magtag/magtag.py Co-authored-by: tekktrik <89490472+tekktrik@users.noreply.github.com> --- adafruit_magtag/magtag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_magtag/magtag.py b/adafruit_magtag/magtag.py index 2cd5e12..9e10303 100755 --- a/adafruit_magtag/magtag.py +++ b/adafruit_magtag/magtag.py @@ -77,7 +77,7 @@ def __init__( json_path: Optional[Sequence[Any]] = None, regexp_path: Optional[Sequence[str]] = None, default_bg: Union[str, int] = 0xFFFFFF, - status_neopixel: Optional[Union[microcontroller.Pin], neopixel.NeoPixel] = None, + status_neopixel: Optional[Union[microcontroller.Pin, neopixel.NeoPixel]] = None, json_transform: Union[Sequence[Callable], Callable] = None, rotation: int = 270, debug: bool = False,