Validate bend geometry options at construction

This commit is contained in:
Jan Petykiewicz 2026-11-29 23:53:39 -08:00
commit f762b86429
2 changed files with 24 additions and 3 deletions

View file

@ -128,6 +128,18 @@ class SearchOptions(_SearchOptionsCache):
_require_nonnegative("distance_heuristic_weight", self.distance_heuristic_weight)
if not isfinite(self.proximity_range) or self.proximity_range <= 0:
raise ValueError("proximity_range must be finite and positive")
proxy_names = ("arc", "bbox", "clipped_bbox")
for field_name, names, optional in (
("bend_collision_type", proxy_names, False),
("bend_proxy_geometry", proxy_names, True),
("bend_physical_geometry", ("arc",), True),
):
geometry = getattr(self, field_name)
if isinstance(geometry, Polygon):
if geometry.is_empty or not geometry.is_valid:
raise ValueError(f"{field_name} must be a valid, nonempty polygon")
elif not ((optional and geometry is None) or (isinstance(geometry, str) and geometry in names)):
raise ValueError(f"Invalid {field_name} {geometry!r}")
if self.bend_physical_length is not None:
_, physical = resolve_bend_geometry(self)
if physical == "arc" or not isfinite(self.bend_physical_length) or self.bend_physical_length <= 0:
@ -159,9 +171,6 @@ class SearchOptions(_SearchOptionsCache):
raise ValueError(f"Duplicate S-bend outline for offset {outline.offset}, radius {outline.radius}, width {outline.width}")
widths.append(outline.width)
object.__setattr__(self, "sbend_outlines", outlines)
for geometry in (self.bend_collision_type, self.bend_proxy_geometry, self.bend_physical_geometry):
if isinstance(geometry, Polygon) and (geometry.is_empty or not geometry.is_valid):
raise ValueError("Custom bend geometry must be a valid, nonempty polygon")
if self.bend_physical_geometry is None and isinstance(self.bend_proxy_geometry, Polygon):
warnings.warn(
"Custom bend proxy provided without bend_physical_geometry; routed bends will keep standard arc geometry.",

View file

@ -451,6 +451,18 @@ def test_public_inputs_reject_invalid_ids_and_named_options() -> None:
SearchOptions(visibility_guidance="exact")
@pytest.mark.parametrize(("name", "value"), [
("bend_collision_type", "clipped-bbox"),
("bend_collision_type", None),
("bend_proxy_geometry", "clipped-bbox"),
("bend_proxy_geometry", 1),
("bend_physical_geometry", "bbox"),
])
def test_search_rejects_unsupported_bend_geometry(name, value) -> None:
with pytest.raises(ValueError, match=f"Invalid {name}"):
SearchOptions(**{name: value})
def _sbend_strip(offset, half_width=1):
centerline = [(0, 0), (20, offset / 10), (50, 3 * offset / 4), (80, offset)]
return Polygon(