Skip to content

Commit 0c9849f

Browse files
committed
Add typehints
Still missing typehint for traceback argument in DCMotor.__exit__()
1 parent 2fbf20f commit 0c9849f

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

adafruit_motor/motor.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
* Author(s): Scott Shawcroft
2121
"""
2222

23+
try:
24+
from typing import Optional, Type
25+
from pwmio import PWMOut
26+
except ImportError:
27+
pass
28+
2329
__version__ = "0.0.0-auto.0"
2430
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Motor.git"
2531

@@ -48,7 +54,7 @@ class DCMotor:
4854
:param ~pwmio.PWMOut negative_pwm: The motor input that causes the motor to spin backwards
4955
when high and the other is low."""
5056

51-
def __init__(self, positive_pwm, negative_pwm):
57+
def __init__(self, positive_pwm: PWMOut, negative_pwm: PWMOut):
5258
self._positive = positive_pwm
5359
self._negative = negative_pwm
5460
self._throttle = None
@@ -63,7 +69,7 @@ def throttle(self):
6369
return self._throttle
6470

6571
@throttle.setter
66-
def throttle(self, value):
72+
def throttle(self, value: Optional[float]):
6773
if value is not None and (value > 1.0 or value < -1.0):
6874
raise ValueError("Throttle must be None or between -1.0 and +1.0")
6975
self._throttle = value
@@ -98,7 +104,7 @@ def decay_mode(self):
98104
return self._decay_mode
99105

100106
@decay_mode.setter
101-
def decay_mode(self, mode=FAST_DECAY):
107+
def decay_mode(self, mode: int = FAST_DECAY):
102108
if mode in (FAST_DECAY, SLOW_DECAY):
103109
self._decay_mode = mode
104110
else:
@@ -109,5 +115,5 @@ def decay_mode(self, mode=FAST_DECAY):
109115
def __enter__(self):
110116
return self
111117

112-
def __exit__(self, exception_type, exception_value, traceback):
118+
def __exit__(self, exception_type: Optional[Type[type]], exception_value: Optional[BaseException], traceback): # TODO: Add traceback typing
113119
self.throttle = None

adafruit_motor/servo.py

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

15+
try:
16+
from typing import Optional
17+
from pwmio import PWMOut
18+
except ImportError:
19+
pass
20+
21+
1522
__version__ = "0.0.0-auto.0"
1623
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Motor.git"
1724

@@ -24,11 +31,11 @@ class _BaseServo: # pylint: disable-msg=too-few-public-methods
2431
:param int min_pulse: The minimum pulse length of the servo in microseconds.
2532
:param int max_pulse: The maximum pulse length of the servo in microseconds."""
2633

27-
def __init__(self, pwm_out, *, min_pulse=750, max_pulse=2250):
34+
def __init__(self, pwm_out: PWMOut, *, min_pulse: int = 750, max_pulse: int = 2250):
2835
self._pwm_out = pwm_out
2936
self.set_pulse_width_range(min_pulse, max_pulse)
3037

31-
def set_pulse_width_range(self, min_pulse=750, max_pulse=2250):
38+
def set_pulse_width_range(self, min_pulse: int = 750, max_pulse: int = 2250):
3239
"""Change min and max pulse widths."""
3340
self._min_duty = int((min_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF)
3441
max_duty = (max_pulse * self._pwm_out.frequency) / 1000000 * 0xFFFF
@@ -45,7 +52,7 @@ def fraction(self):
4552
return (self._pwm_out.duty_cycle - self._min_duty) / self._duty_range
4653

4754
@fraction.setter
48-
def fraction(self, value):
55+
def fraction(self, value: Optional[float]):
4956
if value is None:
5057
self._pwm_out.duty_cycle = 0 # disable the motor
5158
return
@@ -85,7 +92,7 @@ class Servo(_BaseServo):
8592
Test carefully to find the safe minimum and maximum.
8693
"""
8794

88-
def __init__(self, pwm_out, *, actuation_range=180, min_pulse=750, max_pulse=2250):
95+
def __init__(self, pwm_out: PWMOut, *, actuation_range: int = 180, min_pulse: int = 750, max_pulse: int = 2250):
8996
super().__init__(pwm_out, min_pulse=min_pulse, max_pulse=max_pulse)
9097
self.actuation_range = actuation_range
9198
"""The physical range of motion of the servo in degrees."""
@@ -100,7 +107,7 @@ def angle(self):
100107
return self.actuation_range * self.fraction
101108

102109
@angle.setter
103-
def angle(self, new_angle):
110+
def angle(self, new_angle: Optional[int]):
104111
if new_angle is None: # disable the servo by sending 0 signal
105112
self.fraction = None
106113
return
@@ -123,7 +130,7 @@ def throttle(self):
123130
return self.fraction * 2 - 1
124131

125132
@throttle.setter
126-
def throttle(self, value):
133+
def throttle(self, value: float):
127134
if value > 1.0 or value < -1.0:
128135
raise ValueError("Throttle must be between -1.0 and 1.0")
129136
if value is None:

adafruit_motor/stepper.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
from micropython import const
2020

21+
try:
22+
from typing import Union, Optional
23+
from pwmio import PWMOut
24+
from digitalio import DigitalInOut
25+
except ImportError:
26+
pass
27+
2128
__version__ = "0.0.0-auto.0"
2229
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Motor.git"
2330

@@ -79,7 +86,7 @@ class StepperMotor:
7986
:param microsteps: set to `None`
8087
"""
8188

82-
def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):
89+
def __init__(self, ain1: Union[PWMOut, DigitalInOut], ain2: Union[PWMOut, DigitalInOut], bin1: Union[PWMOut, DigitalInOut], bin2: Union[PWMOut, DigitalInOut], *, microsteps: Optional[int] = 16):
8390
if microsteps is None:
8491
#
8592
# Digital IO Pins
@@ -107,7 +114,7 @@ def __init__(self, ain1, ain2, bin1, bin2, *, microsteps=16):
107114
self._microsteps = microsteps
108115
self._update_coils()
109116

110-
def _update_coils(self, *, microstepping=False):
117+
def _update_coils(self, *, microstepping: bool = False):
111118
if self._microsteps is None:
112119
#
113120
# Digital IO Pins
@@ -154,7 +161,7 @@ def release(self):
154161
coil.duty_cycle = 0
155162

156163
def onestep(
157-
self, *, direction=FORWARD, style=SINGLE
164+
self, *, direction: int = FORWARD, style: int = SINGLE
158165
): # pylint: disable=too-many-branches
159166
"""Performs one step of a particular style. The actual rotation amount will vary by style.
160167
`SINGLE` and `DOUBLE` will normal cause a full step rotation. `INTERLEAVE` will normally

0 commit comments

Comments
 (0)