Skip to content

Commit 9108584

Browse files
committed
Add type annotations
1 parent dc54233 commit 9108584

File tree

7 files changed

+143
-48
lines changed

7 files changed

+143
-48
lines changed

adafruit_register/i2c_bcd_alarm.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,27 @@
1717

1818
import time
1919

20+
try:
21+
from typing import Optional, Type, Tuple
22+
from typing_extensions import Literal
23+
from circuitpython_typing.device_drivers import I2CDeviceDriver
2024

21-
def _bcd2bin(value):
25+
FREQUENCY_T = Literal[
26+
"monthly", "weekly", "daily", "hourly", "secondly", "minutely"
27+
]
28+
except ImportError:
29+
pass
30+
31+
32+
def _bcd2bin(value: int) -> int:
2233
"""Convert binary coded decimal to Binary
2334
2435
:param value: the BCD value to convert to binary (required, no default)
2536
"""
2637
return value - 6 * (value >> 4)
2738

2839

29-
def _bin2bcd(value):
40+
def _bin2bcd(value: int) -> int:
3041
"""Convert a binary value to binary coded decimal.
3142
3243
:param value: the binary value to convert to BCD. (required, no default)
@@ -68,8 +79,12 @@ class BCDAlarmTimeRegister:
6879

6980
# Defaults are based on alarm1 of the DS3231.
7081
def __init__(
71-
self, register_address, has_seconds=True, weekday_shared=True, weekday_start=1
72-
):
82+
self,
83+
register_address: int,
84+
has_seconds: bool = True,
85+
weekday_shared: bool = True,
86+
weekday_start: Literal[0, 1] = 1,
87+
) -> None:
7388
buffer_size = 5
7489
if weekday_shared:
7590
buffer_size -= 1
@@ -81,7 +96,11 @@ def __init__(
8196
self.weekday_shared = weekday_shared
8297
self.weekday_start = weekday_start
8398

84-
def __get__(self, obj, objtype=None):
99+
def __get__(
100+
self,
101+
obj: Optional[I2CDeviceDriver],
102+
objtype: Optional[Type[I2CDeviceDriver]] = None,
103+
) -> Tuple[time.struct_time, FREQUENCY_T]:
85104
# Read the alarm register.
86105
with obj.i2c_device as i2c:
87106
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
@@ -136,7 +155,9 @@ def __get__(self, obj, objtype=None):
136155
frequency,
137156
)
138157

139-
def __set__(self, obj, value):
158+
def __set__(
159+
self, obj: I2CDeviceDriver, value: Tuple[time.struct_time, FREQUENCY_T]
160+
) -> None:
140161
if len(value) != 2:
141162
raise ValueError("Value must be sequence of length two")
142163
# Turn all components off by default.

adafruit_register/i2c_bcd_datetime.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@
1717

1818
import time
1919

20+
try:
21+
from typing import Optional, Type
22+
from typing_extensions import Literal
23+
from circuitpython_typing.device_drivers import I2CDeviceDriver
24+
except ImportError:
25+
pass
2026

21-
def _bcd2bin(value):
27+
28+
def _bcd2bin(value: int) -> int:
2229
"""Convert binary coded decimal to Binary
2330
2431
:param value: the BCD value to convert to binary (required, no default)
2532
"""
2633
return value - 6 * (value >> 4)
2734

2835

29-
def _bin2bcd(value):
36+
def _bin2bcd(value: int) -> int:
3037
"""Convert a binary value to binary coded decimal.
3138
3239
:param value: the binary value to convert to BCD. (required, no default)
@@ -52,7 +59,12 @@ class BCDDateTimeRegister:
5259
week
5360
"""
5461

55-
def __init__(self, register_address, weekday_first=True, weekday_start=1):
62+
def __init__(
63+
self,
64+
register_address: int,
65+
weekday_first: bool = True,
66+
weekday_start: Literal[0, 1] = 1,
67+
) -> None:
5668
self.buffer = bytearray(8)
5769
self.buffer[0] = register_address
5870
if weekday_first:
@@ -63,7 +75,11 @@ def __init__(self, register_address, weekday_first=True, weekday_start=1):
6375
# Masking value list n/a sec min hr day wkday mon year
6476
self.mask_datetime = b"\xFF\x7F\x7F\x3F\x3F\x07\x1F\xFF"
6577

66-
def __get__(self, obj, objtype=None):
78+
def __get__(
79+
self,
80+
obj: Optional[I2CDeviceDriver],
81+
objtype: Optional[Type[I2CDeviceDriver]] = None,
82+
) -> time.struct_time:
6783
# Read and return the date and time.
6884
with obj.i2c_device as i2c:
6985
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
@@ -84,7 +100,7 @@ def __get__(self, obj, objtype=None):
84100
)
85101
)
86102

87-
def __set__(self, obj, value):
103+
def __set__(self, obj: I2CDeviceDriver, value: time.struct_time) -> None:
88104
self.buffer[1] = _bin2bcd(value.tm_sec) & 0x7F # format conversions
89105
self.buffer[2] = _bin2bcd(value.tm_min)
90106
self.buffer[3] = _bin2bcd(value.tm_hour)

adafruit_register/i2c_bit.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
__version__ = "0.0.0+auto.0"
1616
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git"
1717

18+
try:
19+
from typing import Optional, Type, NoReturn
20+
from circuitpython_typing.device_drivers import I2CDeviceDriver
21+
except ImportError:
22+
pass
23+
1824

1925
class RWBit:
2026
"""
@@ -23,13 +29,19 @@ class RWBit:
2329
Values are `bool`
2430
2531
:param int register_address: The register address to read the bit from
26-
:param type bit: The bit index within the byte at ``register_address``
32+
:param int bit: The bit index within the byte at ``register_address``
2733
:param int register_width: The number of bytes in the register. Defaults to 1.
2834
:param bool lsb_first: Is the first byte we read from I2C the LSB? Defaults to true
2935
3036
"""
3137

32-
def __init__(self, register_address, bit, register_width=1, lsb_first=True):
38+
def __init__(
39+
self,
40+
register_address: int,
41+
bit: int,
42+
register_width: int = 1,
43+
lsb_first: bool = True,
44+
) -> None:
3345
self.bit_mask = 1 << (bit % 8) # the bitmask *within* the byte!
3446
self.buffer = bytearray(1 + register_width)
3547
self.buffer[0] = register_address
@@ -38,12 +50,16 @@ def __init__(self, register_address, bit, register_width=1, lsb_first=True):
3850
else:
3951
self.byte = register_width - (bit // 8) # the byte number within the buffer
4052

41-
def __get__(self, obj, objtype=None):
53+
def __get__(
54+
self,
55+
obj: Optional[I2CDeviceDriver],
56+
objtype: Optional[Type[I2CDeviceDriver]] = None,
57+
) -> bool:
4258
with obj.i2c_device as i2c:
4359
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
4460
return bool(self.buffer[self.byte] & self.bit_mask)
4561

46-
def __set__(self, obj, value):
62+
def __set__(self, obj: I2CDeviceDriver, value: bool) -> None:
4763
with obj.i2c_device as i2c:
4864
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
4965
if value:
@@ -64,5 +80,5 @@ class ROBit(RWBit):
6480
6581
"""
6682

