Skip to content

Commit a093229

Browse files
authored
Merge pull request #86 from careyk007/add_missing_type_annotations
2 parents db23827 + 211b1e6 commit a093229

File tree

6 files changed

+68
-32
lines changed

6 files changed

+68
-32
lines changed

adafruit_hid/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@
1818
"""
1919

2020
# imports
21+
try:
22+
from typing import Sequence
23+
import usb_hid
24+
except ImportError:
25+
pass
2126

2227
__version__ = "0.0.0-auto.0"
2328
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_HID.git"
2429

2530

26-
def find_device(devices, *, usage_page, usage):
27-
"""Search through the provided list of devices to find the one with the matching usage_page and
28-
usage."""
31+
def find_device(
32+
devices: Sequence[usb_hid.device], *, usage_page: int, usage: int
33+
) -> usb_hid.device:
34+
"""Search through the provided sequence of devices to find the one with the matching
35+
usage_page and usage."""
2936
if hasattr(devices, "send_report"):
3037
devices = [devices]
3138
for device in devices:

adafruit_hid/consumer_control.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
import time
2222
from . import find_device
2323

24+
try:
25+
from typing import Sequence
26+
import usb_hid
27+
except ImportError:
28+
pass
29+
2430

2531
class ConsumerControl:
2632
"""Send ConsumerControl code reports, used by multimedia keyboards, remote controls, etc."""
2733

28-
def __init__(self, devices):
34+
def __init__(self, devices: Sequence[usb_hid.device]) -> None:
2935
"""Create a ConsumerControl object that will send Consumer Control Device HID reports.
3036
31-
Devices can be a list of devices that includes a Consumer Control device or a CC device
37+
Devices can be a sequence of devices that includes a Consumer Control device or a CC device
3238
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
3339
``usage``.
3440
"""
@@ -45,7 +51,7 @@ def __init__(self, devices):
4551
time.sleep(1)
4652
self.send(0x0)
4753

48-
def send(self, consumer_code):
54+
def send(self, consumer_code: int) -> None:
4955
"""Send a report to do the specified consumer control action,
5056
and then stop the action (so it will not repeat).
5157
@@ -64,7 +70,7 @@ def send(self, consumer_code):
6470
self.press(consumer_code)
6571
self.release()
6672

67-
def press(self, consumer_code):
73+
def press(self, consumer_code: int) -> None:
6874
"""Send a report to indicate that the given key has been pressed.
6975
Only one consumer control action can be pressed at a time, so any one
7076
that was previously pressed will be released.
@@ -83,7 +89,7 @@ def press(self, consumer_code):
8389
struct.pack_into("<H", self._report, 0, consumer_code)
8490
self._consumer_device.send_report(self._report)
8591

86-
def release(self):
92+
def release(self) -> None:
8793
"""Send a report indicating that the consumer control key has been
8894
released. Only one consumer control key can be pressed at a time.
8995

adafruit_hid/keyboard.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
from . import find_device
1818

19+
try:
20+
from typing import Sequence
21+
import usb_hid
22+
except ImportError:
23+
pass
24+
1925
_MAX_KEYPRESSES = const(6)
2026

2127

@@ -33,10 +39,10 @@ class Keyboard:
3339

3440
# No more than _MAX_KEYPRESSES regular keys may be pressed at once.
3541

36-
def __init__(self, devices):
42+
def __init__(self, devices: Sequence[usb_hid.device]) -> None:
3743
"""Create a Keyboard object that will send keyboard HID reports.
3844
39-
Devices can be a list of devices that includes a keyboard device or a keyboard device
45+
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
4046
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
4147
``usage``.
4248
"""
@@ -64,7 +70,7 @@ def __init__(self, devices):
6470
time.sleep(1)
6571
self.release_all()
6672

