Skip to content

Commit 71cc92f

Browse files
committed
Type annotate characteristics/stream.py
1 parent c238606 commit 71cc92f

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

adafruit_ble/characteristics/stream.py

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,38 @@
1010
object they are on.
1111
1212
"""
13+
14+
from __future__ import annotations
15+
1316
import _bleio
1417

1518
from . import Attribute
1619
from . import Characteristic
1720
from . import ComplexCharacteristic
1821

22+
try:
23+
from typing import Optional, Union, TYPE_CHECKING
24+
25+
if TYPE_CHECKING:
26+
from circuitpython_typing import ReadableBuffer
27+
from adafruit_ble.characteristics import Characteristic
28+
from adafruit_ble.uuid import UUID
29+
from adafruit_ble.services import Service
30+
31+
except ImportError:
32+
pass
33+
1934
__version__ = "0.0.0-auto.0"
2035
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
2136

2237

2338
class BoundWriteStream:
2439
"""Writes data out to the peer."""
2540

26-
def __init__(self, bound_characteristic):
41+
def __init__(self, bound_characteristic: Characteristic) -> None:
2742
self.bound_characteristic = bound_characteristic
2843

29-
def write(self, buf):
44+
def write(self, buf: ReadableBuffer) -> None:
3045
"""Write data from buf out to the peer."""
3146
# We can only write 20 bytes at a time.
3247
offset = 0
@@ -41,20 +56,20 @@ class StreamOut(ComplexCharacteristic):
4156
def __init__(
4257
self,
4358
*,
44-
uuid=None,
45-
timeout=1.0,
46-
buffer_size=64,
47-
properties=Characteristic.NOTIFY,
48-
read_perm=Attribute.OPEN,
49-
write_perm=Attribute.OPEN
50-
):
59+
uuid: Optional[UUID] = None,
60+
timeout: float = 1.0,
61+
buffer_size: int = 64,
62+
properties: int = Characteristic.NOTIFY,
63+
read_perm: int = Attribute.OPEN,
64+
write_perm: int = Attribute.OPEN,
65+
) -> None:
5166
self._timeout = timeout
5267
self._buffer_size = buffer_size
5368
super().__init__(
5469
uuid=uuid, properties=properties, read_perm=read_perm, write_perm=write_perm
5570
)
5671

57-
def bind(self, service):
72+
def bind(self, service: Service) -> Union[_bleio.Characteristic, BoundWriteStream]:
5873
"""Binds the characteristic to the given Service."""
5974
bound_characteristic = super().bind(service)
6075
# If we're given a remote service then we're the client and need to buffer in.
@@ -74,12 +89,12 @@ class StreamIn(ComplexCharacteristic):
7489
def __init__(
7590
self,
7691
*,
77-
uuid=None,
78-
timeout=1.0,
79-
buffer_size=64,
80-
properties=(Characteristic.WRITE | Characteristic.WRITE_NO_RESPONSE),
81-
write_perm=Attribute.OPEN
82-
):
92+
uuid: Optional[UUID] = None,
93+
timeout: float = 1.0,
94+
buffer_size: int = 64,
95+
properties: int = (Characteristic.WRITE | Characteristic.WRITE_NO_RESPONSE),
96+
write_perm: int = Attribute.OPEN,
97+
) -> None:
8398
self._timeout = timeout
8499
self._buffer_size = buffer_size
85100
super().__init__(
@@ -89,7 +104,9 @@ def __init__(
89104
write_perm=write_perm,
90105
)
91106

92-
def bind(self, service):
107+
def bind(
108+
self, service: Service
109+
) -> Union[_bleio.CharacteristicBuffer, BoundWriteStream]:
93110
"""Binds the characteristic to the given Service."""
94111
bound_characteristic = super().bind(service)
95112
# If the service is remote need to write out.

0 commit comments

Comments
 (0)