Skip to content

Commit 5c7c0ea

Browse files
Adding type hints to button_packet
with a little house cleaning of _xyz_packet
1 parent 934ac91 commit 5c7c0ea

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

adafruit_bluefruit_connect/accelerometer_packet.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616

1717
from ._xyz_packet import _XYZPacket
1818

19-
try:
20-
from typing import Generator, Union, Dict, Optional, Any # adjust these as needed
21-
except ImportError:
22-
pass
2319

2420
class AccelerometerPacket(_XYZPacket):
2521
"""A packet of x, y, z float values from an accelerometer."""

adafruit_bluefruit_connect/button_packet.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,45 @@
1313
1414
"""
1515

16+
from __future__ import annotations
17+
1618
import struct
1719

1820
from .packet import Packet
1921

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

2127
class ButtonPacket(Packet):
2228
"""A packet containing a button name and its state."""
2329

24-
BUTTON_1 = "1"
30+
BUTTON_1: str = "1"
2531
"""Code for Button 1 on the Bluefruit LE Connect app Control Pad screen."""
26-
BUTTON_2 = "2"
32+
BUTTON_2: str = "2"
2733
"""Button 2."""
28-
BUTTON_3 = "3"
34+
BUTTON_3: str = "3"
2935
"""Button 3."""
30-
BUTTON_4 = "4"
36+
BUTTON_4: str = "4"
3137
"""Button 4."""
3238
# pylint: disable= invalid-name
33-
UP = "5"
39+
UP: str = "5"
3440
"""Up Button."""
35-
DOWN = "6"
41+
DOWN: str = "6"
3642
"""Down Button."""
37-
LEFT = "7"
43+
LEFT: str = "7"
3844
"""Left Button."""
39-
RIGHT = "8"
45+
RIGHT: str = "8"
4046
"""Right Button."""
4147

42-
_FMT_PARSE = "<xxssx"
43-
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
48+
_FMT_PARSE: str = "<xxssx"
49+
PACKET_LENGTH: int = struct.calcsize(_FMT_PARSE)
4450
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
45-
_FMT_CONSTRUCT = "<2sss"
46-
_TYPE_HEADER = b"!B"
51+
_FMT_CONSTRUCT: str = "<2sss"
52+
_TYPE_HEADER: bytes = b"!B"
4753

48-
def __init__(self, button, pressed):
54+
def __init__(self, button: str, pressed: bool) -> None:
4955
"""Construct a ButtonPacket from a button name and the button's state.
5056
5157
:param str button: a single character denoting the button
@@ -59,11 +65,11 @@ def __init__(self, button, pressed):
5965
except Exception as err:
6066
raise ValueError("Button must be a single char.") from err
6167

62-
self._button = button
63-
self._pressed = pressed
68+
self._button: str = button
69+
self._pressed: bool = pressed
6470

6571
@classmethod
66-
def parse_private(cls, packet):
72+
def parse_private(cls, packet: bytes) -> Optional[Packet]:
6773
"""Construct a ButtonPacket from an incoming packet.
6874
Do not call this directly; call Packet.from_bytes() instead.
6975
pylint makes it difficult to call this method _parse(), hence the name.
@@ -73,9 +79,9 @@ def parse_private(cls, packet):
7379
raise ValueError("Bad button press/release value")
7480
return cls(chr(button[0]), pressed == b"1")
7581

76-
def to_bytes(self):
82+
def to_bytes(self) -> bytes:
7783
"""Return the bytes needed to send this packet."""
78-
partial_packet = struct.pack(
84+
partial_packet: bytes = struct.pack(
7985
self._FMT_CONSTRUCT,
8086
self._TYPE_HEADER,
8187
bytes(self._button, "utf-8"),
@@ -84,13 +90,13 @@ def to_bytes(self):
8490
return self.add_checksum(partial_packet)
8591

8692
@property
87-
def button(self):
93+
def button(self) -> str:
8894
"""A single character string (not bytes) specifying the button that
8995
the user pressed or released."""
9096
return self._button
9197

9298
@property
93-
def pressed(self):
99+
def pressed(self) -> bool:
94100
"""``True`` if button is pressed, or ``False`` if it is released."""
95101
return self._pressed
96102

adafruit_bluefruit_connect/packet.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from io import RawIOBase
1919

2020
try:
21-
from typing import Union, Optional, Any # adjust these as needed
21+
from typing import Optional, Any # adjust these as needed
2222
except ImportError:
2323
pass
2424

@@ -133,7 +133,7 @@ def from_stream(cls, stream: RawIOBase) -> Optional[Packet]:
133133
return cls.from_bytes(packet)
134134

135135
@classmethod
136-
def parse_private(cls, packet: bytes) -> Packet:
136+
def parse_private(cls, packet: bytes) -> Optional[Packet]:
137137
"""Default implementation for subclasses.
138138
Assumes arguments to ``__init__()`` are exactly the values parsed using
139139
``cls._FMT_PARSE``. Subclasses may need to reimplement if that assumption

0 commit comments

Comments
 (0)