67-
def press(self, *keycodes):
73+
def press(self, *keycodes: int) -> None:
6874
"""Send a report indicating that the given keys have been pressed.
6975
7076
:param keycodes: Press these keycodes all at once.
@@ -90,7 +96,7 @@ def press(self, *keycodes):
9096
self._add_keycode_to_report(keycode)
9197
self._keyboard_device.send_report(self.report)
9298

93-
def release(self, *keycodes):
99+
def release(self, *keycodes: int) -> None:
94100
"""Send a USB HID report indicating that the given keys have been released.
95101
96102
:param keycodes: Release these keycodes all at once.
@@ -106,21 +112,21 @@ def release(self, *keycodes):
106112
self._remove_keycode_from_report(keycode)
107113
self._keyboard_device.send_report(self.report)
108114

109-
def release_all(self):
115+
def release_all(self) -> None:
110116
"""Release all pressed keys."""
111117
for i in range(8):
112118
self.report[i] = 0
113119
self._keyboard_device.send_report(self.report)
114120

115-
def send(self, *keycodes):
121+
def send(self, *keycodes: int) -> None:
116122
"""Press the given keycodes and then release all pressed keys.
117123
118124
:param keycodes: keycodes to send together
119125
"""
120126
self.press(*keycodes)
121127
self.release_all()
122128

123-
def _add_keycode_to_report(self, keycode):
129+
def _add_keycode_to_report(self, keycode: int) -> None:
124130
"""Add a single keycode to the USB HID report."""
125131
modifier = Keycode.modifier_bit(keycode)
126132
if modifier:
@@ -141,7 +147,7 @@ def _add_keycode_to_report(self, keycode):
141147
# All slots are filled.
142148
raise ValueError("Trying to press more than six keys at once.")
143149

144-
def _remove_keycode_from_report(self, keycode):
150+
def _remove_keycode_from_report(self, keycode: int) -> None:
145151
"""Remove a single keycode from the report."""
146152
modifier = Keycode.modifier_bit(keycode)
147153
if modifier:
@@ -154,11 +160,11 @@ def _remove_keycode_from_report(self, keycode):
154160
self.report_keys[i] = 0
155161

156162
@property
157-
def led_status(self):
163+
def led_status(self) -> bytes:
158164
"""Returns the last received report"""
159165
return self._keyboard_device.last_received_report
160166

161-
def led_on(self, led_code):
167+
def led_on(self, led_code: int) -> bool:
162168
"""Returns whether an LED is on based on the led code
163169
164170
Examples::

adafruit_hid/keyboard_layout_us.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
from .keycode import Keycode
1313

14+
try:
15+
from .keyboard import Keyboard
16+
from typing import Tuple
17+
except ImportError:
18+
pass
19+
1420

1521
class KeyboardLayoutUS:
1622
"""Map ASCII characters to appropriate keypresses on a standard US PC keyboard.
@@ -163,7 +169,7 @@ class KeyboardLayoutUS:
163169
b"\x4c" # DEL DELETE (called Forward Delete in usb.org document)
164170
)
165171

166-
def __init__(self, keyboard):
172+
def __init__(self, keyboard: Keyboard) -> None:
167173
"""Specify the layout for the given keyboard.
168174
169175
:param keyboard: a Keyboard object. Write characters to this keyboard when requested.
@@ -176,7 +182,7 @@ def __init__(self, keyboard):
176182

177183
self.keyboard = keyboard
178184

179-
def write(self, string):
185+
def write(self, string: str) -> None:
180186
"""Type the string by pressing and releasing keys on my keyboard.
181187
182188
:param string: A string of ASCII characters.
@@ -197,7 +203,7 @@ def write(self, string):
197203
self.keyboard.press(keycode)
198204
self.keyboard.release_all()
199205

200-
def keycodes(self, char):
206+
def keycodes(self, char: str) -> Tuple[int, ...]:
201207
"""Return a tuple of keycodes needed to type the given character.
202208
203209
:param char: A single ASCII character in a string.
@@ -222,7 +228,7 @@ def keycodes(self, char):
222228

223229
return (keycode,)
224230

225-
def _char_to_keycode(self, char):
231+
def _char_to_keycode(self, char: str) -> int:
226232
"""Return the HID keycode for the given ASCII character, with the SHIFT_FLAG possibly set.
227233
228234
If the character requires pressing the Shift key, the SHIFT_FLAG bit is set.

