type annotation updates

This commit is contained in:
Jan Petykiewicz 2024-07-28 19:44:04 -07:00
parent 3d50ff0070
commit 9d5b1ef5e6
15 changed files with 28 additions and 28 deletions

View File

@ -138,7 +138,7 @@ class Builder(PortList):
@classmethod
def interface(
cls,
cls: type['Builder'],
source: PortList | Mapping[str, Port] | str,
*,
library: ILibrary | None = None,
@ -276,7 +276,7 @@ class Builder(PortList):
logger.error('Skipping plug() since device is dead')
return self
if not isinstance(other, (str, Abstract, Pattern)):
if not isinstance(other, str | Abstract | Pattern):
# We got a Tree; add it into self.library and grab an Abstract for it
other = self.library << other
@ -348,7 +348,7 @@ class Builder(PortList):
logger.error('Skipping place() since device is dead')
return self
if not isinstance(other, (str, Abstract, Pattern)):
if not isinstance(other, str | Abstract | Pattern):
# We got a Tree; add it into self.library and grab an Abstract for it
other = self.library << other

View File

@ -175,7 +175,7 @@ class Pather(Builder):
@classmethod
def from_builder(
cls,
cls: type['Pather'],
builder: Builder,
*,
tools: Tool | MutableMapping[str | None, Tool] | None = None,
@ -195,7 +195,7 @@ class Pather(Builder):
@classmethod
def interface(
cls,
cls: type['Pather'],
source: PortList | Mapping[str, Port] | str,
*,
library: ILibrary | None = None,

View File

@ -128,7 +128,7 @@ class RenderPather(PortList):
@classmethod
def interface(
cls,
cls: type['RenderPather'],
source: PortList | Mapping[str, Port] | str,
*,
library: ILibrary | None = None,

View File

@ -693,9 +693,9 @@ def properties_to_annotations(
assert proprec.values is not None
for value in proprec.values:
if isinstance(value, (float, int)):
if isinstance(value, float | int):
values.append(value)
elif isinstance(value, (NString, AString)):
elif isinstance(value, NString | AString):
values.append(value.string)
elif isinstance(value, PropStringReference):
values.append(propstrings[value.ref].string) # dereference

View File

@ -117,7 +117,7 @@ def clean_pattern_vertices(pat: Pattern) -> Pattern:
for shapes in pat.shapes.values():
remove_inds = []
for ii, shape in enumerate(shapes):
if not isinstance(shape, (Polygon, Path)):
if not isinstance(shape, Polygon | Path):
continue
try:
shape.clean_vertices()

View File

@ -14,7 +14,7 @@ Classes include:
- `AbstractView`: Provides a way to use []-indexing to generate abstracts for patterns in the linked
library. Generated with `ILibraryView.abstract_view()`.
"""
from typing import Callable, Self, Type, TYPE_CHECKING, cast, TypeAlias, Protocol, Literal
from typing import Self, TYPE_CHECKING, cast, TypeAlias, Protocol, Literal
from collections.abc import Iterator, Mapping, MutableMapping, Sequence, Callable
import logging
import base64
@ -285,7 +285,7 @@ class ILibraryView(Mapping[str, 'Pattern'], metaclass=ABCMeta):
if isinstance(tops, str):
tops = (tops,)
flattened: dict[str, 'Pattern | None'] = {}
flattened: dict[str, Pattern | None] = {}
def flatten_single(name: str) -> None:
flattened[name] = None
@ -735,7 +735,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
def dedup(
self,
norm_value: int = int(1e6),
exclude_types: tuple[Type] = (Polygon,),
exclude_types: tuple[type] = (Polygon,),
label2name: Callable[[tuple], str] | None = None,
threshold: int = 2,
) -> Self:
@ -773,7 +773,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
exclude_types = ()
if label2name is None:
def label2name(label):
def label2name(label: tuple) -> str: # noqa: ARG001
return self.get_name(SINGLE_USE_PREFIX + 'shape')
shape_counts: MutableMapping[tuple, int] = defaultdict(int)
@ -863,7 +863,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
from .pattern import Pattern
if name_func is None:
def name_func(_pat, _shape):
def name_func(_pat: Pattern, _shape: Shape | Label) -> str:
return self.get_name(SINGLE_USE_PREFIX + 'rep')
for pat in tuple(self.values()):
@ -1054,7 +1054,7 @@ class Library(ILibrary):
return f'<Library ({type(self.mapping)}) with keys\n' + pformat(list(self.keys())) + '>'
@classmethod
def mktree(cls, name: str) -> tuple[Self, 'Pattern']:
def mktree(cls: type[Self], name: str) -> tuple[Self, 'Pattern']:
"""
Create a new Library and immediately add a pattern

View File

@ -1325,7 +1325,7 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable):
@classmethod
def interface(
cls,
cls: type['Pattern'],
source: PortList | Mapping[str, Port],
*,
in_prefix: str = 'in_',

View File

@ -93,7 +93,7 @@ class Port(PositionableImpl, Rotatable, PivotableImpl, Copyable, Mirrorable):
def copy(self) -> Self:
return self.deepcopy()
def get_bounds(self):
def get_bounds(self) -> NDArray[numpy.float64]:
return numpy.vstack((self.offset, self.offset))
def set_ptype(self, ptype: str) -> Self:

View File

@ -2,7 +2,7 @@
Repetitions provide support for efficiently representing multiple identical
instances of an object .
"""
from typing import Any, Type, Self, TypeVar, cast
from typing import Any, Self, TypeVar, cast
import copy
import functools
from abc import ABCMeta, abstractmethod
@ -116,7 +116,7 @@ class Grid(Repetition):
@classmethod
def aligned(
cls: Type[GG],
cls: type[GG],
x: float,
y: float,
x_count: int,

View File

@ -130,7 +130,7 @@ class Circle(Shape):
self.radius *= c
return self
def normalized_form(self, norm_value) -> normalized_shape_tuple:
def normalized_form(self, norm_value: float) -> normalized_shape_tuple:
rotation = 0.0
magnitude = self.radius / norm_value
return ((type(self),),

View File

@ -63,7 +63,7 @@ class LayerableImpl(Layerable, metaclass=ABCMeta):
return self._layer
@layer.setter
def layer(self, val: layer_t):
def layer(self, val: layer_t) -> None:
self._layer = val
#

View File

@ -44,7 +44,7 @@ class Mirrorable(metaclass=ABCMeta):
# """
# __slots__ = ()
#
# _mirrored: numpy.ndarray # ndarray[bool]
# _mirrored: NDArray[numpy.bool]
# """ Whether to mirror the instance across the x and/or y axes. """
#
# #
@ -52,12 +52,12 @@ class Mirrorable(metaclass=ABCMeta):
# #
# # Mirrored property
# @property
# def mirrored(self) -> numpy.ndarray: # ndarray[bool]
# def mirrored(self) -> NDArray[numpy.bool]:
# """ Whether to mirror across the [x, y] axes, respectively """
# return self._mirrored
#
# @mirrored.setter
# def mirrored(self, val: Sequence[bool]):
# def mirrored(self, val: Sequence[bool]) -> None:
# if is_scalar(val):
# raise MasqueError('Mirrored must be a 2-element list of booleans')
# self._mirrored = numpy.array(val, dtype=bool, copy=True)

View File

@ -34,7 +34,7 @@ class Repeatable(metaclass=ABCMeta):
# @repetition.setter
# @abstractmethod
# def repetition(self, repetition: 'Repetition | None'):
# def repetition(self, repetition: 'Repetition | None') -> None:
# pass
#
@ -75,7 +75,7 @@ class RepeatableImpl(Repeatable, Bounded, metaclass=ABCMeta):
return self._repetition
@repetition.setter
def repetition(self, repetition: 'Repetition | None'):
def repetition(self, repetition: 'Repetition | None') -> None:
from ..repetition import Repetition
if repetition is not None and not isinstance(repetition, Repetition):
raise MasqueError(f'{repetition} is not a valid Repetition object!')

View File

@ -54,7 +54,7 @@ class RotatableImpl(Rotatable, metaclass=ABCMeta):
return self._rotation
@rotation.setter
def rotation(self, val: float):
def rotation(self, val: float) -> None:
if not numpy.size(val) == 1:
raise MasqueError('Rotation must be a scalar')
self._rotation = val % (2 * pi)

View File

@ -48,7 +48,7 @@ class ScalableImpl(Scalable, metaclass=ABCMeta):
return self._scale
@scale.setter
def scale(self, val: float):
def scale(self, val: float) -> None:
if not is_scalar(val):
raise MasqueError('Scale must be a scalar')
if not val > 0: