Skip to content

Add type hints #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
import adafruit_pypixelbuf as adafruit_pixelbuf


try:
# Used only for typing
from typing import Optional, Type
from types import TracebackType
import microcontroller
except ImportError:
pass


__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel.git"

Expand Down Expand Up @@ -102,7 +111,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
Expand Down Expand Up @@ -133,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()
Expand All @@ -144,24 +160,29 @@ 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):
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):
def _transmit(self, buffer: bytearray) -> None:
neopixel_write(self.pin, buffer)