Skip to content

Commit 66daec9

Browse files
committed
Type Anotations inclusion and test sample for pylint check
1 parent ce052ec commit 66daec9

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

adafruit_display_text/label.py

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
https://github.com/adafruit/circuitpython/releases
2222
2323
"""
24-
24+
from typing import Tuple
2525
import displayio
2626

2727
__version__ = "0.0.0-auto.0"
@@ -70,24 +70,24 @@ def __init__(
7070
self,
7171
font,
7272
*,
73-
x=0,
74-
y=0,
75-
text="",
76-
max_glyphs=None,
77-
color=0xFFFFFF,
78-
background_color=None,
79-
line_spacing=1.25,
80-
background_tight=False,
81-
padding_top=0,
82-
padding_bottom=0,
83-
padding_left=0,
84-
padding_right=0,
85-
anchor_point=None,
86-
anchored_position=None,
87-
scale=1,
88-
base_alignment=False,
73+
x: int = 0,
74+
y: int = 0,
75+
text: str = "",
76+
max_glyphs: int = None,
77+
color: int = 0xFFFFFF,
78+
background_color: int = None,
79+
line_spacing: float = 1.25,
80+
background_tight: bool = False,
81+
padding_top: int = 0,
82+
padding_bottom: int = 0,
83+
padding_left: int = 0,
84+
padding_right: int = 0,
85+
anchor_point: Tuple[float, float] = None,
86+
anchored_position: Tuple[int, int] = None,
87+
scale: int = 1,
88+
base_alignment: bool = False,
8989
**kwargs
90-
):
90+
) -> None:
9191
if not max_glyphs and not text:
9292
raise RuntimeError("Please provide a max size, or initial text")
9393
text = " ".join(text.split("\t"))
@@ -140,7 +140,7 @@ def __init__(
140140
if (anchored_position is not None) and (anchor_point is not None):
141141
self.anchored_position = anchored_position
142142

143-
def _create_background_box(self, lines, y_offset):
143+
def _create_background_box(self, lines: int, y_offset: int):
144144
"""Private Class function to create a background_box
145145
:param lines: int number of lines
146146
:param y_offset: int y pixel bottom coordinate for the background_box"""
@@ -182,7 +182,7 @@ def _create_background_box(self, lines, y_offset):
182182

183183
return tile_grid
184184

185-
def _get_ascent_descent(self):
185+
def _get_ascent_descent(self) -> Tuple[int, int]:
186186
""" Private function to calculate ascent and descent font values """
187187
if hasattr(self.font, "ascent"):
188188
return self.font.ascent, self.font.descent
@@ -203,10 +203,10 @@ def _get_ascent_descent(self):
203203
descender_max = max(descender_max, -this_glyph.dy)
204204
return ascender_max, descender_max
205205

206-
def _get_ascent(self):
206+
def _get_ascent(self) -> int:
207207
return self._get_ascent_descent()[0]
208208

209-
def _update_background_color(self, new_color):
209+
def _update_background_color(self, new_color: int) -> None:
210210
"""Private class function that allows updating the font box background color
211211
:param new_color: int color as an RGB hex number."""
212212

@@ -261,9 +261,8 @@ def _update_background_color(self, new_color):
261261
self.local_group.pop(0)
262262
self._added_background_tilegrid = False
263263

264-
def _update_text(
265-
self, new_text
266-
): # pylint: disable=too-many-locals ,too-many-branches, too-many-statements
264+
def _update_text(self, new_text: str) -> None:
265+
# pylint: disable=too-many-locals ,too-many-branches, too-many-statements
267266
x = 0
268267
y = 0
269268
if self._added_background_tilegrid:
@@ -340,19 +339,19 @@ def _update_text(
340339
self._update_background_color(self._background_color)
341340

342341
@property
343-
def bounding_box(self):
342+
def bounding_box(self) -> Tuple[int, int, int, int]:
344343
"""An (x, y, w, h) tuple that completely covers all glyphs. The
345344
first two numbers are offset from the x, y origin of this group"""
346345
return tuple(self._boundingbox)
347346

348347
@property
349-
def line_spacing(self):
348+
def line_spacing(self) -> float:
350349
"""The amount of space between lines of text, in multiples of the font's
351350
bounding-box height. (E.g. 1.0 is the bounding-box height)"""
352351
return self._line_spacing
353352

354353
@line_spacing.setter
355-
def line_spacing(self, spacing):
354+
def line_spacing(self, spacing: float) -> None:
356355
self._line_spacing = spacing
357356
self.text = self._text # redraw the box
358357

@@ -362,7 +361,7 @@ def color(self):
362361
return self.palette[1]
363362

364363
@color.setter
365-
def color(self, new_color):
364+
def color(self, new_color: int) -> None:
366365
self._color = new_color
367366
if new_color is not None:
368367
self.palette[1] = new_color
@@ -372,21 +371,21 @@ def color(self, new_color):
372371
self.palette.make_transparent(1)
373372

374373
@property
375-
def background_color(self):
374+
def background_color(self) -> int:
376375
"""Color of the background as an RGB hex number."""
377376
return self._background_color
378377

379378
@background_color.setter
380-
def background_color(self, new_color):
379+
def background_color(self, new_color: int) -> None:
381380
self._update_background_color(new_color)
382381

383382
@property
384-
def text(self):
383+
def text(self) -> str:
385384
"""Text to display."""
386385
return self._text
387386

388387
@text.setter
389-
def text(self, new_text):
388+
def text(self, new_text: str) -> None:
390389
new_text = " ".join(new_text.split("\t"))
391390
try:
392391
current_anchored_position = self.anchored_position
@@ -396,12 +395,12 @@ def text(self, new_text):
396395
raise RuntimeError("Text length exceeds max_glyphs") from run_error
397396

398397
@property
399-
def scale(self):
398+
def scale(self) -> int:
400399
"""Set the scaling of the label, in integer values"""
401400
return self.local_group.scale
402401

403402
@scale.setter
404-
def scale(self, new_scale):
403+
def scale(self, new_scale: int) -> None:
405404
current_anchored_position = self.anchored_position
406405
self.local_group.scale = new_scale
407406
self.anchored_position = current_anchored_position
@@ -412,7 +411,7 @@ def font(self):
412411
return self._font
413412

414413
@font.setter
415-
def font(self, new_font):
414+
def font(self, new_font) -> None:
416415
old_text = self._text
417416
current_anchored_position = self.anchored_position
418417
self._text = ""
@@ -422,14 +421,14 @@ def font(self, new_font):
422421
self.anchored_position = current_anchored_position
423422

424423
@property
425-
def anchor_point(self):
424+
def anchor_point(self) -> Tuple[float, float]:
426425
"""Point that anchored_position moves relative to.
427426
Tuple with decimal percentage of width and height.
428427
(E.g. (0,0) is top left, (1.0, 0.5): is middle right.)"""
429428
return self._anchor_point
430429

431430
@anchor_point.setter
432-
def anchor_point(self, new_anchor_point):
431+
def anchor_point(self, new_anchor_point: Tuple[float, float]) -> None:
433432
if self._anchor_point is not None:
434433
current_anchored_position = self.anchored_position
435434
self._anchor_point = new_anchor_point
@@ -438,7 +437,7 @@ def anchor_point(self, new_anchor_point):
438437
self._anchor_point = new_anchor_point
439438

440439
@property
441-
def anchored_position(self):
440+
def anchored_position(self) -> Tuple[int, int]:
442441
"""Position relative to the anchor_point. Tuple containing x,y
443442
pixel coordinates."""
444443
if self._anchor_point is None:
@@ -457,7 +456,7 @@ def anchored_position(self):
457456
)
458457

459458
@anchored_position.setter
460-
def anchored_position(self, new_position):
459+
def anchored_position(self, new_position: Tuple[int, int]) -> Tuple[int, int]:
461460
if (self._anchor_point is None) or (new_position is None):
462461
return # Note: anchor_point must be set before setting anchored_position
463462
self.x = int(

0 commit comments

Comments
 (0)