Skip to content

Commit ce8646d

Browse files
committed
Type annotate characteristics/string.py
1 parent 71cc92f commit ce8646d

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

adafruit_ble/characteristics/string.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@
1010
1111
"""
1212

13+
from __future__ import annotations
14+
1315
from . import Attribute
1416
from . import Characteristic
1517

18+
try:
19+
from typing import Optional, Type, Union, TYPE_CHECKING
20+
21+
if TYPE_CHECKING:
22+
from circuitpython_typing import ReadableBuffer
23+
from adafruit_ble.uuid import UUID
24+
from adafruit_ble.services import Service
25+
26+
except ImportError:
27+
pass
28+
1629
__version__ = "0.0.0-auto.0"
1730
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BLE.git"
1831

@@ -23,12 +36,12 @@ class StringCharacteristic(Characteristic):
2336
def __init__(
2437
self,
2538
*,
26-
uuid=None,
27-
properties=Characteristic.READ,
28-
read_perm=Attribute.OPEN,
29-
write_perm=Attribute.OPEN,
30-
initial_value=None
31-
):
39+
uuid: Optional[UUID] = None,
40+
properties: int = Characteristic.READ,
41+
read_perm: int = Attribute.OPEN,
42+
write_perm: int = Attribute.OPEN,
43+
initial_value: Optional[ReadableBuffer] = None,
44+
) -> None:
3245
super().__init__(
3346
uuid=uuid,
3447
properties=properties,
@@ -39,19 +52,23 @@ def __init__(
3952
initial_value=initial_value,
4053
)
4154

42-
def __get__(self, obj, cls=None):
55+
def __get__(
56+
self, obj: Optional[Service], cls: Optional[Type[Service]] = None
57+
) -> Union[str, "StringCharacteristic"]:
4358
if obj is None:
4459
return self
4560
return str(super().__get__(obj, cls), "utf-8")
4661

47-
def __set__(self, obj, value):
62+
def __set__(self, obj: Service, value: str) -> None:
4863
super().__set__(obj, value.encode("utf-8"))
4964

5065

5166
class FixedStringCharacteristic(Characteristic):
5267
"""Fixed strings are set once when bound and unchanged after."""
5368

54-
def __init__(self, *, uuid=None, read_perm=Attribute.OPEN):
69+
def __init__(
70+
self, *, uuid: Optional[UUID] = None, read_perm: int = Attribute.OPEN
71+
) -> None:
5572
super().__init__(
5673
uuid=uuid,
5774
properties=Characteristic.READ,
@@ -60,7 +77,9 @@ def __init__(self, *, uuid=None, read_perm=Attribute.OPEN):
6077
fixed_length=True,
6178
)
6279

63-
def __get__(self, obj, cls=None):
80+
def __get__(
81+
self, obj: Service, cls: Optional[Type[Service]] = None
82+
) -> Union[str, "FixedStringCharacteristic"]:
6483
if obj is None:
6584
return self
6685
return str(super().__get__(obj, cls), "utf-8")

0 commit comments

Comments
 (0)