From 20f37ea0f78e79be142c2de8db7d63cdfab5e8d0 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Wed, 1 Apr 2026 21:58:56 -0700 Subject: [PATCH] [ell] validate spacing length --- masque/builder/utils.py | 11 ++++++++++- masque/test/test_builder.py | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/masque/builder/utils.py b/masque/builder/utils.py index b64cb85..4de6dbb 100644 --- a/masque/builder/utils.py +++ b/masque/builder/utils.py @@ -132,8 +132,17 @@ def ell( if spacing is None: ch_offsets = numpy.zeros_like(y_order) else: + spacing_arr = numpy.asarray(spacing, dtype=float).reshape(-1) steps = numpy.zeros_like(y_order) - steps[1:] = spacing + if spacing_arr.size == 1: + steps[1:] = spacing_arr[0] + elif spacing_arr.size == len(ports) - 1: + steps[1:] = spacing_arr + else: + raise BuildError( + f'spacing must be scalar or have length {len(ports) - 1} for {len(ports)} ports; ' + f'got length {spacing_arr.size}' + ) ch_offsets = numpy.cumsum(steps)[y_ind] x_start = rot_offsets[:, 0] diff --git a/masque/test/test_builder.py b/masque/test/test_builder.py index 0553b75..309dab6 100644 --- a/masque/test/test_builder.py +++ b/masque/test/test_builder.py @@ -1,7 +1,11 @@ +import numpy +import pytest from numpy.testing import assert_equal, assert_allclose from numpy import pi from ..builder import Builder +from ..builder.utils import ell +from ..error import BuildError from ..library import Library from ..pattern import Pattern from ..ports import Port @@ -131,6 +135,20 @@ def test_dead_plug_best_effort() -> None: assert_allclose(b.ports['B'].rotation, 0, atol=1e-10) +def test_ell_validates_spacing_length() -> None: + ports = { + 'A': Port((0, 0), 0), + 'B': Port((0, 1), 0), + 'C': Port((0, 2), 0), + } + + with pytest.raises(BuildError, match='spacing must be scalar or have length 2'): + ell(ports, True, 'min_extension', 5, spacing=[1, 2, 3]) + + with pytest.raises(BuildError, match='spacing must be scalar or have length 2'): + ell(ports, True, 'min_extension', 5, spacing=[]) + + def test_ell_handles_array_spacing_when_ccw_none() -> None: ports = { 'A': Port((0, 0), 0),