Skip to content

Commit 24695d3

Browse files
Adding type hints to color_packet
1 parent 5c7c0ea commit 24695d3

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

adafruit_bluefruit_connect/color_packet.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,27 @@
1212
1313
"""
1414

15+
from __future__ import annotations
16+
1517
import struct
1618

1719
from .packet import Packet
1820

21+
try:
22+
from typing import Optional # adjust these as needed
23+
except ImportError:
24+
pass
1925

2026
class ColorPacket(Packet):
2127
"""A packet containing an RGB color value."""
2228

23-
_FMT_PARSE = "<xx3Bx"
24-
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
29+
_FMT_PARSE: str = "<xx3Bx"
30+
PACKET_LENGTH: int = struct.calcsize(_FMT_PARSE)
2531
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
26-
_FMT_CONSTRUCT = "<2s3B"
27-
_TYPE_HEADER = b"!C"
32+
_FMT_CONSTRUCT: str = "<2s3B"
33+
_TYPE_HEADER: bytes = b"!C"
2834

29-
def __init__(self, color):
35+
def __init__(self, color: Optional[Packet]) -> None:
3036
"""Construct a ColorPacket from a 3-element :class:`tuple` of RGB
3137
values, or from an int color value 0xRRGGBB.
3238
@@ -41,22 +47,22 @@ def __init__(self, color):
4147
raise ValueError("Color must be an integer 0xRRGGBB or a tuple(r,g,b)")
4248

4349
@classmethod
44-
def parse_private(cls, packet):
50+
def parse_private(cls, packet: Optional[Packet]) -> Optional[Packet]:
4551
"""Construct a ColorPacket from an incoming packet.
4652
Do not call this directly; call Packet.from_bytes() instead.
4753
pylint makes it difficult to call this method _parse(), hence the name.
4854
"""
4955
return cls(struct.unpack(cls._FMT_PARSE, packet))
5056

51-
def to_bytes(self):
57+
def to_bytes(self) -> bytes:
5258
"""Return the bytes needed to send this packet."""
5359
partial_packet = struct.pack(
5460
self._FMT_CONSTRUCT, self._TYPE_HEADER, *self._color
5561
)
5662
return self.add_checksum(partial_packet)
5763

5864
@property
59-
def color(self):
65+
def color(self) -> tuple:
6066
"""A :class:`tuple` ``(red, green blue)`` representing the color the
6167
user chose in the BlueFruit Connect app."""
6268
return self._color

0 commit comments

Comments
 (0)