Skip to content

Commit d57abcb

Browse files
authored
Merge pull request #85 from cguardia/displayio_type_annotations
Missing type annotations, refs #50
2 parents 64b75e1 + 5f23ebd commit d57abcb

File tree

9 files changed

+331
-240
lines changed

9 files changed

+331
-240
lines changed

adafruit_displayio_layout/layouts/grid_layout.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"""
2525
try:
2626
# Used only for typing
27-
from typing import Tuple
27+
from typing import Any, List, Optional, Tuple, Union
2828
except ImportError:
2929
pass
3030

@@ -61,32 +61,32 @@ class GridLayout(displayio.Group):
6161
# pylint: disable=too-many-instance-attributes
6262
def __init__(
6363
self,
64-
x,
65-
y,
66-
width,
67-
height,
68-
grid_size,
69-
cell_padding=0,
70-
divider_lines=False,
71-
h_divider_line_rows=None,
72-
v_divider_line_cols=None,
73-
divider_line_color=0xFFFFFF,
74-
cell_anchor_point=(0.0, 0.0),
75-
):
64+
x: int,
65+
y: int,
66+
width: int,
67+
height: int,
68+
grid_size: tuple[int, int],
69+
cell_padding: int = 0,
70+
divider_lines: bool = False,
71+
h_divider_line_rows: Union[Tuple[int, ...], List[int], None] = None,
72+
v_divider_line_cols: Union[Tuple[int, ...], List[int], None] = None,
73+
divider_line_color: int = 0xFFFFFF,
74+
cell_anchor_point: Tuple[float, float] = (0.0, 0.0),
75+
) -> None:
7676
super().__init__(x=x, y=y)
7777
self.x = x
7878
self.y = y
7979
self._width = width
8080
self._height = height
8181
self.grid_size = grid_size
8282
self.cell_padding = cell_padding
83-
self._cell_content_list = []
83+
self._cell_content_list: List[dict[str, Any]] = []
8484
self._cell_anchor_point = cell_anchor_point
8585

86-
self._divider_lines = []
86+
self._divider_lines: List[dict[str, Any]] = []
8787
self._divider_color = divider_line_color
88-
self.h_divider_line_rows = h_divider_line_rows
89-
self.v_divider_line_cols = v_divider_line_cols
88+
self.h_divider_line_rows = h_divider_line_rows or tuple()
89+
self.v_divider_line_cols = v_divider_line_cols or tuple()
9090

9191
self._divider_lines_enabled = (
9292
(divider_lines is True)
@@ -95,27 +95,22 @@ def __init__(
9595
)
9696

9797
if divider_lines:
98-
if self.h_divider_line_rows is None:
98+
if h_divider_line_rows is None:
9999
self.h_divider_line_rows = []
100100
for _y in range(self.grid_size[1] + 1):
101101
self.h_divider_line_rows.append(_y)
102-
if self.v_divider_line_cols is None:
102+
if v_divider_line_cols is None:
103103
self.v_divider_line_cols = []
104104
for _x in range(self.grid_size[0] + 1):
105105
self.v_divider_line_cols.append(_x)
106-
else:
107-
if not h_divider_line_rows:
108-
self.h_divider_line_rows = tuple()
109-
if not v_divider_line_cols:
110-
self.v_divider_line_cols = tuple()
111106

112107
# use at least 1 padding so that content is inside the divider lines
113108
if cell_padding == 0 and (
114109
divider_lines or h_divider_line_rows or v_divider_line_cols
115110
):
116111
self.cell_padding = 1
117112

118-
def _layout_cells(self):
113+
def _layout_cells(self) -> None:
119114
# pylint: disable=too-many-locals, too-many-branches, too-many-statements
120115
for cell in self._cell_content_list:
121116
if cell["content"] not in self:
@@ -382,8 +377,12 @@ def _layout_cells(self):
382377
self.append(line_obj["tilegrid"])
383378

384379
def add_content(
385-
self, cell_content, grid_position, cell_size, cell_anchor_point=None
386-
):
380+
self,
381+
cell_content: displayio.Group,
382+
grid_position: Tuple[int, int],
383+
cell_size: Tuple[int, int],
384+
cell_anchor_point: Optional[Tuple[float, ...]] = None,
385+
) -> None:
387386
"""Add a child to the grid.
388387
389388
:param cell_content: the content to add to this cell e.g. label, button, etc...
@@ -412,7 +411,7 @@ def add_content(
412411
self._cell_content_list.append(sub_view_obj)
413412
self._layout_cells()
414413

415-
def get_cell(self, cell_coordinates):
414+
def get_cell(self, cell_coordinates: Tuple[int, int]) -> displayio.Group:
416415
"""
417416
Return a cells content based on the cell_coordinates. Raises
418417
KeyError if coordinates were not found in the GridLayout.

adafruit_displayio_layout/widgets/cartesian.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
pass
3737

3838
try:
39-
from typing import Tuple
39+
from typing import Any, List, Optional, Tuple
4040
except ImportError:
4141
pass
4242

@@ -178,15 +178,15 @@ def __init__(
178178
tick_color: int = 0xFFFFFF,
179179
major_tick_stroke: int = 1,
180180
major_tick_length: int = 5,
181-
tick_label_font=terminalio.FONT,
181+
tick_label_font: terminalio.FONT = terminalio.FONT,
182182
font_color: int = 0xFFFFFF,
183183
pointer_radius: int = 1,
184184
pointer_color: int = 0xFFFFFF,
185185
subticks: bool = False,
186186
nudge_x: int = 0,
187187
nudge_y: int = 0,
188188
verbose: bool = False,
189-
**kwargs,
189+
**kwargs: Any,
190190
) -> None:
191191

192192
super().__init__(**kwargs)
@@ -317,12 +317,12 @@ def __init__(
317317
self.append(self._screen_tilegrid)
318318
self.append(self._corner_tilegrid)
319319

320-
self._pointer = None
321-
self._circle_palette = None
322-
self.plot_line_point = None
320+
self._pointer: Optional[vectorio.Circle] = None
321+
self._circle_palette: Optional[displayio.Palette] = None
322+
self.plot_line_point: List[Tuple[int, int]] = []
323323

324324
@staticmethod
325-
def _get_font_height(font, scale: int) -> Tuple[int, int]:
325+
def _get_font_height(font: terminalio.FONT, scale: int) -> Tuple[int, int]:
326326
if hasattr(font, "get_bounding_box"):
327327
font_height = int(scale * font.get_bounding_box()[1])
328328
font_width = int(scale * font.get_bounding_box()[0])
@@ -448,14 +448,15 @@ def _draw_ticks(self) -> None:
448448
def _draw_pointers(self, x: int, y: int) -> None:
449449

450450
self._circle_palette = displayio.Palette(1)
451+
451452
self._circle_palette[0] = self._pointer_color
452453
self._pointer = vectorio.Circle(
453454
radius=self._pointer_radius, x=x, y=y, pixel_shader=self._circle_palette
454455
)
455456

456457
self.append(self._pointer)
457458

458-
def _calc_local_xy(self, x: int, y: int) -> (int, int):
459+
def _calc_local_xy(self, x: int, y: int) -> Tuple[int, int]:
459460
local_x = (
460461
int((x - self._xrange[0]) * self._factorx * self._valuex) + self._nudge_x
461462
)
@@ -469,24 +470,24 @@ def _calc_local_xy(self, x: int, y: int) -> (int, int):
469470
)
470471
return (local_x, local_y)
471472

472-
def _check_local_x_in_range(self, local_x):
473+
def _check_local_x_in_range(self, local_x: int) -> bool:
473474
return 0 <= local_x < self.width
474475

475-
def _check_local_y_in_range(self, local_y):
476+
def _check_local_y_in_range(self, local_y: int) -> bool:
476477
return 0 <= local_y < self.height
477478

478-
def _check_local_xy_in_range(self, local_x, local_y):
479+
def _check_local_xy_in_range(self, local_x: int, local_y: int) -> bool:
479480
return self._check_local_x_in_range(local_x) and self._check_local_y_in_range(
480481
local_y
481482
)
482483

483-
def _check_x_in_range(self, x):
484+
def _check_x_in_range(self, x: int) -> bool:
484485
return self._xrange[0] <= x <= self._xrange[1]
485486

486-
def _check_y_in_range(self, y):
487+
def _check_y_in_range(self, y: int) -> bool:
487488
return self._yrange[0] <= y <= self._yrange[1]
488489

489-
def _check_xy_in_range(self, x, y):
490+
def _check_xy_in_range(self, x: int, y: int) -> bool:
490491
return self._check_x_in_range(x) and self._check_y_in_range(y)
491492

492493
def _add_point(self, x: int, y: int) -> None:
@@ -587,6 +588,7 @@ def update_pointer(self, x: int, y: int) -> None:
587588
:return: None
588589
rtype: None
589590
"""
591+
590592
self._add_point(x, y)
591593
if not self._pointer:
592594
self._draw_pointers(
@@ -609,6 +611,7 @@ def add_plot_line(self, x: int, y: int) -> None:
609611
610612
rtype: None
611613
"""
614+
612615
self._add_point(x, y)
613616
if len(self.plot_line_point) > 1:
614617
bitmaptools.draw_line(
@@ -620,7 +623,7 @@ def add_plot_line(self, x: int, y: int) -> None:
620623
1,
621624
)
622625

623-
def clear_plot_lines(self, palette_index=5):
626+
def clear_plot_lines(self, palette_index: int = 5) -> None:
624627
"""clear_plot_lines function.
625628
626629
clear all added lines
@@ -631,5 +634,5 @@ def clear_plot_lines(self, palette_index=5):
631634
632635
rtype: None
633636
"""
634-
self.plot_line_point = None
637+
self.plot_line_point = []
635638
self._plot_bitmap.fill(palette_index)

adafruit_displayio_layout/widgets/control.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
2222
"""
2323

24+
try:
25+
from typing import Optional, Tuple
26+
except ImportError:
27+
pass
28+
29+
2430
__version__ = "0.0.0+auto.0"
2531
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DisplayIO_Layout.git"
2632

@@ -46,20 +52,24 @@ class Control:
4652

4753
def __init__(
4854
self,
49-
):
55+
) -> None:
5056
self.touch_boundary = (
51-
None # `self.touch_boundary` should be updated by the subclass
57+
0,
58+
0,
59+
0,
60+
0, # `self.touch_boundary` should be updated by the subclass
5261
)
5362
# Tuple of [x, y, width, height]: [int, int, int, int] all in pixel units
5463
# where x,y define the upper left corner
5564
# and width and height define the size of the `touch_boundary`
5665

57-
def contains(self, touch_point):
66+
def contains(self, touch_point: Tuple[int, int, Optional[int]]) -> bool:
5867
"""Checks if the Control was touched. Returns True if the touch_point is within the
5968
Control's touch_boundary.
6069
61-
:param touch_point: x,y location of the screen, converted to local coordinates.
62-
:type touch_point: Tuple[x,y]
70+
:param touch_point: x, y, p location of the screen, converted to local coordinates, plus
71+
an optional pressure value for screens that support it.
72+
:type touch_point: Tuple[int, int, Optional[int]]
6373
:return: Boolean
6474
6575
"""
@@ -82,11 +92,12 @@ def contains(self, touch_point):
8292
return False
8393

8494
# place holder touch_handler response functions
85-
def selected(self, touch_point):
95+
def selected(self, touch_point: Tuple[int, int, Optional[int]]) -> None:
8696
"""Response function when Control is selected. Should be overridden by subclass.
8797
88-
:param touch_point: x,y location of the screen, converted to local coordinates.
89-
:type touch_point: Tuple[x,y]
98+
:param touch_point: x, y, p location of the screen, converted to local coordinates, plus
99+
an optional pressure value for screens that support it.
100+
:type touch_point: Tuple[int, int, Optional[int]]
90101
:return: None
91102
92103
"""

0 commit comments

Comments
 (0)