From cd2875e62dcc34b2bd1525c31a0e93627cf8d12f Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 25 Mar 2022 09:32:07 -0400 Subject: [PATCH 1/2] Add type annotation protocols for LEDs --- circuitpython_typing/led.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 circuitpython_typing/led.py diff --git a/circuitpython_typing/led.py b/circuitpython_typing/led.py new file mode 100644 index 0000000..10dabdb --- /dev/null +++ b/circuitpython_typing/led.py @@ -0,0 +1,32 @@ +# SPDX-FileCopyrightText: Copyright (c) 2022 Alec Delaney +# +# SPDX-License-Identifier: MIT + +""" +`circuitpython_typing.led` +================================================================================ + +Type annotation definitions for device drivers. Used for where LEDs are +type annotated. + +* Author(s): Alec Delaney +""" + +# # Protocol was introduced in Python 3.8. +try: + from typing import Union, Tuple, Protocol +except ImportError: + from typing_extensions import Protocol + +ColorBasedColorUnion = Union[int, Tuple[int, int, int]] +FillBasedColorUnion = Union[ColorBasedColorUnion, Tuple[int, int, int, int]] + +class ColorBasedLED(Protocol): + + def color(self, value: ColorBasedColorUnion) -> None: + ... + +class FillBasedLED(Protocol): + + def fill(self, color: FillBasedColorUnion) -> None: + ... From 64f0a7dd4eaf2ab15e88ddab135e79477d5d3557 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 25 Mar 2022 09:40:12 -0400 Subject: [PATCH 2/2] Linted and reformatted per pre-commit --- circuitpython_typing/led.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/circuitpython_typing/led.py b/circuitpython_typing/led.py index 10dabdb..741996b 100644 --- a/circuitpython_typing/led.py +++ b/circuitpython_typing/led.py @@ -21,12 +21,18 @@ ColorBasedColorUnion = Union[int, Tuple[int, int, int]] FillBasedColorUnion = Union[ColorBasedColorUnion, Tuple[int, int, int, int]] + class ColorBasedLED(Protocol): + """Protocol for LEDs using the :meth:`color` method""" def color(self, value: ColorBasedColorUnion) -> None: + """Sets the color of the LED""" ... + class FillBasedLED(Protocol): + """Protocol for LEDs using the :meth:`fill` method""" def fill(self, color: FillBasedColorUnion) -> None: + """Sets the color of the LED""" ...