adafruit_hid/keycode.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* Author(s): Scott Shawcroft, Dan Halbert
1010
"""
1111

12+
try:
13+
import typing # pylint: disable=unused-import
14+
except ImportError:
15+
pass
16+
1217

1318
class Keycode:
1419
"""USB HID Keycode constants.
@@ -299,7 +304,7 @@ class Keycode:
299304

300305
# pylint: enable-msg=invalid-name
301306
@classmethod
302-
def modifier_bit(cls, keycode):
307+
def modifier_bit(cls, keycode: int) -> int:
303308
"""Return the modifer bit to be set in an HID keycode report if this is a
304309
modifier key; otherwise return 0."""
305310
return (

adafruit_hid/mouse.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
from . import find_device
1414

15+
try:
16+
from typing import Sequence
17+
import usb_hid
18+
except ImportError:
19+
pass
20+
1521

1622
class Mouse:
1723
"""Send USB HID mouse reports."""
@@ -23,10 +29,10 @@ class Mouse:
2329
MIDDLE_BUTTON = 4
2430
"""Middle mouse button."""
2531

26-
def __init__(self, devices):
32+
def __init__(self, devices: Sequence[usb_hid.device]):
2733
"""Create a Mouse object that will send USB mouse HID reports.
2834
29-
Devices can be a list of devices that includes a keyboard device or a keyboard device
35+
Devices can be a sequence of devices that includes a keyboard device or a keyboard device
3036
itself. A device is any object that implements ``send_report()``, ``usage_page`` and
3137
``usage``.
3238
"""
@@ -47,7 +53,7 @@ def __init__(self, devices):
4753
time.sleep(1)
4854
self._send_no_move()
4955

50-
def press(self, buttons):
56+
def press(self, buttons: int) -> None:
5157
"""Press the given mouse buttons.
5258
5359
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``,
@@ -64,7 +70,7 @@ def press(self, buttons):
6470
self.report[0] |= buttons
6571
self._send_no_move()
6672

67-
def release(self, buttons):
73+
def release(self, buttons: int) -> None:
6874
"""Release the given mouse buttons.
6975
7076
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``,
@@ -73,12 +79,12 @@ def release(self, buttons):
7379
self.report[0] &= ~buttons
7480
self._send_no_move()
7581

76-
def release_all(self):
82+
def release_all(self) -> None:
7783
"""Release all the mouse buttons."""
7884
self.report[0] = 0
7985
self._send_no_move()
8086

81-
def click(self, buttons):
87+
def click(self, buttons: int) -> None:
8288
"""Press and release the given mouse buttons.
8389
8490
:param buttons: a bitwise-or'd combination of ``LEFT_BUTTON``,
@@ -96,7 +102,7 @@ def click(self, buttons):
96102
self.press(buttons)
97103
self.release(buttons)
98104

99-
def move(self, x=0, y=0, wheel=0):
105+
def move(self, x: int = 0, y: int = 0, wheel: int = 0) -> None:
100106
"""Move the mouse and turn the wheel as directed.
101107
102108
:param x: Move the mouse along the x axis. Negative is to the left, positive
@@ -134,13 +140,13 @@ def move(self, x=0, y=0, wheel=0):
134140
y -= partial_y
135141
wheel -= partial_wheel
136142

137-
def _send_no_move(self):
143+
def _send_no_move(self) -> None:
138144
"""Send a button-only report."""
139145
self.report[1] = 0
140146
self.report[2] = 0
141147
self.report[3] = 0
142148
self._mouse_device.send_report(self.report)
143149

144150
@staticmethod
145-
def _limit(dist):
151+
def _limit(dist: int) -> int:
146152
return min(127, max(-127, dist))

0 commit comments

Comments
 (0)