Skip to content

Added Type Annotations #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions adafruit_display_shapes/circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

"""

try:
from typing import Optional
except ImportError:
pass

from adafruit_display_shapes.roundrect import RoundRect

__version__ = "0.0.0-auto.0"
Expand All @@ -42,7 +47,16 @@ class Circle(RoundRect):

"""

def __init__(self, x0, y0, r, *, fill=None, outline=None, stroke=1):
def __init__(
self,
x0: int,
y0: int,
r: int,
*,
fill: Optional[int] = None,
outline: Optional[int] = None,
stroke: int = 1,
) -> None:
super().__init__(
x0 - r,
y0 - r,
Expand All @@ -56,19 +70,19 @@ def __init__(self, x0, y0, r, *, fill=None, outline=None, stroke=1):
self.r = r

@property
def x0(self):
def x0(self) -> int:
"""The x-position of the center of the circle."""
return self.x + self.r

@property
def y0(self):
def y0(self) -> int:
"""The y-position of the center of the circle."""
return self.y + self.r

@x0.setter
def x0(self, x0):
def x0(self, x0: int) -> None:
self.x = x0 - self.r

@y0.setter
def y0(self, y0):
def y0(self, y0: int) -> None:
self.y = y0 - self.r
13 changes: 10 additions & 3 deletions adafruit_display_shapes/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,22 @@ class Line(Polygon):
:param color: The color of the line.
"""

def __init__(self, x0, y0, x1, y1, color):
def __init__(
self,
x0: int,
y0: int,
x1: int,
y1: int,
color: int,
) -> None:
super().__init__([(x0, y0), (x1, y1)], outline=color)

@property
def color(self):
def color(self) -> int:
"""The line color value. Can be a hex value for a color or
``None`` for no line color."""
return self.outline

@color.setter
def color(self, color):
def color(self, color: int) -> None:
self.outline = color
25 changes: 21 additions & 4 deletions adafruit_display_shapes/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

"""

try:
from typing import Optional, List, Tuple
except ImportError:
pass

import displayio

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

def __init__(self, points, *, outline=None):
def __init__(
self,
points: List[Tuple[int, int]],
*,
outline: Optional[int] = None,
) -> None:
xs = []
ys = []

Expand Down Expand Up @@ -77,7 +87,14 @@ def __init__(self, points, *, outline=None):
)

# pylint: disable=invalid-name, too-many-locals, too-many-branches
def _line(self, x0, y0, x1, y1, color):
def _line(
self,
x0: int,
y0: int,
x1: int,
y1: int,
color: int,
) -> None:
if x0 == x1:
if y0 > y1:
y0, y1 = y1, y0
Expand Down Expand Up @@ -121,13 +138,13 @@ def _line(self, x0, y0, x1, y1, color):
# pylint: enable=invalid-name, too-many-locals, too-many-branches

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

@outline.setter
def outline(self, color):
def outline(self, color: int) -> None:
if color is None:
self._palette[1] = 0
self._palette.make_transparent(1)
Expand Down
25 changes: 20 additions & 5 deletions adafruit_display_shapes/rect.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@

"""

try:
from typing import Optional
except ImportError:
pass

import displayio

__version__ = "0.0.0-auto.0"
Expand All @@ -43,7 +48,17 @@ class Rect(displayio.TileGrid):

"""

def __init__(self, x, y, width, height, *, fill=None, outline=None, stroke=1):
def __init__(
self,
x: int,
y: int,
width: int,
height: int,
*,
fill: Optional[int] = None,
outline: Optional[int] = None,
stroke: int = 1,
) -> None:
self._bitmap = displayio.Bitmap(width, height, 2)
self._palette = displayio.Palette(2)

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

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

@fill.setter
def fill(self, color):
def fill(self, color: int) -> None:
if color is None:
self._palette[0] = 0
self._palette.make_transparent(0)
Expand All @@ -82,13 +97,13 @@ def fill(self, color):
self._palette.make_opaque(0)

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

