From ff8ca929633ea999b344414dac47de0535638c2f Mon Sep 17 00:00:00 2001 From: jan Date: Mon, 16 Feb 2026 20:48:15 -0800 Subject: [PATCH] cleanup --- masque/builder/pather.py | 3 ++- masque/builder/renderpather.py | 3 ++- masque/library.py | 10 +++++----- masque/pattern.py | 4 ++-- masque/shapes/polygon.py | 2 +- masque/shapes/shape.py | 8 ++++---- masque/shapes/text.py | 4 ++-- masque/test/test_boolean.py | 1 + masque/test/test_label.py | 4 +--- masque/traits/repeatable.py | 2 +- masque/utils/boolean.py | 4 +++- masque/utils/vertices.py | 6 +++--- pyproject.toml | 2 ++ 13 files changed, 29 insertions(+), 24 deletions(-) diff --git a/masque/builder/pather.py b/masque/builder/pather.py index 387b0d8..a3c4dc5 100644 --- a/masque/builder/pather.py +++ b/masque/builder/pather.py @@ -400,11 +400,12 @@ class Pather(Builder, PatherMixin): kwargs_plug = kwargs | {'plug_into': plug_into} self.path(portspec, ccw0, length - abs(jog1), **kwargs_no_out) self.path(portspec, not ccw0, abs(jog - jog0), **kwargs_plug) - return self except (BuildError, NotImplementedError): if not self._dead: raise # Fall through to dummy extension below + else: + return self except BuildError: if not self._dead: raise diff --git a/masque/builder/renderpather.py b/masque/builder/renderpather.py index c47232f..fae975a 100644 --- a/masque/builder/renderpather.py +++ b/masque/builder/renderpather.py @@ -528,11 +528,12 @@ class RenderPather(PatherMixin): kwargs_plug = kwargs | {'plug_into': plug_into} self.path(portspec, ccw0, length - abs(jog1), **kwargs_no_out) self.path(portspec, not ccw0, abs(jog - jog0), **kwargs_plug) - return self except (BuildError, NotImplementedError): if not self._dead: raise # Fall through to dummy extension below + else: + return self except BuildError: if not self._dead: raise diff --git a/masque/library.py b/masque/library.py index 0ed5271..3e1c65c 100644 --- a/masque/library.py +++ b/masque/library.py @@ -763,7 +763,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta): Returns: (name, pattern) tuple """ - from .pattern import Pattern + from .pattern import Pattern #noqa: PLC0415 pat = Pattern() self[name] = pat return name, pat @@ -803,7 +803,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta): Raises: `LibraryError` if a duplicate name is encountered even after applying `rename_theirs()`. """ - from .pattern import map_targets + from .pattern import map_targets #noqa: PLC0415 duplicates = set(self.keys()) & set(other.keys()) if not duplicates: @@ -909,7 +909,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta): # This currently simplifies globally (same shape in different patterns is # merged into the same ref target). - from .pattern import Pattern + from .pattern import Pattern #noqa: PLC0415 if exclude_types is None: exclude_types = () @@ -1002,7 +1002,7 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta): Returns: self """ - from .pattern import Pattern + from .pattern import Pattern #noqa: PLC0415 if name_func is None: def name_func(_pat: Pattern, _shape: Shape | Label) -> str: @@ -1205,7 +1205,7 @@ class Library(ILibrary): Returns: The newly created `Library` and the newly created `Pattern` """ - from .pattern import Pattern + from .pattern import Pattern #noqa: PLC0415 tree = cls() pat = Pattern() tree[name] = pat diff --git a/masque/pattern.py b/masque/pattern.py index 6b2de99..4c72fdf 100644 --- a/masque/pattern.py +++ b/masque/pattern.py @@ -1066,8 +1066,8 @@ class Pattern(PortList, AnnotatableImpl, Mirrorable): """ # TODO: add text labels to visualize() try: - from matplotlib import pyplot # type: ignore - import matplotlib.collections # type: ignore + from matplotlib import pyplot # type: ignore #noqa: PLC0415 + import matplotlib.collections # type: ignore #noqa: PLC0415 except ImportError: logger.exception('Pattern.visualize() depends on matplotlib!\n' + 'Make sure to install masque with the [visualize] option to pull in the needed dependencies.') diff --git a/masque/shapes/polygon.py b/masque/shapes/polygon.py index a243901..1fd6350 100644 --- a/masque/shapes/polygon.py +++ b/masque/shapes/polygon.py @@ -480,5 +480,5 @@ class Polygon(Shape): Returns: A list of resulting Polygons. """ - from ..utils.boolean import boolean + from ..utils.boolean import boolean #noqa: PLC0415 return boolean([self], other, operation=operation, scale=scale) diff --git a/masque/shapes/shape.py b/masque/shapes/shape.py index 13d2e1e..ee6f19a 100644 --- a/masque/shapes/shape.py +++ b/masque/shapes/shape.py @@ -121,7 +121,7 @@ class Shape(FlippableImpl, PivotableImpl, RepeatableImpl, AnnotatableImpl, Returns: List of `Polygon` objects with grid-aligned edges. """ - from . import Polygon + from . import Polygon #noqa: PLC0415 gx = numpy.unique(grid_x) gy = numpy.unique(grid_y) @@ -250,9 +250,9 @@ class Shape(FlippableImpl, PivotableImpl, RepeatableImpl, AnnotatableImpl, Returns: List of `Polygon` objects with grid-aligned edges. """ - from . import Polygon - import skimage.measure # type: ignore - import float_raster + from . import Polygon #noqa: PLC0415 + import skimage.measure #noqa: PLC0415 + import float_raster #noqa: PLC0415 grx = numpy.unique(grid_x) gry = numpy.unique(grid_y) diff --git a/masque/shapes/text.py b/masque/shapes/text.py index 65a9213..dec4c33 100644 --- a/masque/shapes/text.py +++ b/masque/shapes/text.py @@ -205,8 +205,8 @@ def get_char_as_polygons( char: str, resolution: float = 48 * 64, ) -> tuple[list[NDArray[numpy.float64]], float]: - from freetype import Face # type: ignore - from matplotlib.path import Path # type: ignore + from freetype import Face # type: ignore #noqa: PLC0415 + from matplotlib.path import Path # type: ignore #noqa: PLC0415 """ Get a list of polygons representing a single character. diff --git a/masque/test/test_boolean.py b/masque/test/test_boolean.py index c1a2d7b..bf5d33d 100644 --- a/masque/test/test_boolean.py +++ b/masque/test/test_boolean.py @@ -1,3 +1,4 @@ +# ruff: noqa: PLC0415 import pytest import numpy from numpy.testing import assert_allclose diff --git a/masque/test/test_label.py b/masque/test/test_label.py index ed40614..ad8c08b 100644 --- a/masque/test/test_label.py +++ b/masque/test/test_label.py @@ -4,6 +4,7 @@ from numpy import pi from ..label import Label from ..repetition import Grid +from ..utils import annotations_eq def test_label_init() -> None: @@ -39,9 +40,6 @@ def test_label_copy() -> None: print(f"l1: string={l1.string}, offset={l1.offset}, repetition={l1.repetition}, annotations={l1.annotations}") print(f"l2: string={l2.string}, offset={l2.offset}, repetition={l2.repetition}, annotations={l2.annotations}") - - from ..utils import annotations_eq - print(f"annotations_eq: {annotations_eq(l1.annotations, l2.annotations)}") assert l1 == l2 diff --git a/masque/traits/repeatable.py b/masque/traits/repeatable.py index fbd765f..dbf4fad 100644 --- a/masque/traits/repeatable.py +++ b/masque/traits/repeatable.py @@ -76,7 +76,7 @@ class RepeatableImpl(Repeatable, Bounded, metaclass=ABCMeta): @repetition.setter def repetition(self, repetition: 'Repetition | None') -> None: - from ..repetition import Repetition + from ..repetition import Repetition #noqa: PLC0415 if repetition is not None and not isinstance(repetition, Repetition): raise MasqueError(f'{repetition} is not a valid Repetition object!') self._repetition = repetition diff --git a/masque/utils/boolean.py b/masque/utils/boolean.py index 9b9514e..78c24e2 100644 --- a/masque/utils/boolean.py +++ b/masque/utils/boolean.py @@ -8,8 +8,10 @@ from numpy.typing import NDArray from ..shapes.polygon import Polygon from ..error import PatternError + logger = logging.getLogger(__name__) + def _bridge_holes(outer_path: NDArray[numpy.float64], holes: list[NDArray[numpy.float64]]) -> NDArray[numpy.float64]: """ Bridge multiple holes into an outer boundary using zero-width slits. @@ -96,7 +98,7 @@ def boolean( A list of result Polygons. """ try: - import pyclipper + import pyclipper #noqa: PLC0415 except ImportError: raise ImportError( "Boolean operations require 'pyclipper'. " diff --git a/masque/utils/vertices.py b/masque/utils/vertices.py index 36d2e59..7d3791c 100644 --- a/masque/utils/vertices.py +++ b/masque/utils/vertices.py @@ -87,7 +87,7 @@ def poly_contains_points( max_bounds = numpy.max(vertices, axis=0)[None, :] trivially_outside = ((points < min_bounds).any(axis=1) - | (points > max_bounds).any(axis=1)) # noqa: E128 + | (points > max_bounds).any(axis=1)) nontrivial = ~trivially_outside if trivially_outside.all(): @@ -105,10 +105,10 @@ def poly_contains_points( dv = numpy.roll(verts, -1, axis=0) - verts is_left = (dv[:, 0] * (ntpts[..., 1] - verts[:, 1]) # >0 if left of dv, <0 if right, 0 if on the line - - dv[:, 1] * (ntpts[..., 0] - verts[:, 0])) # noqa: E128 + - dv[:, 1] * (ntpts[..., 0] - verts[:, 0])) winding_number = ((upward & (is_left > 0)).sum(axis=0) - - (downward & (is_left < 0)).sum(axis=0)) # noqa: E128 + - (downward & (is_left < 0)).sum(axis=0)) nontrivial_inside = winding_number != 0 # filter nontrivial points based on winding number if include_boundary: diff --git a/pyproject.toml b/pyproject.toml index 15ba402..ba7a240 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,6 +102,8 @@ lint.ignore = [ "PLR09", # Too many xxx "PLR2004", # magic number "PLC0414", # import x as x +# "PLC0415", # non-top-level import + "PLW1641", # missing __hash__ with total_ordering "TRY003", # Long exception message ]