Skip to content

Commit e18be23

Browse files
authored
Merge pull request #37 from HundredVisionsGuy/type-annotations
Type annotations
2 parents 863951f + 5d95d67 commit e18be23

File tree

10 files changed

+112
-73
lines changed

10 files changed

+112
-73
lines changed

adafruit_bluefruit_connect/_xyz_packet.py

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

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

1719
from .packet import Packet
@@ -20,37 +22,37 @@
2022
class _XYZPacket(Packet):
2123
"""A packet of x, y, z float values. Used for several different Bluefruit controller packets."""
2224

23-
_FMT_PARSE = "<xxfffx"
24-
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
25+
_FMT_PARSE: str = "<xxfffx"
26+
PACKET_LENGTH: int = struct.calcsize(_FMT_PARSE)
2527
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
26-
_FMT_CONSTRUCT = "<2sfff"
28+
_FMT_CONSTRUCT: str = "<2sfff"
2729
# _TYPE_HEADER is set by each concrete subclass.
2830

29-
def __init__(self, x, y, z):
31+
def __init__(self, x: float, y: float, z: float) -> None:
3032
# Construct an _XYZPacket subclass object
3133
# from the given x, y, and z float values, and type character.
3234
self._x = x
3335
self._y = y
3436
self._z = z
3537

36-
def to_bytes(self):
38+
def to_bytes(self) -> bytes:
3739
"""Return the bytes needed to send this packet."""
3840
partial_packet = struct.pack(
3941
self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z
4042
)
4143
return self.add_checksum(partial_packet)
4244

4345
@property
44-
def x(self):
46+
def x(self) -> float:
4547
"""The x value."""
4648
return self._x
4749

4850
@property
49-
def y(self):
51+
def y(self) -> float:
5052
"""The y value."""
5153
return self._y
5254

5355
@property
54-
def z(self):
56+
def z(self) -> float:
5557
"""The z value."""
5658
return self._z

adafruit_bluefruit_connect/accelerometer_packet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
1313
"""
1414

15+
from __future__ import annotations
16+
1517
from ._xyz_packet import _XYZPacket
1618

1719

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

2123
# Everything else is handled by _XYZPacket.
22-
_TYPE_HEADER = b"!A"
24+
_TYPE_HEADER: bytes = b"!A"
2325

2426

2527
# Register this class with the superclass. This allows the user to import only what is needed.

adafruit_bluefruit_connect/button_packet.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,46 @@
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
26+
2027

2128
class ButtonPacket(Packet):
2229
"""A packet containing a button name and its state."""
2330

24-
BUTTON_1 = "1"
31+
BUTTON_1: str = "1"
2532
"""Code for Button 1 on the Bluefruit LE Connect app Control Pad screen."""
26-
BUTTON_2 = "2"
33+
BUTTON_2: str = "2"
2734
"""Button 2."""
28-
BUTTON_3 = "3"
35+
BUTTON_3: str = "3"
2936
"""Button 3."""
30-
BUTTON_4 = "4"
37+
BUTTON_4: str = "4"
3138
"""Button 4."""
3239
# pylint: disable= invalid-name
33-
UP = "5"
40+
UP: str = "5"
3441
"""Up Button."""
35-
DOWN = "6"
42+
DOWN: str = "6"
3643
"""Down Button."""
37-
LEFT = "7"
44+
LEFT: str = "7"
3845
"""Left Button."""
39-
RIGHT = "8"
46+
RIGHT: str = "8"
4047
"""Right Button."""
4148

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

48-
def __init__(self, button, pressed):
55+
def __init__(self, button: str, pressed: bool) -> None:
4956
"""Construct a ButtonPacket from a button name and the button's state.
5057
5158
:param str button: a single character denoting the button
@@ -59,11 +66,11 @@ def __init__(self, button, pressed):
5966
except Exception as err:
6067
raise ValueError("Button must be a single char.") from err
6168

62-
self._button = button
63-
self._pressed = pressed
69+
self._button: str = button
70+
self._pressed: bool = pressed
6471