67-
def __set__(self, obj, value):
83+
def __set__(self, obj: I2CDeviceDriver, value: bool) -> NoReturn:
6884
raise AttributeError()

adafruit_register/i2c_bits.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
__version__ = "0.0.0+auto.0"
1616
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git"
1717

18+
try:
19+
from typing import Optional, Type, NoReturn
20+
from circuitpython_typing.device_drivers import I2CDeviceDriver
21+
except ImportError:
22+
pass
23+
1824

1925
class RWBits:
2026
"""
@@ -25,7 +31,7 @@ class RWBits:
2531
2632
:param int num_bits: The number of bits in the field.
2733
:param int register_address: The register address to read the bit from
28-
:param type lowest_bit: The lowest bits index within the byte at ``register_address``
34+
:param int lowest_bit: The lowest bits index within the byte at ``register_address``
2935
:param int register_width: The number of bytes in the register. Defaults to 1.
3036
:param bool lsb_first: Is the first byte we read from I2C the LSB? Defaults to true
3137
:param bool signed: If True, the value is a "two's complement" signed value.
@@ -34,13 +40,13 @@ class RWBits:
3440

3541
def __init__( # pylint: disable=too-many-arguments
3642
self,
37-
num_bits,
38-
register_address,
39-
lowest_bit,
40-
register_width=1,
41-
lsb_first=True,
42-
signed=False,
43-
):
43+
num_bits: int,
44+
register_address: int,
45+
lowest_bit: int,
46+
register_width: int = 1,
47+
lsb_first: bool = True,
48+
signed: bool = False,
49+
) -> None:
4450
self.bit_mask = ((1 << num_bits) - 1) << lowest_bit
4551
# print("bitmask: ",hex(self.bit_mask))
4652
if self.bit_mask >= 1 << (register_width * 8):
@@ -51,7 +57,11 @@ def __init__( # pylint: disable=too-many-arguments
5157
self.lsb_first = lsb_first
5258
self.sign_bit = (1 << (num_bits - 1)) if signed else 0
5359

54-
def __get__(self, obj, objtype=None):
60+
def __get__(
61+
self,
62+
obj: Optional[I2CDeviceDriver],
63+
objtype: Optional[Type[I2CDeviceDriver]] = None,
64+
) -> int:
5565
with obj.i2c_device as i2c:
5666
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
5767
# read the number of bytes into a single variable
@@ -67,7 +77,7 @@ def __get__(self, obj, objtype=None):
6777
reg -= 2 * self.sign_bit
6878
return reg
6979

70-
def __set__(self, obj, value):
80+
def __set__(self, obj: I2CDeviceDriver, value: int) -> None:
7181
value <<= self.lowest_bit # shift the value over to the right spot
7282
with obj.i2c_device as i2c:
7383
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
@@ -100,5 +110,5 @@ class ROBits(RWBits):
100110
:param int register_width: The number of bytes in the register. Defaults to 1.
101111
"""
102112

103-
def __set__(self, obj, value):
113+
def __set__(self, obj: I2CDeviceDriver, value: int) -> NoReturn:
104114
raise AttributeError()

adafruit_register/i2c_struct.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
* Author(s): Scott Shawcroft
1313
"""
1414

15-
import struct
16-
1715
__version__ = "0.0.0+auto.0"
1816
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git"
1917

18+
import struct
19+
20+
try:
21+
from typing import Optional, Type, Tuple, Any, NoReturn
22+
from circuitpython_typing.device_drivers import I2CDeviceDriver
23+
except ImportError:
24+
pass
25+
2026

2127
class Struct:
2228
"""
@@ -26,20 +32,24 @@ class Struct:
2632
module documentation for struct format string and its possible value types.
2733
2834
:param int register_address: The register address to read the bit from
29-
:param type struct_format: The struct format string for this register.
35+
:param str struct_format: The struct format string for this register.
3036
"""
3137

32-
def __init__(self, register_address, struct_format):
38+
def __init__(self, register_address: int, struct_format: str) -> None:
3339
self.format = struct_format
3440
self.buffer = bytearray(1 + struct.calcsize(self.format))
3541
self.buffer[0] = register_address
3642

37-
def __get__(self, obj, objtype=None):
43+
def __get__(
44+
self,
45+
obj: Optional[I2CDeviceDriver],
46+
objtype: Optional[Type[I2CDeviceDriver]] = None,
47+
) -> Tuple:
3848
with obj.i2c_device as i2c:
3949
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
4050
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])
4151

42-
def __set__(self, obj, value):
52+
def __set__(self, obj: I2CDeviceDriver, value: Tuple) -> None:
4353
struct.pack_into(self.format, self.buffer, 1, *value)
4454
with obj.i2c_device as i2c:
4555
i2c.write(self.buffer)
@@ -53,21 +63,25 @@ class UnaryStruct:
5363
module documentation for struct format string and its possible value types.
5464
5565
:param int register_address: The register address to read the bit from
56-
:param type struct_format: The struct format string for this register.
66+
:param str struct_format: The struct format string for this register.
5767
"""
5868

59-
def __init__(self, register_address, struct_format):
69+
def __init__(self, register_address: int, struct_format: str) -> None:
6070
self.format = struct_format
6171
self.address = register_address
6272

63-
def __get__(self, obj, objtype=None):
73+
def __get__(
74+
self,
75+
obj: Optional[I2CDeviceDriver],
76+
objtype: Optional[Type[I2CDeviceDriver]] = None,
77+
) -> Any:
6478
buf = bytearray(1 + struct.calcsize(self.format))
6579
buf[0] = self.address
6680
with obj.i2c_device as i2c:
6781
i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
6882
return struct.unpack_from(self.format, buf, 1)[0]
6983

70-
def __set__(self, obj, value):
84+
def __set__(self, obj: I2CDeviceDriver, value: Any) -> None:
7185
buf = bytearray(1 + struct.calcsize(self.format))
7286
buf[0] = self.address
7387
struct.pack_into(self.format, buf, 1, value)
@@ -86,5 +100,5 @@ class ROUnaryStruct(UnaryStruct):
86100
:param type struct_format: The struct format string for this register.
87101
"""
88102

89-
def __set__(self, obj, value):
103+
def __set__(self, obj: I2CDeviceDriver, value: Any) -> NoReturn:
90104
raise AttributeError()

0 commit comments

Comments
 (0)