Skip to content

Commit 3d64ac0

Browse files
committed
Added Type Annotations
1 parent d90f9e7 commit 3d64ac0

File tree

7 files changed

+163
-52
lines changed

7 files changed

+163
-52
lines changed

adafruit_display_shapes/circle.py

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

24+
try:
25+
from typing import Optional
26+
except ImportError:
27+
pass
28+
2429
from adafruit_display_shapes.roundrect import RoundRect
2530

2631
__version__ = "0.0.0-auto.0"
@@ -42,7 +47,16 @@ class Circle(RoundRect):
4247
4348
"""
4449

45-
def __init__(self, x0, y0, r, *, fill=None, outline=None, stroke=1):
50+
def __init__(
51+
self,
52+
x0: int,
53+
y0: int,
54+
r: int,
55+
*,
56+
fill: Optional[int] = None,
57+
outline: Optional[int] = None,
58+
stroke: int = 1,
59+
) -> None:
4660
super().__init__(
4761
x0 - r,
4862
y0 - r,
@@ -56,19 +70,19 @@ def __init__(self, x0, y0, r, *, fill=None, outline=None, stroke=1):
5670
self.r = r
5771

5872
@property
59-
def x0(self):
73+
def x0(self) -> int:
6074
"""The x-position of the center of the circle."""
6175
return self.x + self.r
6276

6377
@property
64-
def y0(self):
78+
def y0(self) -> int:
6579
"""The y-position of the center of the circle."""
6680
return self.y + self.r
6781

6882
@x0.setter
69-
def x0(self, x0):
83+
def x0(self, x0: int) -> None:
7084
self.x = x0 - self.r
7185

7286
@y0.setter
73-
def y0(self, y0):
87+
def y0(self, y0: int) -> None:
7488
self.y = y0 - self.r

adafruit_display_shapes/line.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,22 @@ class Line(Polygon):
3838
:param color: The color of the line.
3939
"""
4040

41-
def __init__(self, x0, y0, x1, y1, color):
41+
def __init__(
42+
self,
43+
x0: int,
44+
y0: int,
45+
x1: int,
46+
y1: int,
47+
color: int,
48+
) -> None:
4249
super().__init__([(x0, y0), (x1, y1)], outline=color)
4350

4451
@property
45-
def color(self):
52+
def color(self) -> int:
4653
"""The line color value. Can be a hex value for a color or
4754
``None`` for no line color."""
4855
return self.outline
4956

5057
@color.setter
51-
def color(self, color):
58+
def color(self, color: int) -> None:
5259
self.outline = color

adafruit_display_shapes/polygon.py

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

24+
try:
25+
from typing import Optional, List, Tuple
26+
except ImportError:
27+
pass
28+
2429
import displayio
2530

2631
__version__ = "0.0.0-auto.0"
@@ -36,7 +41,12 @@ class Polygon(displayio.TileGrid):
3641
``None`` for no outline.
3742
"""
3843

39-
def __init__(self, points, *, outline=None):
44+
def __init__(
45+
self,
46+
points: List[Tuple[int, int]],
47+
*,
48+
outline: Optional[int] = None,
49+
) -> None:
4050
xs = []
4151
ys = []
4252

@@ -77,7 +87,14 @@ def __init__(self, points, *, outline=None):
7787
)
7888

7989
# pylint: disable=invalid-name, too-many-locals, too-many-branches
80-
def _line(self, x0, y0, x1, y1, color):
90+
def _line(
91+
self,
92+
x0: int,
93+
y0: int,
94+
x1: int,
95+
y1: int,
96+
color: int,
97+
) -> None:
8198
if x0 == x1:
8299
if y0 > y1:
83100
y0, y1 = y1, y0
@@ -121,13 +138,13 @@ def _line(self, x0, y0, x1, y1, color):
121138
# pylint: enable=invalid-name, too-many-locals, too-many-branches
122139

123140
@property
124-
def outline(self):
141+
def outline(self) -> int:
125142
"""The outline of the polygon. Can be a hex value for a color or
126143
``None`` for no outline."""
127144
return self._palette[1]
128145

129146
@outline.setter
130-
def outline(self, color):
147+
def outline(self, color: int) -> None:
131148
if color is None:
132149
self._palette[1] = 0
133150
self._palette.make_transparent(1)

adafruit_display_shapes/rect.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
2222
"""
2323

