modernize type annotations

This commit is contained in:
Jan Petykiewicz 2023-02-23 13:15:32 -08:00 committed by jan
commit 1463535676
34 changed files with 409 additions and 444 deletions

View file

@ -1,4 +1,4 @@
from typing import List, Dict, Optional, Sequence, Any
from typing import Sequence, Any
import copy
import math
@ -157,8 +157,8 @@ class Arc(Shape):
rotation: float = 0,
mirrored: Sequence[bool] = (False, False),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
annotations: Optional[annotations_t] = None,
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
raw: bool = False,
) -> None:
if raw:
@ -184,7 +184,7 @@ class Arc(Shape):
self.layer = layer
[self.mirror(a) for a, do in enumerate(mirrored) if do]
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'Arc':
def __deepcopy__(self, memo: dict | None = None) -> 'Arc':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
@ -195,9 +195,9 @@ class Arc(Shape):
def to_polygons(
self,
num_vertices: Optional[int] = DEFAULT_POLY_NUM_VERTICES,
max_arclen: Optional[float] = None,
) -> List[Polygon]:
num_vertices: int | None = DEFAULT_POLY_NUM_VERTICES,
max_arclen: float | None = None,
) -> list[Polygon]:
if (num_vertices is None) and (max_arclen is None):
raise PatternError('Max number of points and arclength left unspecified'
+ ' (default was also overridden)')

View file

@ -1,4 +1,3 @@
from typing import List, Dict, Optional
import copy
import numpy
@ -46,8 +45,8 @@ class Circle(Shape):
*,
offset: ArrayLike = (0.0, 0.0),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
annotations: Optional[annotations_t] = None,
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
raw: bool = False,
) -> None:
if raw:
@ -64,7 +63,7 @@ class Circle(Shape):
self.annotations = annotations if annotations is not None else {}
self.layer = layer
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'Circle':
def __deepcopy__(self, memo: dict | None = None) -> 'Circle':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
@ -73,14 +72,14 @@ class Circle(Shape):
def to_polygons(
self,
num_vertices: Optional[int] = DEFAULT_POLY_NUM_VERTICES,
max_arclen: Optional[float] = None,
) -> List[Polygon]:
num_vertices: int | None = DEFAULT_POLY_NUM_VERTICES,
max_arclen: float | None = None,
) -> list[Polygon]:
if (num_vertices is None) and (max_arclen is None):
raise PatternError('Number of points and arclength left '
'unspecified (default was also overridden)')
n: List[float] = []
n: list[float] = []
if num_vertices is not None:
n += [num_vertices]
if max_arclen is not None:

View file

@ -1,4 +1,4 @@
from typing import List, Dict, Sequence, Optional, Any
from typing import Sequence, Any
import copy
import math
@ -92,8 +92,8 @@ class Ellipse(Shape):
rotation: float = 0,
mirrored: Sequence[bool] = (False, False),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
annotations: Optional[annotations_t] = None,
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
raw: bool = False,
) -> None:
if raw:
@ -114,7 +114,7 @@ class Ellipse(Shape):
self.layer = layer
[self.mirror(a) for a, do in enumerate(mirrored) if do]
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'Ellipse':
def __deepcopy__(self, memo: dict | None = None) -> 'Ellipse':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
@ -124,9 +124,9 @@ class Ellipse(Shape):
def to_polygons(
self,
num_vertices: Optional[int] = DEFAULT_POLY_NUM_VERTICES,
max_arclen: Optional[float] = None,
) -> List[Polygon]:
num_vertices: int | None = DEFAULT_POLY_NUM_VERTICES,
max_arclen: float | None = None,
) -> list[Polygon]:
if (num_vertices is None) and (max_arclen is None):
raise PatternError('Number of points and arclength left unspecified'
' (default was also overridden)')

View file

@ -1,4 +1,4 @@
from typing import List, Tuple, Dict, Optional, Sequence, Any, cast
from typing import Sequence, Any, cast
import copy
from enum import Enum
@ -36,7 +36,7 @@ class Path(Shape):
_vertices: NDArray[numpy.float64]
_width: float
_cap: PathCap
_cap_extensions: Optional[NDArray[numpy.float64]]
_cap_extensions: NDArray[numpy.float64] | None
Cap = PathCap
@ -76,7 +76,7 @@ class Path(Shape):
# cap_extensions property
@property
def cap_extensions(self) -> Optional[Any]: # TODO mypy#3004 NDArray[numpy.float64]]:
def cap_extensions(self) -> Any | None: # TODO mypy#3004 NDArray[numpy.float64]]:
"""
Path end-cap extension
@ -86,7 +86,7 @@ class Path(Shape):
return self._cap_extensions
@cap_extensions.setter
def cap_extensions(self, vals: Optional[ArrayLike]) -> None:
def cap_extensions(self, vals: ArrayLike | None) -> None:
custom_caps = (PathCap.SquareCustom,)
if self.cap in custom_caps:
if vals is None:
@ -150,13 +150,13 @@ class Path(Shape):
width: float = 0.0,
*,
cap: PathCap = PathCap.Flush,
cap_extensions: Optional[ArrayLike] = None,
cap_extensions: ArrayLike | None = None,
offset: ArrayLike = (0.0, 0.0),
rotation: float = 0,
mirrored: Sequence[bool] = (False, False),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
annotations: Optional[annotations_t] = None,
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
raw: bool = False,
) -> None:
self._cap_extensions = None # Since .cap setter might access it
@ -185,7 +185,7 @@ class Path(Shape):
self.rotate(rotation)
[self.mirror(a) for a, do in enumerate(mirrored) if do]
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'Path':
def __deepcopy__(self, memo: dict | None = None) -> 'Path':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
@ -197,10 +197,10 @@ class Path(Shape):
@staticmethod
def travel(
travel_pairs: Sequence[Tuple[float, float]],
travel_pairs: Sequence[tuple[float, float]],
width: float = 0.0,
cap: PathCap = PathCap.Flush,
cap_extensions: Optional[Tuple[float, float]] = None,
cap_extensions: tuple[float, float] | None = None,
offset: ArrayLike = (0.0, 0.0),
rotation: float = 0,
mirrored: Sequence[bool] = (False, False),
@ -243,9 +243,9 @@ class Path(Shape):
def to_polygons(
self,
num_vertices: Optional[int] = None,
max_arclen: Optional[float] = None,
) -> List['Polygon']:
num_vertices: int | None = None,
max_arclen: float | None = None,
) -> list['Polygon']:
extensions = self._calculate_cap_extensions()
v = remove_colinear_vertices(self.vertices, closed_path=False)

View file

@ -1,4 +1,4 @@
from typing import List, Dict, Optional, Sequence, Any, cast
from typing import Sequence, Any, cast
import copy
import numpy
@ -83,8 +83,8 @@ class Polygon(Shape):
rotation: float = 0.0,
mirrored: Sequence[bool] = (False, False),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
annotations: Optional[annotations_t] = None,
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
raw: bool = False,
) -> None:
if raw:
@ -104,7 +104,7 @@ class Polygon(Shape):
self.rotate(rotation)
[self.mirror(a) for a, do in enumerate(mirrored) if do]
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'Polygon':
def __deepcopy__(self, memo: dict | None = None) -> 'Polygon':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
@ -119,7 +119,7 @@ class Polygon(Shape):
rotation: float = 0.0,
offset: ArrayLike = (0.0, 0.0),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
repetition: Repetition | None = None,
) -> 'Polygon':
"""
Draw a square given side_length, centered on the origin.
@ -151,7 +151,7 @@ class Polygon(Shape):
rotation: float = 0,
offset: ArrayLike = (0.0, 0.0),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
repetition: Repetition | None = None,
) -> 'Polygon':
"""
Draw a rectangle with side lengths lx and ly, centered on the origin.
@ -178,16 +178,16 @@ class Polygon(Shape):
@staticmethod
def rect(
*,
xmin: Optional[float] = None,
xctr: Optional[float] = None,
xmax: Optional[float] = None,
lx: Optional[float] = None,
ymin: Optional[float] = None,
yctr: Optional[float] = None,
ymax: Optional[float] = None,
ly: Optional[float] = None,
xmin: float | None = None,
xctr: float | None = None,
xmax: float | None = None,
lx: float | None = None,
ymin: float | None = None,
yctr: float | None = None,
ymax: float | None = None,
ly: float | None = None,
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
repetition: Repetition | None = None,
) -> 'Polygon':
"""
Draw a rectangle by specifying side/center positions.
@ -276,13 +276,13 @@ class Polygon(Shape):
@staticmethod
def octagon(
*,
side_length: Optional[float] = None,
inner_radius: Optional[float] = None,
side_length: float | None = None,
inner_radius: float | None = None,
regular: bool = True,
center: ArrayLike = (0.0, 0.0),
rotation: float = 0.0,
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
repetition: Repetition | None = None,
) -> 'Polygon':
"""
Draw an octagon given one of (side length, inradius, circumradius).
@ -333,9 +333,9 @@ class Polygon(Shape):
def to_polygons(
self,
num_vertices: Optional[int] = None, # unused
max_arclen: Optional[float] = None, # unused
) -> List['Polygon']:
num_vertices: int | None = None, # unused
max_arclen: float | None = None, # unused
) -> list['Polygon']:
return [copy.deepcopy(self)]
def get_bounds(self) -> NDArray[numpy.float64]:

View file

@ -1,4 +1,4 @@
from typing import List, Tuple, Callable, TypeVar, Optional, TYPE_CHECKING
from typing import Callable, TypeVar, TYPE_CHECKING
from abc import ABCMeta, abstractmethod
import numpy
@ -15,9 +15,9 @@ if TYPE_CHECKING:
# Type definitions
normalized_shape_tuple = Tuple[
Tuple,
Tuple[NDArray[numpy.float64], float, float, bool],
normalized_shape_tuple = tuple[
tuple,
tuple[NDArray[numpy.float64], float, float, bool],
Callable[[], 'Shape'],
]
@ -49,9 +49,9 @@ class Shape(PositionableImpl, LayerableImpl, Rotatable, Mirrorable, Copyable, Sc
@abstractmethod
def to_polygons(
self,
num_vertices: Optional[int] = None,
max_arclen: Optional[float] = None,
) -> List['Polygon']:
num_vertices: int | None = None,
max_arclen: float | None = None,
) -> list['Polygon']:
"""
Returns a list of polygons which approximate the shape.
@ -98,7 +98,7 @@ class Shape(PositionableImpl, LayerableImpl, Rotatable, Mirrorable, Copyable, Sc
self,
grid_x: ArrayLike,
grid_y: ArrayLike,
) -> List['Polygon']:
) -> list['Polygon']:
"""
Returns a list of polygons with grid-aligned ("Manhattan") edges approximating the shape.
@ -208,7 +208,7 @@ class Shape(PositionableImpl, LayerableImpl, Rotatable, Mirrorable, Copyable, Sc
self,
grid_x: ArrayLike,
grid_y: ArrayLike,
) -> List['Polygon']:
) -> list['Polygon']:
"""
Returns a list of polygons with grid-aligned ("Manhattan") edges approximating the shape.

