Skip to content

Commit 74b48de

Browse files
committed
Add type hints for cursorcontrol.py
1 parent 7329754 commit 74b48de

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

adafruit_cursorcontrol/cursorcontrol.py

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
"""
2323
import displayio
2424

25+
try:
26+
from typing import Optional, Type
27+
from types import TracebackType
28+
except ImportError:
29+
pass
30+
2531
__version__ = "0.0.0-auto.0"
2632
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CursorControl.git"
2733

@@ -54,12 +60,12 @@ class Cursor:
5460
# pylint: disable=too-many-arguments,line-too-long
5561
def __init__(
5662
self,
57-
display=None,
58-
display_group=None,
59-
bmp=None,
60-
is_hidden=False,
61-
cursor_speed=5,
62-
scale=1,
63+
display: Optional[displayio.Display] = None,
64+
display_group: Optional[displayio.Group] = None,
65+
bmp: Optional[displayio.Bitmap] = None,
66+
is_hidden: bool = False,
67+
cursor_speed: int = 5,
68+
scale: int = 1,
6369
):
6470
self._display = display
6571
self._scale = scale
@@ -76,19 +82,19 @@ def __init__(
7682

7783
# pylint: enable=too-many-arguments,line-too-long
7884

79-
def __enter__(self):
85+
def __enter__(self) -> 'Cursor':
8086
return self
8187

82-
def __exit__(self, exception_type, exception_value, traceback):
88+
def __exit__(self, exception_type: Optional[Type[type]], exception_value: Optional[BaseException], traceback: Optional[TracebackType]) -> None:
8389
self.deinit()
8490

85-
def deinit(self):
91+
def deinit(self) -> None:
8692
"""deinitializes the cursor object."""
8793
self._is_deinited()
8894
self._scale = None
8995
self._display_grp.remove(self._cursor_grp)
9096

91-
def _is_deinited(self):
97+
def _is_deinited(self) -> None:
9298
"""checks cursor deinitialization"""
9399
if self._scale is None:
94100
raise ValueError(
@@ -97,12 +103,12 @@ def _is_deinited(self):
97103
)
98104

99105
@property
100-
def scale(self):
106+
def scale(self) -> int:
101107
"""Returns the cursor's scale amount as an integer."""
102108
return self._scale
103109

104110
@scale.setter
105-
def scale(self, scale_value):
111+
def scale(self, scale_value: int) -> None:
106112
"""Scales the cursor by scale_value in both directions.
107113
:param int scale_value: Amount to scale the cursor by.
108114
"""
@@ -112,12 +118,12 @@ def scale(self, scale_value):
112118
self._cursor_grp.scale = scale_value
113119

114120
@property
115-
def speed(self):
121+
def speed(self) -> int:
116122
"""Returns the cursor's speed, in pixels."""
117123
return self._speed
118124

119125
@speed.setter
120-
def speed(self, speed):
126+
def speed(self, speed: int) -> None:
121127
"""Sets the speed of the cursor.
122128
:param int speed: Cursor movement speed, in pixels.
123129
"""
@@ -126,12 +132,12 @@ def speed(self, speed):
126132
self._speed = speed
127133

128134
@property
129-
def x(self):
135+
def x(self) -> int:
130136
"""Returns the cursor's x-coordinate."""
131137
return self._cursor_grp.x
132138

133139
@x.setter
134-
def x(self, x_val):
140+
def x(self, x_val: int) -> None:
135141
"""Sets the x-value of the cursor.
136142
:param int x_val: cursor x-position, in pixels.
137143
"""
@@ -144,12 +150,12 @@ def x(self, x_val):
144150
self._cursor_grp.x = x_val
145151

146152
@property
147-
def y(self):
153+
def y(self) -> int:
148154
"""Returns the cursor's y-coordinate."""
149155
return self._cursor_grp.y
150156

151157
@y.setter
152-
def y(self, y_val):
158+
def y(self, y_val: int) -> None:
153159
"""Sets the y-value of the cursor.
154160
:param int y_val: cursor y-position, in pixels.
155161
"""
@@ -162,12 +168,12 @@ def y(self, y_val):
162168
self._cursor_grp.y = y_val
163169

164170
@property
165-
def hidden(self):
171+
def hidden(self) -> bool:
166172
"""Returns True if the cursor is hidden or visible on the display."""
167173
return self._is_hidden
168174

169175
@hidden.setter
170-
def hidden(self, is_hidden):
176+
def hidden(self, is_hidden: bool) -> None:
171177
self._is_deinited()
172178
if is_hidden:
173179
self._is_hidden = True
@@ -176,16 +182,16 @@ def hidden(self, is_hidden):
176182
self._is_hidden = False
177183
self._display_grp.append(self._cursor_grp)
178184

179-
def hide(self):
185+
def hide(self) -> None:
180186
"""Hide the cursor."""
181187
self.hidden = True
182188

183-
def show(self):
189+
def show(self) -> None:
184190
"""Show the cursor."""
185191
self.hidden = False
186192

187193
# pylint:disable=no-self-use
188-
def _default_cursor_bitmap(self):
194+
def _default_cursor_bitmap(self) -> displayio.Bitmap:
189195
bmp = displayio.Bitmap(20, 20, 3)
190196
# left edge, outline
191197
for i in range(0, bmp.height):
@@ -210,12 +216,12 @@ def _default_cursor_bitmap(self):
210216
# pylint:enable=no-self-use
211217

212218
@property
213-
def cursor_bitmap(self):
219+
def cursor_bitmap(self) -> displayio.Bitmap:
214220
"""Return the cursor bitmap."""
215221
return self._cursor_bitmap
216222

217223
@cursor_bitmap.setter
218-
def cursor_bitmap(self, bmp):
224+
def cursor_bitmap(self, bmp: displayio.Bitmap) -> None:
219225
"""Set a new cursor bitmap.
220226
221227
:param ~displayio.Bitmap bmp: A Bitmap to use for the cursor
@@ -225,7 +231,7 @@ def cursor_bitmap(self, bmp):
225231
self._cur_sprite = displayio.TileGrid(bmp, pixel_shader=self._cur_palette)
226232
self._cursor_grp.append(self._cur_sprite)
227233

228-
def generate_cursor(self, bmp):
234+
def generate_cursor(self, bmp: displayio.Bitmap) -> None:
229235
"""Generates a cursor icon
230236
231237
:param ~displayio.Bitmap bmp: A Bitmap to use for the cursor

0 commit comments

Comments
 (0)