24+
try:
25+
from typing import Optional
26+
except ImportError:
27+
pass
28+
2429
import displayio
2530

2631
__version__ = "0.0.0-auto.0"
@@ -43,7 +48,17 @@ class Rect(displayio.TileGrid):
4348
4449
"""
4550

46-
def __init__(self, x, y, width, height, *, fill=None, outline=None, stroke=1):
51+
def __init__(
52+
self,
53+
x: int,
54+
y: int,
55+
width: int,
56+
height: int,
57+
*,
58+
fill: Optional[int] = None,
59+
outline: Optional[int] = None,
60+
stroke: int = 1,
61+
) -> None:
4762
self._bitmap = displayio.Bitmap(width, height, 2)
4863
self._palette = displayio.Palette(2)
4964

@@ -67,13 +82,13 @@ def __init__(self, x, y, width, height, *, fill=None, outline=None, stroke=1):
6782
super().__init__(self._bitmap, pixel_shader=self._palette, x=x, y=y)
6883

6984
@property
70-
def fill(self):
85+
def fill(self) -> int:
7186
"""The fill of the rectangle. Can be a hex value for a color or ``None`` for
7287
transparent."""
7388
return self._palette[0]
7489

7590
@fill.setter
76-
def fill(self, color):
91+
def fill(self, color: int) -> None:
7792
if color is None:
7893
self._palette[0] = 0
7994
self._palette.make_transparent(0)
@@ -82,13 +97,13 @@ def fill(self, color):
8297
self._palette.make_opaque(0)
8398

8499
@property
85-
def outline(self):
100+
def outline(self) -> int:
86101
"""The outline of the rectangle. Can be a hex value for a color or ``None``
87102
for no outline."""
88103
return self._palette[1]
89104

90105
@outline.setter
91-
def outline(self, color):
106+
def outline(self, color: int) -> None:
92107
if color is None:
93108
self._palette[1] = 0
94109
self._palette.make_transparent(1)

adafruit_display_shapes/roundrect.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
1212
"""
1313

14+
try:
15+
from typing import Optional
16+
except ImportError:
17+
pass
18+
1419
import displayio
1520

