Skip to content

Commit 88f6b69

Browse files
committed
Fixed operations_sketch.py typing problems
1 parent 475bf42 commit 88f6b69

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

src/build123d/operations_sketch.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
"""
2929

3030
from __future__ import annotations
31-
from typing import Union
3231

3332
from collections.abc import Iterable
33+
from scipy.spatial import Voronoi
3434
from build123d.build_enums import Mode, SortBy
3535
from build123d.topology import (
3636
Compound,
@@ -46,7 +46,6 @@
4646
from build123d.geometry import Vector, TOLERANCE
4747
from build123d.build_common import flatten_sequence, validate_inputs
4848
from build123d.build_sketch import BuildSketch
49-
from scipy.spatial import Voronoi
5049

5150

5251
def full_round(
@@ -77,7 +76,7 @@ def full_round(
7776
geometric center of the arc, and the third the radius of the arc
7877
7978
"""
80-
context: BuildSketch = BuildSketch._get_context("full_round")
79+
context: BuildSketch | None = BuildSketch._get_context("full_round")
8180

8281
if not isinstance(edge, Edge):
8382
raise ValueError("A single Edge must be provided")
@@ -108,7 +107,11 @@ def full_round(
108107
# Refine the largest empty circle center estimate by averaging the best
109108
# three candidates. The minimum distance between the edges and this
110109
# center is the circle radius.
111-
best_three = [(float("inf"), None), (float("inf"), None), (float("inf"), None)]
110+
best_three: list[tuple[float, int]] = [
111+
(float("inf"), int()),
112+
(float("inf"), int()),
113+
(float("inf"), int()),
114+
]
112115

113116
for i, v in enumerate(voronoi_vertices):
114117
distances = [edge_group[i].distance_to(v) for i in range(3)]
@@ -125,7 +128,9 @@ def full_round(
125128

126129
# Extract the indices of the best three and average them
127130
best_indices = [x[1] for x in best_three]
128-
voronoi_circle_center = sum(voronoi_vertices[i] for i in best_indices) / 3
131+
voronoi_circle_center: Vector = (
132+
sum((voronoi_vertices[i] for i in best_indices), Vector(0, 0, 0)) / 3.0
133+
)
129134

130135
# Determine where the connected edges intersect with the largest empty circle
131136
connected_edges_end_points = [
@@ -142,7 +147,7 @@ def full_round(
142147
for i, e in enumerate(connected_edges)
143148
]
144149
for param in connected_edges_end_params:
145-
if not (0.0 < param < 1.0):
150+
if not 0.0 < param < 1.0:
146151
raise ValueError("Invalid geometry to create the end arc")
147152

148153
common_vertex_points = [
@@ -177,7 +182,14 @@ def full_round(
177182
)
178183

179184
# Recover other edges
180-
other_edges = edge.topo_parent.edges() - topo_explore_connected_edges(edge) - [edge]
185+
if edge.topo_parent is None:
186+
other_edges: ShapeList[Edge] = ShapeList()
187+
else:
188+
other_edges = (
189+
edge.topo_parent.edges()
190+
- topo_explore_connected_edges(edge)
191+
- ShapeList([edge])
192+
)
181193

182194
# Rebuild the face
183195
# Note that the longest wire must be the perimeter and others holes
@@ -195,7 +207,7 @@ def full_round(
195207

196208

197209
def make_face(
198-
edges: Edge | Iterable[Edge] = None, mode: Mode = Mode.ADD
210+
edges: Edge | Iterable[Edge] | None = None, mode: Mode = Mode.ADD
199211
) -> Sketch:
200212
"""Sketch Operation: make_face
201213
@@ -206,7 +218,7 @@ def make_face(
206218
sketch pending edges.
207219
mode (Mode, optional): combination mode. Defaults to Mode.ADD.
208220
"""
209-
context: BuildSketch = BuildSketch._get_context("make_face")
221+
context: BuildSketch | None = BuildSketch._get_context("make_face")
210222

211223
if edges is not None:
212224
outer_edges = flatten_sequence(edges)
@@ -230,7 +242,7 @@ def make_face(
230242

231243

232244
def make_hull(
233-
edges: Edge | Iterable[Edge] = None, mode: Mode = Mode.ADD
245+
edges: Edge | Iterable[Edge] | None = None, mode: Mode = Mode.ADD
234246
) -> Sketch:
235247
"""Sketch Operation: make_hull
236248
@@ -241,7 +253,7 @@ def make_hull(
241253
sketch pending edges.
242254
mode (Mode, optional): combination mode. Defaults to Mode.ADD.
243255
"""
244-
context: BuildSketch = BuildSketch._get_context("make_hull")
256+
context: BuildSketch | None = BuildSketch._get_context("make_hull")
245257

246258
if edges is not None:
247259
hull_edges = flatten_sequence(edges)
@@ -268,7 +280,7 @@ def make_hull(
268280

269281

270282
def trace(
271-
lines: Curve | Edge | Wire | Iterable[Curve | Edge | Wire] = None,
283+
lines: Curve | Edge | Wire | Iterable[Curve | Edge | Wire] | None = None,
272284
line_width: float = 1,
273285
mode: Mode = Mode.ADD,
274286
) -> Sketch:
@@ -277,7 +289,7 @@ def trace(
277289
Convert edges, wires or pending edges into faces by sweeping a perpendicular line along them.
278290
279291
Args:
280-
lines (Union[Curve, Edge, Wire, Iterable[Union[Curve, Edge, Wire]]], optional): lines to
292+
lines (Curve | Edge | Wire | Iterable[Curve | Edge | Wire]], optional): lines to
281293
trace. Defaults to sketch pending edges.
282294
line_width (float, optional): Defaults to 1.
283295
mode (Mode, optional): combination mode. Defaults to Mode.ADD.
@@ -288,7 +300,7 @@ def trace(
288300
Returns:
289301
Sketch: Traced lines
290302
"""
291-
context: BuildSketch = BuildSketch._get_context("trace")
303+
context: BuildSketch | None = BuildSketch._get_context("trace")
292304

293305
if lines is not None:
294306
trace_lines = flatten_sequence(lines)
@@ -298,14 +310,15 @@ def trace(
298310
else:
299311
raise ValueError("No objects to trace")
300312

301-
new_faces = []
313+
new_faces: list[Face] = []
302314
for edge in trace_edges:
303315
trace_pen = edge.perpendicular_line(line_width, 0)
304316
new_faces.extend(Face.sweep(trace_pen, edge).faces())
305317
if context is not None:
306318
context._add_to_context(*new_faces, mode=mode)
307319
context.pending_edges = ShapeList()
308320

321+
# pylint: disable=no-value-for-parameter
309322
combined_faces = Face.fuse(*new_faces) if len(new_faces) > 1 else new_faces[0]
310323
result = (
311324
Sketch(combined_faces)

0 commit comments

Comments
 (0)