Skip to content

Commit c368494

Browse files
authored
Merge pull request #50 from FoamyGuy/type_annotations
type annotations
2 parents 9d18dcd + cf32fd3 commit c368494

File tree

3 files changed

+86
-67
lines changed

3 files changed

+86
-67
lines changed

adafruit_button/button.py

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
from adafruit_display_shapes.roundrect import RoundRect
2727
from adafruit_button.button_base import ButtonBase, _check_color
2828

29+
try:
30+
from typing import Optional, Union
31+
from fontio import FontProtocol
32+
from displayio import Group
33+
except ImportError:
34+
pass
35+
2936
__version__ = "0.0.0+auto.0"
3037
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Button.git"
3138

@@ -117,22 +124,22 @@ def _create_body(self):
117124
def __init__(
118125
self,
119126
*,
120-
x,
121-
y,
122-
width,
123-
height,
124-
name=None,
125-
style=RECT,
126-
fill_color=0xFFFFFF,
127-
outline_color=0x0,
128-
label=None,
129-
label_font=None,
130-
label_color=0x0,
131-
selected_fill=None,
132-
selected_outline=None,
133-
selected_label=None,
134-
label_scale=None
135-
):
127+
x: int,
128+
y: int,
129+
width: int,
130+
height: int,
131+
name: Optional[str] = None,
132+
style: int = RECT,
133+
fill_color: Optional[Union[int, tuple[int, int, int]]] = 0xFFFFFF,
134+
outline_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
135+
label: Optional[str] = None,
136+
label_font: Optional[FontProtocol] = None,
137+
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
138+
selected_fill: Optional[Union[int, tuple[int, int, int]]] = None,
139+
selected_outline: Optional[Union[int, tuple[int, int, int]]] = None,
140+
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
141+
label_scale: Optional[int] = None
142+
) -> None:
136143
super().__init__(
137144
x=x,
138145
y=y,
@@ -167,7 +174,7 @@ def __init__(
167174

168175
self.label = label
169176

170-
def _subclass_selected_behavior(self, value):
177+
def _subclass_selected_behavior(self, value: bool) -> None:
171178
if self._selected:
172179
new_fill = self.selected_fill
173180
new_out = self.selected_outline
@@ -180,7 +187,7 @@ def _subclass_selected_behavior(self, value):
180187
self.body.outline = new_out
181188

182189
@property
183-
def group(self):
190+
def group(self) -> Group:
184191
"""Return self for compatibility with old API."""
185192
print(
186193
"Warning: The group property is being deprecated. "
@@ -189,7 +196,7 @@ def group(self):
189196
)
190197
return self
191198

192-
def contains(self, point):
199+
def contains(self, point: tuple[int, int]) -> bool:
193200
"""Used to determine if a point is contained within a button. For example,
194201
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
195202
determining that a button has been touched.
@@ -199,56 +206,56 @@ def contains(self, point):
199206
)
200207

201208
@property
202-
def fill_color(self):
209+
def fill_color(self) -> int:
203210
"""The fill color of the button body"""
204211
return self._fill_color
205212

206213
@fill_color.setter
207-
def fill_color(self, new_color):
214+
def fill_color(self, new_color: int) -> None:
208215
self._fill_color = _check_color(new_color)
209216
if not self.selected:
210217
self.body.fill = self._fill_color
211218

212219
@property
213-
def outline_color(self):
220+
def outline_color(self) -> int:
214221
"""The outline color of the button body"""
215222
return self._outline_color
216223

217224
@outline_color.setter
218-
def outline_color(self, new_color):
225+
def outline_color(self, new_color: int) -> None:
219226
self._outline_color = _check_color(new_color)
220227
if not self.selected:
221228
self.body.outline = self._outline_color
222229

223230
@property
224-
def selected_fill(self):
231+
def selected_fill(self) -> int:
225232
"""The fill color of the button body when selected"""
226233
return self._selected_fill
227234

228235
@selected_fill.setter
229-
def selected_fill(self, new_color):
236+
def selected_fill(self, new_color: int) -> None:
230237
self._selected_fill = _check_color(new_color)
231238
if self.selected:
232239
self.body.fill = self._selected_fill
233240

234241
@property
235-
def selected_outline(self):
242+
def selected_outline(self) -> int:
236243
"""The outline color of the button body when selected"""
237244
return self._selected_outline
238245

239246
@selected_outline.setter
240-
def selected_outline(self, new_color):
247+
def selected_outline(self, new_color: int) -> None:
241248
self._selected_outline = _check_color(new_color)
242249
if self.selected:
243250
self.body.outline = self._selected_outline
244251

245252
@property
246-
def width(self):
253+
def width(self) -> int:
247254
"""The width of the button"""
248255
return self._width
249256

250257
@width.setter
251-
def width(self, new_width):
258+
def width(self, new_width: int) -> None:
252259
self._width = new_width
253260
self._empty_self_group()
254261
self._create_body()
@@ -257,20 +264,20 @@ def width(self, new_width):
257264
self.label = self.label
258265

259266
@property
260-
def height(self):
267+
def height(self) -> int:
261268
"""The height of the button"""
262269
return self._height
263270

264271
@height.setter
265-
def height(self, new_height):
272+
def height(self, new_height: int) -> None:
266273
self._height = new_height
267274
self._empty_self_group()
268275
self._create_body()
269276
if self.body:
270277
self.append(self.body)
271278
self.label = self.label
272279

273-
def resize(self, new_width, new_height):
280+
def resize(self, new_width: int, new_height: int) -> None:
274281
"""Resize the button to the new width and height given
275282
:param new_width int the desired width
276283
:param new_height int the desired height

adafruit_button/button_base.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
from adafruit_display_text.bitmap_label import Label
2424
from displayio import Group
2525

26+
try:
27+
from typing import Optional, Union
28+
from fontio import FontProtocol
29+
except ImportError:
30+
pass
31+
2632

2733
def _check_color(color):
2834
# if a tuple is supplied, convert it to a RGB number
@@ -50,16 +56,16 @@ class ButtonBase(Group):
5056
def __init__(
5157
self,
5258
*,
53-
x,
54-
y,
55-
width,
56-
height,
57-
name=None,
58-
label=None,
59-
label_font=None,
60-
label_color=0x0,
61-
selected_label=None,
62-
label_scale=None
59+
x: int,
60+
y: int,
61+
width: int,
62+
height: int,
63+
name: Optional[str] = None,
64+
label: Optional[str] = None,
65+
label_font: Optional[FontProtocol] = None,
66+
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
67+
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
68+
label_scale: Optional[int] = None
6369
):
6470
super().__init__(x=x, y=y)
6571
self.x = x
@@ -76,12 +82,12 @@ def __init__(
7682
self._label_scale = label_scale or 1
7783

7884
@property
79-
def label(self):
85+
def label(self) -> str:
8086
"""The text label of the button"""
8187
return getattr(self._label, "text", None)
8288

8389
@label.setter
84-
def label(self, newtext):
90+
def label(self, newtext: str) -> None:
8591
if self._label and self and (self[-1] == self._label):
8692
self.pop()
8793

@@ -120,12 +126,12 @@ def _subclass_selected_behavior(self, value):
120126
pass
121127

122128
@property
123-
def selected(self):
129+
def selected(self) -> bool:
124130
"""Selected inverts the colors."""
125131
return self._selected
126132

127133
@selected.setter
128-
def selected(self, value):
134+
def selected(self, value: bool) -> None:
129135
if value == self._selected:
130136
return # bail now, nothing more to do
131137
self._selected = value
@@ -140,20 +146,20 @@ def selected(self, value):
140146
self._subclass_selected_behavior(value)
141147

142148
@property
143-
def selected_label(self):
149+
def selected_label(self) -> int:
144150
"""The font color of the button when selected"""
145151
return self._selected_label
146152

147153
@selected_label.setter
148-
def selected_label(self, new_color):
154+
def selected_label(self, new_color: int) -> None:
149155
self._selected_label = _check_color(new_color)
150156

151157
@property
152-
def label_color(self):
158+
def label_color(self) -> int:
153159
"""The font color of the button"""
154160
return self._label_color
155161

156162
@label_color.setter
157-
def label_color(self, new_color):
163+
def label_color(self, new_color: int):
158164
self._label_color = _check_color(new_color)
159165
self._label.color = self._label_color

adafruit_button/sprite_button.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
from adafruit_imageload import load
2525
from adafruit_button.button_base import ButtonBase
2626

27+
try:
28+
from typing import Optional, Union, Tuple
29+
from fontio import FontProtocol
30+
except ImportError:
31+
pass
32+
2733

2834
class SpriteButton(ButtonBase):
2935
"""Helper class for creating 3x3 Bitmap Spritesheet UI buttons for ``displayio``.
@@ -45,19 +51,19 @@ class SpriteButton(ButtonBase):
4551
def __init__(
4652
self,
4753
*,
48-
x,
49-
y,
50-
width,
51-
height,
52-
name=None,
53-
label=None,
54-
label_font=None,
55-
label_color=0x0,
56-
selected_label=None,
57-
bmp_path=None,
58-
selected_bmp_path=None,
59-
transparent_index=None,
60-
label_scale=None
54+
x: int,
55+
y: int,
56+
width: int,
57+
height: int,
58+
name: Optional[str] = None,
59+
label: Optional[str] = None,
60+
label_font: Optional[FontProtocol] = None,
61+
label_color: Optional[Union[int, tuple[int, int, int]]] = 0x0,
62+
selected_label: Optional[Union[int, tuple[int, int, int]]] = None,
63+
bmp_path: Optional[str] = None,
64+
selected_bmp_path: Optional[str] = None,
65+
transparent_index: Optional[int] = None,
66+
label_scale: Optional[int] = None
6167
):
6268
if bmp_path is None:
6369
raise ValueError("Please supply bmp_path. It cannot be None.")
@@ -104,16 +110,16 @@ def __init__(
104110
self.label = label
105111

106112
@property
107-
def width(self):
113+
def width(self) -> int:
108114
"""The width of the button"""
109115
return self._width
110116

111117
@property
112-
def height(self):
118+
def height(self) -> int:
113119
"""The height of the button"""
114120
return self._height
115121

116-
def contains(self, point):
122+
def contains(self, point: Tuple[int, int]):
117123
"""Used to determine if a point is contained within a button. For example,
118124
``button.contains(touch)`` where ``touch`` is the touch point on the screen will allow for
119125
determining that a button has been touched.
@@ -122,7 +128,7 @@ def contains(self, point):
122128
self.y <= point[1] <= self.y + self.height
123129
)
124130

125-
def _subclass_selected_behavior(self, value):
131+
def _subclass_selected_behavior(self, value: bool) -> None:
126132
if self._selected:
127133
if self._selected_bmp is not None:
128134
self._btn_tilegrid.bitmap = self._selected_bmp

0 commit comments

Comments
 (0)