1621
__version__ = "0.0.0-auto.0"
@@ -35,7 +40,18 @@ class RoundRect(displayio.TileGrid):
3540
3641
"""
3742

38-
def __init__(self, x, y, width, height, r, *, fill=None, outline=None, stroke=1):
43+
def __init__(
44+
self,
45+
x: int,
46+
y: int,
47+
width: int,
48+
height: int,
49+
r: int,
50+
*,
51+
fill: Optional[int] = None,
52+
outline: Optional[int] = None,
53+
stroke: int = 1,
54+
) -> None:
3955
self._palette = displayio.Palette(3)
4056
self._palette.make_transparent(0)
4157
self._bitmap = displayio.Bitmap(width, height, 3)
@@ -85,17 +101,17 @@ def __init__(self, x, y, width, height, r, *, fill=None, outline=None, stroke=1)
85101
# pylint: disable=invalid-name, too-many-locals, too-many-branches
86102
def _helper(
87103
self,
88-
x0,
89-
y0,
90-
r,
104+
x0: int,
105+
y0: int,
106+
r: int,
91107
*,
92-
color,
93-
x_offset=0,
94-
y_offset=0,
95-
stroke=1,
96-
corner_flags=0xF,
97-
fill=False
98-
):
108+
color: int,
109+
x_offset: int = 0,
110+
y_offset: int = 0,
111+
stroke: int = 1,
112+
corner_flags: int = 0xF,
113+
fill: bool = False,
114+
) -> None:
99115
f = 1 - r
100116
ddF_x = 1
101117
ddF_y = -2 * r
@@ -142,13 +158,13 @@ def _helper(
142158
# pylint: enable=invalid-name, too-many-locals, too-many-branches
143159

144160
@property
145-
def fill(self):
161+
def fill(self) -> int:
146162
"""The fill of the rounded-corner rectangle. Can be a hex value for a color or ``None`` for
147163
transparent."""
148164
return self._palette[2]
149165

150166
@fill.setter
151-
def fill(self, color):
167+
def fill(self, color: int) -> None:
152168
if color is None:
153169
self._palette[2] = 0
154170
self._palette.make_transparent(2)
@@ -157,13 +173,13 @@ def fill(self, color):
157173
self._palette.make_opaque(2)
158174

159175
@property
160-
def outline(self):
176+
def outline(self) -> int:
161177
"""The outline of the rounded-corner rectangle. Can be a hex value for a color or ``None``
162178
for no outline."""
163179
return self._palette[1]
164180

165181
@outline.setter
166-
def outline(self, color):
182+
def outline(self, color: int) -> None:
167183
if color is None:
168184
self._palette[1] = 0
169185
self._palette.make_transparent(1)

adafruit_display_shapes/sparkline.py

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
# pylint: disable=too-many-instance-attributes
4141

42+
try:
43+
from typing import Optional, List
44+
except ImportError:
45+
pass
4246
import displayio
4347
from adafruit_display_shapes.line import Line
4448

@@ -59,15 +63,15 @@ class Sparkline(displayio.Group):
5963

6064
def __init__(
6165
self,
62-
width,
63-
height,
64-
max_items,
65-
y_min=None, # None = autoscaling
66-
y_max=None, # None = autoscaling
67-
x=0,
68-
y=0,
69-
color=0xFFFFFF, # line color, default is WHITE
70-
):
66+
width: int,
67+
height: int,
68+
max_items: int,
69+
y_min: Optional[int] = None, # None = autoscaling
70+
y_max: Optional[int] = None, # None = autoscaling
71+
x: int = 0,
72+
y: int = 0,
73+
color: int = 0xFFFFFF, # line color, default is WHITE
74+
) -> None:
7175

7276
# define class instance variables
7377
self.width = width # in pixels
@@ -88,14 +92,14 @@ def __init__(
8892

8993
super().__init__(x=x, y=y) # self is a group of lines
9094

91-
def clear_values(self):
95+
def clear_values(self) -> None:
9296
"""Removes all values from the _spark_list list and removes all lines in the group"""
9397

9498
for _ in range(len(self)): # remove all items from the current group
9599
self.pop()
96100
self._spark_list = [] # empty the list
97101

98-
def add_value(self, value):
102+
def add_value(self, value: float) -> None:
99103
"""Add a value to the sparkline.
100104
:param value: The value to be added to the sparkline
101105
"""
@@ -111,8 +115,14 @@ def add_value(self, value):
111115
# pylint: disable=no-else-return
112116
@staticmethod
113117
def _xintercept(
114-
x_1, y_1, x_2, y_2, horizontal_y
115-
): # finds intercept of the line and a horizontal line at horizontalY
118+
x_1: int,
119+
y_1: float,
120+
x_2: int,
121+
y_2: float,
122+
horizontal_y: int,
123+
) -> Optional[
124+
int
125+
]: # finds intercept of the line and a horizontal line at horizontalY
116126
slope = (y_2 - y_1) / (x_2 - x_1)
117127
b = y_1 - slope * x_1
118128

@@ -124,15 +134,23 @@ def _xintercept(
124134
) / slope # calculate the x-intercept at position y=horizontalY
125135
return int(xint)
126136

127-
def _plotline(self, x_1, last_value, x_2, value, y_bottom, y_top):
137+
def _plotline(
138+
self,
139+
x_1: int,
140+
last_value: float,
141+
x_2: int,
142+
value: float,
143+
y_bottom: int,
144+
y_top: int,
145+
) -> None:
128146

129147
y_2 = int(self.height * (y_top - value) / (y_top - y_bottom))
130148
y_1 = int(self.height * (y_top - last_value) / (y_top - y_bottom))
131149
self.append(Line(x_1, y_1, x_2, y_2, self.color)) # plot the line
132150

133151
# pylint: disable= too-many-branches, too-many-nested-blocks
134152

135-
def update(self):
153+
def update(self) -> None:
136154
"""Update the drawing of the sparkline."""
137155

138156
# get the y range
@@ -224,7 +242,7 @@ def update(self):
224242

225243
last_value = value # store value for the next iteration
226244

227-
def values(self):
245+
def values(self) -> List[float]:
228246
"""Returns the values displayed on the sparkline."""
229247

230248
return self._spark_list

0 commit comments

Comments
 (0)