Skip to content

Commit e093aba

Browse files
Adding type annotations to several packets
We are still getting mypy complaints about the Liskov Principle, and quarternion due to trying to return the sum of a bytes and an int. Need to flag this as an issue.
1 parent 3171d37 commit e093aba

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

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.

adafruit_bluefruit_connect/quaternion_packet.py

Lines changed: 9 additions & 7 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 ._xyz_packet import _XYZPacket
@@ -23,26 +25,26 @@ class QuaternionPacket(_XYZPacket):
2325

2426
# Use _XYZPacket to handle x, y, z, and add w.
2527

26-
_FMT_PARSE = "<xxffffx"
27-
PACKET_LENGTH = struct.calcsize(_FMT_PARSE)
28+
_FMT_PARSE: str = "<xxffffx"
29+
PACKET_LENGTH: int = struct.calcsize(_FMT_PARSE)
2830
# _FMT_CONSTRUCT doesn't include the trailing checksum byte.
29-
_FMT_CONSTRUCT = "<2sffff"
30-
_TYPE_HEADER = b"!Q"
31+
_FMT_CONSTRUCT: str = "<2sffff"
32+
_TYPE_HEADER: bytes = b"!Q"
3133

32-
def __init__(self, x, y, z, w):
34+
def __init__(self, x: float, y: float, z: float, w: float) -> None:
3335
"""Construct a QuaternionPacket from the given x, y, z, and w float values."""
3436
super().__init__(x, y, z)
3537
self._w = w
3638

37-
def to_bytes(self):
39+
def to_bytes(self) -> bytes:
3840
"""Return the bytes needed to send this packet."""
3941
partial_packet = struct.pack(
4042
self._FMT_CONSTRUCT, self._TYPE_HEADER, self._x, self._y, self._z, self._w
4143
)
4244
return partial_packet + self.checksum(partial_packet)
4345

4446
@property
45-
def w(self):
47+
def w(self) -> float:
4648
"""The w value."""
4749
return self._w
4850

adafruit_bluefruit_connect/raw_text_packet.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,25 @@
2222
2323
"""
2424

25+
from __future__ import annotations
26+
2527
from .packet import Packet
2628

2729

2830
class RawTextPacket(Packet):
2931
"""A packet containing a text string."""
3032

31-
_TYPE_HEADER = b"RT"
33+
_TYPE_HEADER: bytes = b"RT"
3234

33-
def __init__(self, text):
35+
def __init__(self, text: str) -> None:
3436
"""Construct a RawTextPacket from a binary string."""
3537
if isinstance(text, bytes):
36-
self._text = text.strip()
38+
self._text: str = text.strip()
3739
else:
3840
raise ValueError("Text must be a bytes string")
3941

4042
@property
41-
def text(self):
43+
def text(self) -> str:
4244
"""Return the text associated with the object."""
4345
return self._text
4446

0 commit comments

Comments
 (0)