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 all commits
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
18 changes: 15 additions & 3 deletions adafruit_display_shapes/line.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.polygon import Polygon

__version__ = "0.0.0-auto.0"
Expand All @@ -38,15 +43,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) -> Optional[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: Optional[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) -> Optional[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: Optional[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) -> Optional[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: Optional[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) -> Optional[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: Optional[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) -> Optional[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: Optional[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) -> Optional[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: Optional[int]) -> None:
if color is None:
self._palette[1] = 0
self._palette.make_transparent(1)
Expand Down
Loading