Skip to content

Add type annotations #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions adafruit_servokit.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@
import board
from adafruit_pca9685 import PCA9685

try:
from typing import Optional
from busio import I2C
from adafruit_motor.servo import Servo, ContinuousServo
except ImportError:
pass

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ServoKit.git"

Expand All @@ -54,6 +61,8 @@ class ServoKit:

:param int channels: The number of servo channels available. Must be 8 or 16. The FeatherWing
has 8 channels. The Shield, HAT, and Bonnet have 16 channels.
:param ~I2C i2c: The I2C bus to use. If not provided, it will use generate the default I2C
bus singleton ``busio.I2C()`` and use that.
:param int address: The I2C address of the PCA9685. Default address is ``0x40``.
:param int reference_clock_speed: The frequency of the internal reference clock in Hertz.
Default reference clock speed is ``25000000``.
Expand All @@ -65,12 +74,12 @@ class ServoKit:
def __init__(
self,
*,
channels,
i2c=None,
address=0x40,
reference_clock_speed=25000000,
frequency=50
):
channels: int,
i2c: Optional[I2C] = None,
address: int = 0x40,
reference_clock_speed: int = 25000000,
frequency: int = 50
) -> None:
if channels not in [8, 16]:
raise ValueError("servo_channels must be 8 or 16!")
self._items = [None] * channels
Expand All @@ -86,7 +95,7 @@ def __init__(
self._continuous_servo = _ContinuousServo(self)

@property
def servo(self):
def servo(self) -> "_Servo":
""":class:`~adafruit_motor.servo.Servo` controls for standard servos.

This FeatherWing example rotates a servo on channel ``0`` to ``180`` degrees for one second,
Expand All @@ -107,7 +116,7 @@ def servo(self):
return self._servo

@property
def continuous_servo(self):
def continuous_servo(self) -> "_ContinuousServo":
""":class:`~adafruit_motor.servo.ContinuousServo` controls for continuous rotation
servos.

Expand All @@ -133,10 +142,10 @@ def continuous_servo(self):

class _Servo:
# pylint: disable=protected-access
def __init__(self, kit):
def __init__(self, kit: ServoKit) -> None:
self.kit = kit

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

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

def __len__(self):
def __len__(self) -> int:
return len(self.kit._items)


class _ContinuousServo:
# pylint: disable=protected-access
def __init__(self, kit):
def __init__(self, kit: ServoKit) -> None:
self.kit = kit

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

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

def __len__(self):
def __len__(self) -> int:
return len(self.kit._items)