@outline.setter
def outline(self, color):
def outline(self, color: int) -> None:
if color is None:
self._palette[1] = 0
self._palette.make_transparent(1)
Expand Down
46 changes: 31 additions & 15 deletions adafruit_display_shapes/roundrect.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

"""

try:
from typing import Optional
except ImportError:
pass

import displayio

__version__ = "0.0.0-auto.0"
Expand All @@ -35,7 +40,18 @@ class RoundRect(displayio.TileGrid):

"""

def __init__(self, x, y, width, height, r, *, fill=None, outline=None, stroke=1):
def __init__(
self,
x: int,
y: int,
width: int,
height: int,
r: int,
*,
fill: Optional[int] = None,
outline: Optional[int] = None,
stroke: int = 1,
) -> None:
self._palette = displayio.Palette(3)
self._palette.make_transparent(0)
self._bitmap = displayio.Bitmap(width, height, 3)
Expand Down Expand Up @@ -85,17 +101,17 @@ def __init__(self, x, y, width, height, r, *, fill=None, outline=None, stroke=1)
# pylint: disable=invalid-name, too-many-locals, too-many-branches
def _helper(
self,
x0,
y0,
r,
x0: int,
y0: int,
r: int,
*,
color,
x_offset=0,
y_offset=0,
stroke=1,
corner_flags=0xF,
fill=False
):
color: int,
x_offset: int = 0,
y_offset: int = 0,
stroke: int = 1,
corner_flags: int = 0xF,
fill: bool = False,
) -> None:
f = 1 - r
ddF_x = 1
ddF_y = -2 * r
Expand Down Expand Up @@ -142,13 +158,13 @@ def _helper(
# pylint: enable=invalid-name, too-many-locals, too-many-branches

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

@fill.setter
def fill(self, color):
def fill(self, color: int) -> None:
if color is None:
self._palette[2] = 0
self._palette.make_transparent(2)
Expand All @@ -157,13 +173,13 @@ def fill(self, color):
self._palette.make_opaque(2)

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

@outline.setter
def outline(self, color):
def outline(self, color: int) -> None:
if color is None:
self._palette[1] = 0
self._palette.make_transparent(1)
Expand Down
50 changes: 34 additions & 16 deletions adafruit_display_shapes/sparkline.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@

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

try:
from typing import Optional, List
except ImportError:
pass
import displayio
from adafruit_display_shapes.line import Line

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

def __init__(
self,
width,
height,
max_items,
y_min=None, # None = autoscaling
y_max=None, # None = autoscaling
x=0,
y=0,
color=0xFFFFFF, # line color, default is WHITE
):
width: int,
height: int,
max_items: int,
y_min: Optional[int] = None, # None = autoscaling
y_max: Optional[int] = None, # None = autoscaling
x: int = 0,
y: int = 0,
color: int = 0xFFFFFF, # line color, default is WHITE
) -> None:

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

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

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

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

def add_value(self, value):
def add_value(self, value: float) -> None:
"""Add a value to the sparkline.
:param value: The value to be added to the sparkline
"""
Expand All @@ -111,8 +115,14 @@ def add_value(self, value):
# pylint: disable=no-else-return
@staticmethod
def _xintercept(
x_1, y_1, x_2, y_2, horizontal_y
): # finds intercept of the line and a horizontal line at horizontalY
x_1: int,
y_1: float,
x_2: int,
y_2: float,
horizontal_y: int,
) -> Optional[
int
]: # finds intercept of the line and a horizontal line at horizontalY
slope = (y_2 - y_1) / (x_2 - x_1)
b = y_1 - slope * x_1

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

def _plotline(self, x_1, last_value, x_2, value, y_bottom, y_top):
def _plotline(
self,
x_1: int,
last_value: float,
x_2: int,
value: float,
y_bottom: int,
y_top: int,
) -> None:

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

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

def update(self):
def update(self) -> None:
"""Update the drawing of the sparkline."""

# get the y range
Expand Down Expand Up @@ -224,7 +242,7 @@ def update(self):

last_value = value # store value for the next iteration

def values(self):
def values(self) -> List[float]:
"""Returns the values displayed on the sparkline."""

return self._spark_list
Loading