From fabe0e9d8111c0059fef83846fb9aac1162ff554 Mon Sep 17 00:00:00 2001 From: Jason Symons Date: Wed, 6 Oct 2021 20:44:20 -0400 Subject: [PATCH 1/2] Add type hints --- neopixel.py | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/neopixel.py b/neopixel.py index 9b62818..bf80567 100644 --- a/neopixel.py +++ b/neopixel.py @@ -27,6 +27,16 @@ import adafruit_pypixelbuf as adafruit_pixelbuf +try: + from typing import Optional, Type + from types import TracebackType +except ImportError: + pass + + +import microcontroller + + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git" @@ -102,7 +112,14 @@ class NeoPixel(adafruit_pixelbuf.PixelBuf): """ def __init__( - self, pin, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None + self, + pin: microcontroller.Pin, + n: int, + *, + bpp: int = 3, + brightness: float = 1.0, + auto_write: bool = True, + pixel_order: str = None ): if not pixel_order: pixel_order = GRB if bpp == 3 else GRBW @@ -144,7 +161,12 @@ def deinit(self): def __enter__(self): return self - def __exit__(self, exception_type, exception_value, traceback): + def __exit__( + self, + exception_type: Optional[Type[BaseException]], + exception_value: Optional[BaseException], + traceback: Optional[TracebackType], + ): self.deinit() def __repr__(self): @@ -163,5 +185,5 @@ def write(self): Use ``show`` instead. It matches Micro:Bit and Arduino APIs.""" self.show() - def _transmit(self, buffer): + def _transmit(self, buffer: bytearray): neopixel_write(self.pin, buffer) From 29083f9593783441af108ee740c4e233f24c93d4 Mon Sep 17 00:00:00 2001 From: Jason Symons Date: Thu, 7 Oct 2021 21:51:37 -0400 Subject: [PATCH 2/2] Moved typing-related imports into try/except block --- neopixel.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/neopixel.py b/neopixel.py index bf80567..877cebe 100644 --- a/neopixel.py +++ b/neopixel.py @@ -28,15 +28,14 @@ try: + # Used only for typing from typing import Optional, Type from types import TracebackType + import microcontroller except ImportError: pass -import microcontroller - - __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git" @@ -150,7 +149,7 @@ def __init__( self.pin = digitalio.DigitalInOut(pin) self.pin.direction = digitalio.Direction.OUTPUT - def deinit(self): + def deinit(self) -> None: """Blank out the NeoPixels and release the pin.""" self.fill(0) self.show() @@ -173,17 +172,17 @@ def __repr__(self): return "[" + ", ".join([str(x) for x in self]) + "]" @property - def n(self): + def n(self) -> int: """ The number of neopixels in the chain (read-only) """ return len(self) - def write(self): + def write(self) -> None: """.. deprecated: 1.0.0 Use ``show`` instead. It matches Micro:Bit and Arduino APIs.""" self.show() - def _transmit(self, buffer: bytearray): + def _transmit(self, buffer: bytearray) -> None: neopixel_write(self.pin, buffer)