6572
@classmethod
66-
def parse_private(cls, packet):
73+
def parse_private(cls, packet: bytes) -> Optional[Packet]:
6774
"""Construct a ButtonPacket from an incoming packet.
6875
Do not call this directly; call Packet.from_bytes() instead.
6976
pylint makes it difficult to call this method _parse(), hence the name.
@@ -73,9 +80,9 @@ def parse_private(cls, packet):
7380
raise ValueError("Bad button press/release value")
7481
return cls(chr(button[0]), pressed == b"1")
7582

76-
def to_bytes(self):
83+
def to_bytes(self) -> bytes:
7784
"""Return the bytes needed to send this packet."""
78-
partial_packet = struct.pack(
85+
partial_packet: bytes = struct.pack(
7986
self._FMT_CONSTRUCT,
8087
self._TYPE_HEADER,
8188
bytes(self._button, "utf-8"),
@@ -84,13 +91,13 @@ def to_bytes(self):
8491
return self.add_checksum(partial_packet)
8592

8693
@property
87-
def button(self):
94+
def button(self) -> str:
8895
"""A single character string (not bytes) specifying the button that
8996
the user pressed or released."""
9097
return self._button
9198

9299
@property
93-
def pressed(self):
100+
def pressed(self) -> bool:
94101
"""``True`` if button is pressed, or ``False`` if it is released."""
95102
return self._pressed
96103

adafruit_bluefruit_connect/color_packet.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,58 @@
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, Tuple # adjust these as needed
23+
except ImportError:
24+
pass
25+
1926

2027
class ColorPacket(Packet):
2128
"""A packet containing an RGB color value."""
2229

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

29-
def __init__(self, color):
36+
def __init__(self, color: Tuple) -> None:
3037
"""Construct a ColorPacket from a 3-element :class:`tuple` of RGB
3138
values, or from an int color value 0xRRGGBB.
3239
3340
:param tuple/int color: an RGB :class:`tuple` ``(red, green, blue)``
3441
or an int color value ``0xRRGGBB``
3542
"""
3643
if isinstance(color, int):
37-
self._color = tuple(color.to_bytes(3, "big"))
44+
self._color: Tuple = tuple(color.to_bytes(3, "big"))
3845
elif len(color) == 3 and all(0 <= c <= 255 for c in color):
3946
self._color = color
4047
else:
4148
raise ValueError("Color must be an integer 0xRRGGBB or a tuple(r,g,b)")
4249

4350
@classmethod
44-
def parse_private(cls, packet):
51+
def parse_private(cls, packet: bytes) -> Optional[Packet]:
4552
"""Construct a ColorPacket from an incoming packet.
4653
Do not call this directly; call Packet.from_bytes() instead.
4754
pylint makes it difficult to call this method _parse(), hence the name.
4855
"""
4956
return cls(struct.unpack(cls._FMT_PARSE, packet))
5057

51-
def to_bytes(self):
58+
def to_bytes(self) -> bytes:
5259
"""Return the bytes needed to send this packet."""
5360
partial_packet = struct.pack(
5461
self._FMT_CONSTRUCT, self._TYPE_HEADER, *self._color
5562
)
5663
return self.add_checksum(partial_packet)
5764

5865
@property
59-
def color(self):
66+
def color(self) -> tuple:
6067
"""A :class:`tuple` ``(red, green blue)`` representing the color the
6168
user chose in the BlueFruit Connect app."""
6269
return self._color

adafruit_bluefruit_connect/gyro_packet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
1313
"""
1414

15+
from __future__ import annotations
16+
1517
from ._xyz_packet import _XYZPacket
1618

1719

1820
class GyroPacket(_XYZPacket):
1921
"""A packet of x, y, z float values from a gyroscope."""
2022

2123
# Everything else is handled by _XYZPacket.
22-
_TYPE_HEADER = b"!G"
24+
_TYPE_HEADER: bytes = b"!G"
2325

2426

2527
# Register this class with the superclass. This allows the user to import only what is needed.

adafruit_bluefruit_connect/location_packet.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
1313
"""
1414

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

1719
from .packet import Packet
@@ -20,19 +22,19 @@
2022
class LocationPacket(Packet):
2123
"""A packet of latitude, longitude, and altitude values."""
2224

23-
_FMT_PARSE = "<xxfffx"
24-
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
25+
_FMT_PARSE: str = "<xxfffx"
26+
PACKET_LENGTH: int = struct.calcsize(_FMT_PARSE)
2527
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
26-
_FMT_CONSTRUCT = "<2sfff"
27-
_TYPE_HEADER = b"!L"
28+
_FMT_CONSTRUCT: str = "<2sfff"
29+
_TYPE_HEADER: bytes = b"!L"
2830

29-
def __init__(self, latitude, longitude, altitude):
31+
def __init__(self, latitude: float, longitude: float, altitude: float) -> None:
3032
"""Construct a LocationPacket from the given values."""
3133
self._latitude = latitude
3234
self._longitude = longitude
3335
self._altitude = altitude
3436

35-
def to_bytes(self):
37+
def to_bytes(self) -> bytes:
3638
"""Return the bytes needed to send this packet."""
3739
partial_packet = struct.pack(
3840
self._FMT_CONSTRUCT,
@@ -44,17 +46,17 @@ def to_bytes(self):
4446
return self.add_checksum(partial_packet)
4547

4648
@property
47-
def latitude(self):
49+
def latitude(self) -> float:
4850
"""The latitude value."""
4951
return self._latitude
5052

5153
@property
52-
def longitude(self):
54+
def longitude(self) -> float:
5355
"""The longitude value."""
5456
return self._longitude
5557

5658
@property
57-
def altitude(self):
59+
def altitude(self) -> float:
5860
"""The altitude value."""
5961
return self._altitude
6062

adafruit_bluefruit_connect/magnetometer_packet.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
1313
"""
1414

15+
from __future__ import annotations
16+
1517
from ._xyz_packet import _XYZPacket
1618

1719

1820
class MagnetometerPacket(_XYZPacket):
1921
"""A packet of x, y, z float values from a magnetometer."""
2022

2123
# Everything else is handled by _XYZPacket.
22-
_TYPE_HEADER = b"!M"
24+
_TYPE_HEADER: bytes = b"!M"
2325

2426

2527
# Register this class with the superclass. This allows the user to import only what is needed.

0 commit comments

Comments
 (0)