View file

@ -1,4 +1,4 @@
from typing import List, Tuple, Dict, Sequence, Optional, Any
from typing import Sequence, Any
import copy
import numpy
@ -74,8 +74,8 @@ class Text(RotatableImpl, Shape):
rotation: float = 0.0,
mirrored: ArrayLike = (False, False),
layer: layer_t = 0,
repetition: Optional[Repetition] = None,
annotations: Optional[annotations_t] = None,
repetition: Repetition | None = None,
annotations: annotations_t | None = None,
raw: bool = False,
) -> None:
if raw:
@ -100,7 +100,7 @@ class Text(RotatableImpl, Shape):
self.annotations = annotations if annotations is not None else {}
self.font_path = font_path
def __deepcopy__(self, memo: Optional[Dict] = None) -> 'Text':
def __deepcopy__(self, memo: dict | None = None) -> 'Text':
memo = {} if memo is None else memo
new = copy.copy(self)
new._offset = self._offset.copy()
@ -110,9 +110,9 @@ class Text(RotatableImpl, Shape):
def to_polygons(
self,
num_vertices: Optional[int] = None, # unused
max_arclen: Optional[float] = None, # unused
) -> List[Polygon]:
num_vertices: int | None = None, # unused
max_arclen: float | None = None, # unused
) -> list[Polygon]:
all_polygons = []
total_advance = 0.0
for char in self.string:
@ -172,7 +172,7 @@ def get_char_as_polygons(
font_path: str,
char: str,
resolution: float = 48 * 64,
) -> Tuple[List[List[List[float]]], float]:
) -> tuple[list[list[list[float]]], float]:
from freetype import Face # type: ignore
from matplotlib.path import Path # type: ignore
@ -209,7 +209,7 @@ def get_char_as_polygons(
tags = outline.tags[start:end + 1]
tags.append(tags[0])
segments: List[List[List[float]]] = []
segments: list[list[list[float]]] = []
for j, point in enumerate(points):
# If we already have a segment, add this point to it
if j > 0: