From 9d5b1ef5e6735b29fc5cd902cd355881be28a37d Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Sun, 28 Jul 2024 19:44:04 -0700 Subject: [PATCH] type annotation updates --- masque/builder/builder.py | 6 +++--- masque/builder/pather.py | 4 ++-- masque/builder/renderpather.py | 2 +- masque/file/oasis.py | 4 ++-- masque/file/utils.py | 2 +- masque/library.py | 12 ++++++------ masque/pattern.py | 2 +- masque/ports.py | 2 +- masque/repetition.py | 4 ++-- masque/shapes/circle.py | 2 +- masque/traits/layerable.py | 2 +- masque/traits/mirrorable.py | 6 +++--- masque/traits/repeatable.py | 4 ++-- masque/traits/rotatable.py | 2 +- masque/traits/scalable.py | 2 +- 15 files changed, 28 insertions(+), 28 deletions(-) diff --git a/masque/builder/builder.py b/masque/builder/builder.py index dd971d2..b92c9ac 100644 --- a/masque/builder/builder.py +++ b/masque/builder/builder.py @@ -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 diff --git a/masque/builder/pather.py b/masque/builder/pather.py index 0879882..7cb434d 100644 --- a/masque/builder/pather.py +++ b/masque/builder/pather.py @@ -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, diff --git a/masque/builder/renderpather.py b/masque/builder/renderpather.py index e58386d..6c42519 100644 --- a/masque/builder/renderpather.py +++ b/masque/builder/renderpather.py @@ -128,7 +128,7 @@ class RenderPather(PortList): @classmethod def interface( - cls, + cls: type['RenderPather'], source: PortList | Mapping[str, Port] | str, *, library: ILibrary | None = None, diff --git a/masque/file/oasis.py b/masque/file/oasis.py index 1424004..10064b9 100644 --- a/masque/file/oasis.py +++ b/masque/file/oasis.py @@ -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 diff --git a/masque/file/utils.py b/masque/file/utils.py index 2d072e7..95248ae 100644 --- a/masque/file/utils.py +++ b/masque/file/utils.py @@ -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() diff --git a/masque/library.py b/masque/library.py index 86c22ae..d5b3136 100644 --- a/masque/library.py +++ b/masque/library.py @@ -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'' @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 diff --git a/masque/pattern.py b/masque/pattern.py index a0fb58e..637b953 100644 --- a/masque/pattern.py +++ b/masque/pattern.py @@ -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_', diff --git a/masque/ports.py b/masque/ports.py index a7b2594..69e5399 100644 --- a/masque/ports.py +++ b/masque/ports.py @@ -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: diff --git a/masque/repetition.py b/masque/repetition.py index 5436c11..2272770 100644 --- a/masque/repetition.py +++ b/masque/repetition.py @@ -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, diff --git a/masque/shapes/circle.py b/masque/shapes/circle.py index 628f8b8..d35a7a8 100644 --- a/masque/shapes/circle.py +++ b/masque/shapes/circle.py @@ -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),), diff --git a/masque/traits/layerable.py b/masque/traits/layerable.py index 2ccd65e..639d97a 100644 --- a/masque/traits/layerable.py +++ b/masque/traits/layerable.py @@ -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 # diff --git a/masque/traits/mirrorable.py b/masque/traits/mirrorable.py index c547780..bb7f011 100644 --- a/masque/traits/mirrorable.py +++ b/masque/traits/mirrorable.py @@ -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) diff --git a/masque/traits/repeatable.py b/masque/traits/repeatable.py index 838e12b..fbd765f 100644 --- a/masque/traits/repeatable.py +++ b/masque/traits/repeatable.py @@ -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!') diff --git a/masque/traits/rotatable.py b/masque/traits/rotatable.py index 850f70a..45caa31 100644 --- a/masque/traits/rotatable.py +++ b/masque/traits/rotatable.py @@ -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) diff --git a/masque/traits/scalable.py b/masque/traits/scalable.py index a3d21e2..bdbe399 100644 --- a/masque/traits/scalable.py +++ b/masque/traits/scalable.py @@ -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: