Skip to content

Commit 67982ff

Browse files
authored
Merge pull request #37 from tekktrik/doc/add-typing
Add type annotations
2 parents fc0878f + 3ddcac6 commit 67982ff

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

adafruit_servokit.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535
import board
3636
from adafruit_pca9685 import PCA9685
3737

38+
try:
39+
from typing import Optional
40+
from busio import I2C
41+
from adafruit_motor.servo import Servo, ContinuousServo
42+
except ImportError:
43+
pass
44+
3845
__version__ = "0.0.0-auto.0"
3946
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ServoKit.git"
4047

@@ -54,6 +61,8 @@ class ServoKit:
5461
5562
:param int channels: The number of servo channels available. Must be 8 or 16. The FeatherWing
5663
has 8 channels. The Shield, HAT, and Bonnet have 16 channels.
64+
:param ~I2C i2c: The I2C bus to use. If not provided, it will use generate the default I2C
65+
bus singleton ``busio.I2C()`` and use that.
5766
:param int address: The I2C address of the PCA9685. Default address is ``0x40``.
5867
:param int reference_clock_speed: The frequency of the internal reference clock in Hertz.
5968
Default reference clock speed is ``25000000``.
@@ -65,12 +74,12 @@ class ServoKit:
6574
def __init__(
6675
self,
6776
*,
68-
channels,
69-
i2c=None,
70-
address=0x40,
71-
reference_clock_speed=25000000,
72-
frequency=50
73-
):
77+
channels: int,
78+
i2c: Optional[I2C] = None,
79+
address: int = 0x40,
80+
reference_clock_speed: int = 25000000,
81+
frequency: int = 50
82+
) -> None:
7483
if channels not in [8, 16]:
7584
raise ValueError("servo_channels must be 8 or 16!")
7685
self._items = [None] * channels
@@ -86,7 +95,7 @@ def __init__(
8695
self._continuous_servo = _ContinuousServo(self)
8796

8897
@property
89-
def servo(self):
98+
def servo(self) -> "_Servo":
9099
""":class:`~adafruit_motor.servo.Servo` controls for standard servos.
91100
92101
This FeatherWing example rotates a servo on channel ``0`` to ``180`` degrees for one second,
@@ -107,7 +116,7 @@ def servo(self):
107116
return self._servo
108117

109118
@property
110-
def continuous_servo(self):
119+
def continuous_servo(self) -> "_ContinuousServo":
111120
""":class:`~adafruit_motor.servo.ContinuousServo` controls for continuous rotation
112121
servos.
113122
@@ -133,10 +142,10 @@ def continuous_servo(self):
133142

134143
class _Servo:
135144
# pylint: disable=protected-access
136-
def __init__(self, kit):
145+
def __init__(self, kit: ServoKit) -> None:
137146
self.kit = kit
138147

139-
def __getitem__(self, servo_channel):
148+
def __getitem__(self, servo_channel: int) -> Servo:
140149
import adafruit_motor.servo # pylint: disable=import-outside-toplevel
141150

142151
num_channels = self.kit._channels
@@ -151,16 +160,16 @@ def __getitem__(self, servo_channel):
151160
return servo
152161
raise ValueError("Channel {} is already in use.".format(servo_channel))
153162

154-
def __len__(self):
163+
def __len__(self) -> int:
155164
return len(self.kit._items)
156165

157166

158167
class _ContinuousServo:
159168
# pylint: disable=protected-access
160-
def __init__(self, kit):
169+
def __init__(self, kit: ServoKit) -> None:
161170
self.kit = kit
162171

163-
def __getitem__(self, servo_channel):
172+
def __getitem__(self, servo_channel: int) -> ContinuousServo:
164173
import adafruit_motor.servo # pylint: disable=import-outside-toplevel
165174

166175
num_channels = self.kit._channels
@@ -179,5 +188,5 @@ def __getitem__(self, servo_channel):
179188
return servo
180189
raise ValueError("Channel {} is already in use.".format(servo_channel))
181190

182-
def __len__(self):
191+
def __len__(self) -> int:
183192
return len(self.kit._items)

0 commit comments

Comments
 (0)