Skip to content

Commit 33bffb8

Browse files
committed
Type Annotations
1 parent 8816314 commit 33bffb8

File tree

2 files changed

+58
-52
lines changed

2 files changed

+58
-52
lines changed

adafruit_display_text/__init__.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
Display Text module helper functions
77
"""
88
from displayio import Group, Palette
9-
9+
try:
10+
from typing import Tuple
11+
except:
12+
pass
1013

1114
def wrap_text_to_pixels(string, max_width, font=None, indent0="", indent1=""):
1215
"""wrap_text_to_pixels function
@@ -183,27 +186,26 @@ class LabelBase(Group):
183186
def __init__(
184187
self,
185188
font,
186-
x=0,
187-
y=0,
188-
text="",
189-
max_glyphs=None,
190-
# with label.py
191-
color=0xFFFFFF,
192-
background_color=None,
193-
line_spacing=1.25,
194-
background_tight=False,
195-
padding_top=0,
196-
padding_bottom=0,
197-
padding_left=0,
198-
padding_right=0,
199-
anchor_point=None,
200-
anchored_position=None,
201-
save_text=True, # can reduce memory use if save_text = False
202-
scale=1,
203-
base_alignment=False,
204-
tab_replacement=(4, " "),
189+
x: int = 0,
190+
y: int = 0,
191+
text: str = "",
192+
max_glyphs: int = None,
193+
color: int = 0xFFFFFF,
194+
background_color: int = None,
195+
line_spacing: float = 1.25,
196+
background_tight: bool = False,
197+
padding_top: int = 0,
198+
padding_bottom: int = 0,
199+
padding_left: int = 0,
200+
padding_right: int = 0,
201+
anchor_point: Tuple[float, float] = None,
202+
anchored_position: Tuple[int, int] = None,
203+
save_text: bool = True, # can reduce memory use if save_text = False
204+
scale:int = 1,
205+
base_alignment: bool = False,
206+
tab_replacement: Tuple[int, str] = (4, " "),
205207
**kwargs,
206-
):
208+
) -> None:
207209
super().__init__(max_size=1, x=x, y=y, scale=1)
208210

209211
self._font = font
@@ -230,7 +232,7 @@ def __init__(
230232
else:
231233
self._y_offset = self._get_ascent() // 2
232234

233-
def _get_ascent_descent(self):
235+
def _get_ascent_descent(self) -> Tuple[int, int]:
234236
""" Private function to calculate ascent and descent font values """
235237
if hasattr(self.font, "ascent"):
236238
return self.font.ascent, self.font.descent
@@ -251,29 +253,29 @@ def _get_ascent_descent(self):
251253
descender_max = max(descender_max, -this_glyph.dy)
252254
return ascender_max, descender_max
253255

254-
def _get_ascent(self):
256+
def _get_ascent(self) -> int:
255257
return self._get_ascent_descent()[0]
256258

257259
@property
258-
def font(self):
260+
def font(self) -> None:
259261
"""Font to use for text display."""
260262
return self._font
261263

262-
def _set_font(self, new_font):
264+
def _set_font(self, new_font) -> None:
263265
# subclasses should override this
264266
pass
265267

266268
@font.setter
267-
def font(self, new_font):
269+
def font(self, new_font) -> None:
268270
self._set_font(new_font)
269271

270272
@property
271-
def color(self):
273+
def color(self) -> int:
272274
"""Color of the text as an RGB hex number."""
273275
return self._color
274276

275277
@color.setter
276-
def color(self, new_color):
278+
def color(self, new_color: int):
277279
self._color = new_color
278280
if new_color is not None:
279281
self.palette[1] = new_color
@@ -283,12 +285,12 @@ def color(self, new_color):
283285
self.palette.make_transparent(1)
284286

285287
@property
286-
def background_color(self):
288+
def background_color(self) -> int:
287289
"""Color of the background as an RGB hex number."""
288290
return self._background_color
289291

290292
@background_color.setter
291-
def background_color(self, new_color):
293+
def background_color(self, new_color: int) -> None:
292294
self._background_color = new_color
293295
if new_color is not None:
294296
self.palette[0] = new_color
@@ -298,14 +300,14 @@ def background_color(self, new_color):
298300
self.palette.make_transparent(0)
299301

300302
@property
301-
def anchor_point(self):
303+
def anchor_point(self) -> Tuple[float, float]:
302304
"""Point that anchored_position moves relative to.
303305
Tuple with decimal percentage of width and height.
304306
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)"""
305307
return self._anchor_point
306308

307309
@anchor_point.setter
308-
def anchor_point(self, new_anchor_point):
310+
def anchor_point(self, new_anchor_point: Tuple[float, float]) -> None:
309311
if new_anchor_point[1] == self.baseline:
310312
self._anchor_point = (new_anchor_point[0], -1.0)
311313
else:
@@ -315,13 +317,13 @@ def anchor_point(self, new_anchor_point):
315317
) # update the anchored_position using setter
316318

317319
@property
318-
def anchored_position(self):
320+
def anchored_position(self) -> Tuple[int, int]:
319321
"""Position relative to the anchor_point. Tuple containing x,y
320322
pixel coordinates."""
321323
return self._anchored_position
322324

