Skip to content

Commit d5ee528

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 d5ee528

File tree

7 files changed

+25
-16
lines changed

7 files changed

+25
-16
lines changed

adafruit_bluefruit_connect/_xyz_packet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import struct
1818

1919
try:
20-
from typing import Generator, Union, Dict, Optional, Any # adjust these as needed
20+
from typing import Generator, Union, Dict, Optional, Any # adjust these as needed
2121
except ImportError:
2222
pass
2323

adafruit_bluefruit_connect/button_packet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
from .packet import Packet
2121

2222
try:
23-
from typing import Optional # adjust these as needed
23+
from typing import Optional # adjust these as needed
2424
except ImportError:
2525
pass
2626

27+
2728
class ButtonPacket(Packet):
2829
"""A packet containing a button name and its state."""
2930

adafruit_bluefruit_connect/color_packet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
from .packet import Packet
2020

2121
try:
22-
from typing import Optional # adjust these as needed
22+
from typing import Optional # adjust these as needed
2323
except ImportError:
2424
pass
2525

26+
2627
class ColorPacket(Packet):
2728
"""A packet containing an RGB color value."""
2829

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/packet.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
from io import RawIOBase
1919

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

25+
2526
class Packet:
2627
"""
2728
A Bluefruit app controller packet. A packet consists of these bytes, in order:

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)