323325
@anchored_position.setter
324-
def anchored_position(self, new_position):
326+
def anchored_position(self, new_position: Tuple[int, int]) -> None:
325327
self._anchored_position = new_position
326328
# Set anchored_position
327329
if (self._anchor_point is not None) and (self._anchored_position is not None):
@@ -340,44 +342,44 @@ def anchored_position(self, new_position):
340342
)
341343

342344
@property
343-
def scale(self):
345+
def scale(self) -> int:
344346
"""Set the scaling of the label, in integer values"""
345347
return self.local_group.scale
346348

347349
@scale.setter
348-
def scale(self, new_scale):
350+
def scale(self, new_scale: int) -> None:
349351
self.local_group.scale = new_scale
350352
self.anchored_position = self._anchored_position # update the anchored_position
351353

352-
def _set_text(self, new_text, scale):
354+
def _set_text(self, new_text: str, scale: int) -> None:
353355
# subclasses should override this
354356
pass
355357

356358
@property
357-
def text(self):
359+
def text(self) -> str:
358360
"""Text to be displayed."""
359361
return self._text
360362

361363
@text.setter # Cannot set color or background color with text setter, use separate setter
362-
def text(self, new_text):
364+
def text(self, new_text: str) -> None:
363365
self._set_text(new_text, self.scale)
364366

365367
@property
366-
def bounding_box(self):
368+
def bounding_box(self) -> Tuple[int, int]:
367369
"""An (x, y, w, h) tuple that completely covers all glyphs. The
368370
first two numbers are offset from the x, y origin of this group"""
369371
return tuple(self._bounding_box)
370372

371373
@property
372-
def line_spacing(self):
374+
def line_spacing(self) -> float:
373375
"""The amount of space between lines of text, in multiples of the font's
374376
bounding-box height. (E.g. 1.0 is the bounding-box height)"""
375377
return self._line_spacing
376378

377-
def _set_line_spacing(self, new_line_spacing):
379+
def _set_line_spacing(self, new_line_spacing: float) -> None:
378380
# subclass should override this.
379381
pass
380382

381383
@line_spacing.setter
382-
def line_spacing(self, new_line_spacing):
384+
def line_spacing(self, new_line_spacing: float) -> None:
383385
self._set_line_spacing(new_line_spacing)

adafruit_display_text/label.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"""
2424

2525
import displayio
26+
try:
27+
from typing import Tuple
28+
except:
29+
pass
2630

2731
__version__ = "0.0.0-auto.0"
2832
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Text.git"
@@ -71,7 +75,7 @@ class Label(LabelBase):
7175
# pylint: disable=too-many-instance-attributes, too-many-locals
7276
# This has a lot of getters/setters, maybe it needs cleanup.
7377

74-
def __init__(self, font, **kwargs):
78+
def __init__(self, font, **kwargs) -> None:
7579
super().__init__(font, **kwargs)
7680

7781
max_glyphs = kwargs.get("max_glyphs", None)
@@ -130,7 +134,7 @@ def __init__(self, font, **kwargs):
130134
):
131135
self.anchored_position = kwargs.get("anchored_position", None)
132136

133-
def _create_background_box(self, lines, y_offset):
137+
def _create_background_box(self, lines: int, y_offset: int) -> None:
134138
"""Private Class function to create a background_box
135139
:param lines: int number of lines
136140
:param y_offset: int y pixel bottom coordinate for the background_box"""
@@ -172,7 +176,7 @@ def _create_background_box(self, lines, y_offset):
172176

173177
return tile_grid
174178

175-
def _update_background_color(self, new_color):
179+
def _update_background_color(self, new_color: int) -> None:
176180
"""Private class function that allows updating the font box background color
177181
:param new_color: int color as an RGB hex number."""
178182

@@ -228,8 +232,8 @@ def _update_background_color(self, new_color):
228232
self._added_background_tilegrid = False
229233

230234
def _update_text(
231-
self, new_text
232-
): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
235+
self, new_text: str
236+
) -> None: # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
233237
x = 0
234238
y = 0
235239
if self._added_background_tilegrid:
@@ -305,7 +309,7 @@ def _update_text(
305309
if self.background_color is not None:
306310
self._update_background_color(self._background_color)
307311

308-
def _reset_text(self, new_text):
312+
def _reset_text(self, new_text: str) -> None:
309313
new_text = self._tab_text.join(new_text.split("\t"))
310314
try:
311315
current_anchored_position = self.anchored_position
@@ -314,7 +318,7 @@ def _reset_text(self, new_text):
314318
except RuntimeError as run_error:
315319
raise RuntimeError("Text length exceeds max_glyphs") from run_error
316320

317-
def _set_font(self, new_font):
321+
def _set_font(self, new_font) -> None:
318322
old_text = self._text
319323
current_anchored_position = self.anchored_position
320324
self._text = ""
@@ -323,9 +327,9 @@ def _set_font(self, new_font):
323327
self._update_text(str(old_text))
324328
self.anchored_position = current_anchored_position
325329

326-
def _set_line_spacing(self, new_line_spacing):
330+
def _set_line_spacing(self, new_line_spacing: float) -> None:
327331
self._line_spacing = new_line_spacing
328332
self.text = self._text # redraw the box
329333

330-
def _set_text(self, new_text, scale):
334+
def _set_text(self, new_text: str, scale: int) -> None:
331335
self._reset_text(new_text)

0 commit comments

Comments
 (0)