Skip to content

Commit

Permalink
pylint improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Nov 26, 2023
1 parent 2a747b3 commit 273277c
Showing 1 changed file with 50 additions and 41 deletions.
91 changes: 50 additions & 41 deletions src/build123d/build_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from abc import ABC, abstractmethod
from itertools import product
from math import sqrt
from typing import Any, Callable, Iterable, Optional, Union, TypeVar
from typing import Callable, Iterable, Optional, Union, TypeVar
from typing_extensions import Self, ParamSpec, Concatenate

from build123d.build_enums import Align, Mode, Select
Expand All @@ -70,6 +70,8 @@
new_edges,
)

# pylint: disable=too-many-lines

# Create a build123d logger to distinguish these logs from application logs.
# If the user doesn't configure logging, all build123d logs will be discarded.
logging.getLogger("build123d").addHandler(logging.NullHandler())
Expand Down Expand Up @@ -140,6 +142,8 @@ class Builder(ABC):
"""

# pylint: disable=too-many-instance-attributes

# Context variable used to by Objects and Operations to link to current builder instance
_current: contextvars.ContextVar["Builder"] = contextvars.ContextVar(
"Builder._current"
Expand Down Expand Up @@ -301,6 +305,10 @@ def _add_to_context(
ValueError: Nothing to intersect with
ValueError: Nothing to intersect with
"""
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements

self.obj_before = self._obj
self.to_combine = list(objects)
if mode != Mode.PRIVATE and len(objects) > 0:
Expand All @@ -310,7 +318,7 @@ def _add_to_context(
typed[cls] = [obj for obj in objects if isinstance(obj, cls)]

# Check for invalid inputs
num_stored = sum([len(t) for t in typed.values()])
num_stored = sum(len(t) for t in typed.values())
# Generate an exception if not processing exceptions
if len(objects) != num_stored and not sys.exc_info()[1]:
unsupported = set(objects) - set(v for l in typed.values() for v in l)
Expand All @@ -325,31 +333,31 @@ def _add_to_context(
# Align sketch planar faces with Plane.XY
if self._tag == "BuildSketch":
aligned = []
face: Face
for face in typed[Face]:
if not face.is_coplanar(Plane.XY):
new_face: Face
for new_face in typed[Face]:
if not new_face.is_coplanar(Plane.XY):
# Try to keep the x direction, if not allow it to be assigned automatically
try:
plane = Plane(
origin=(0, 0, 0),
x_dir=(1, 0, 0),
z_dir=face.normal_at(),
z_dir=new_face.normal_at(),
)
except:
plane = Plane(origin=(0, 0, 0), z_dir=face.normal_at())
plane = Plane(origin=(0, 0, 0), z_dir=new_face.normal_at())

face = plane.to_local_coords(face)
face.move(Location((0, 0, -face.center().Z)))
if face.normal_at().Z > 0: # Flip the face if up-side-down
aligned.append(face)
new_face = plane.to_local_coords(new_face)
new_face.move(Location((0, 0, -new_face.center().Z)))
if new_face.normal_at().Z > 0: # Flip the face if up-side-down
aligned.append(new_face)
else:
aligned.append(-face)
aligned.append(-new_face)
typed[Face] = aligned

# Convert wires to edges
wire: Wire
for wire in typed[Wire]:
typed[Edge].extend(wire.edges())
new_wire: Wire
for new_wire in typed[Wire]:
typed[Edge].extend(new_wire.edges())

# Allow faces to be combined with solids for section operations
if not faces_to_pending:
Expand Down Expand Up @@ -426,9 +434,7 @@ def _add_to_context(
if self._tag == "BuildPart":
self._add_to_pending(*typed[Edge])
for plane in WorkplaneList._get_context().workplanes:
global_faces = [
plane.from_local_coords(face) for face in typed[Face]
]
global_faces = [plane.from_local_coords(f) for f in typed[Face]]
self._add_to_pending(*global_faces, face_plane=plane)
elif self._tag == "BuildSketch":
self._add_to_pending(*typed[Edge])
Expand All @@ -448,8 +454,8 @@ def vertices(self, select: Select = Select.ALL) -> ShapeList[Vertex]:
"""
vertex_list: list[Vertex] = []
if select == Select.ALL:
for edge in self._obj.edges():
vertex_list.extend(edge.vertices())
for obj_edge in self._obj.edges():
vertex_list.extend(obj_edge.vertices())
elif select == Select.LAST:
vertex_list = self.lasts[Vertex]
elif select == Select.NEW:
Expand All @@ -471,11 +477,11 @@ def vertex(self, select: Select = Select.ALL) -> Vertex:
Returns:
Vertex: Vertex extracted
"""
vertices = self.vertices(select)
vertex_count = len(vertices)
all_vertices = self.vertices(select)
vertex_count = len(all_vertices)
if vertex_count != 1:
warnings.warn(f"Found {vertex_count} vertices, returning first")
return vertices[0]
return all_vertices[0]

def edges(self, select: Select = Select.ALL) -> ShapeList[Edge]:
"""Return Edges
Expand Down Expand Up @@ -511,11 +517,11 @@ def edge(self, select: Select = Select.ALL) -> Edge:
Returns:
Edge: Edge extracted
"""
edges = self.edges(select)
edge_count = len(edges)
all_edges = self.edges(select)
edge_count = len(all_edges)
if edge_count != 1:
warnings.warn(f"Found {edge_count} edges, returning first")
return edges[0]
return all_edges[0]

def wires(self, select: Select = Select.ALL) -> ShapeList[Wire]:
"""Return Wires
Expand Down Expand Up @@ -551,11 +557,11 @@ def wire(self, select: Select = Select.ALL) -> Wire:
Returns:
Wire: Wire extracted
"""
wires = self.wires(select)
wire_count = len(wires)
all_wires = self.wires(select)
wire_count = len(all_wires)
if wire_count != 1:
warnings.warn(f"Found {wire_count} wires, returning first")
return wires[0]
return all_wires[0]

def faces(self, select: Select = Select.ALL) -> ShapeList[Face]:
"""Return Faces
Expand Down Expand Up @@ -591,11 +597,11 @@ def face(self, select: Select = Select.ALL) -> Face:
Returns:
Face: Face extracted
"""
faces = self.faces(select)
face_count = len(faces)
all_faces = self.faces(select)
face_count = len(all_faces)
if face_count != 1:
warnings.warn(f"Found {face_count} faces, returning first")
return faces[0]
return all_faces[0]

def solids(self, select: Select = Select.ALL) -> ShapeList[Solid]:
"""Return Solids
Expand Down Expand Up @@ -631,11 +637,11 @@ def solid(self, select: Select = Select.ALL) -> Solid:
Returns:
Solid: Solid extracted
"""
solids = self.solids(select)
solid_count = len(solids)
all_solids = self.solids(select)
solid_count = len(all_solids)
if solid_count != 1:
warnings.warn(f"Found {solid_count} solids, returning first")
return solids[0]
return all_solids[0]

def _shapes(self, obj_type: Union[Vertex, Edge, Face, Solid] = None) -> ShapeList:
"""Extract Shapes"""
Expand Down Expand Up @@ -819,6 +825,8 @@ def __init__(
y_count: int,
align: Union[Align, tuple[Align, Align]] = (Align.CENTER, Align.CENTER),
):
# pylint: disable=too-many-locals

diagonal = 4 * apothem / sqrt(3)
x_spacing = 3 * diagonal / 4
y_spacing = diagonal * sqrt(3) / 2
Expand Down Expand Up @@ -1020,6 +1028,8 @@ class GridLocations(LocationList):
ValueError: Either x or y count must be greater than or equal to one.
"""

# pylint: disable=too-many-instance-attributes

def __init__(
self,
x_spacing: float,
Expand Down Expand Up @@ -1181,16 +1191,16 @@ def localize(cls, *points: VectorLike) -> Union[list[Vector], Vector]:


def __gen_context_component_getter(
f: Callable[Concatenate[Builder, P], T]
func: Callable[Concatenate[Builder, P], T]
) -> Callable[P, T]:
@functools.wraps(f)
@functools.wraps(func)
def getter(select: Select = Select.ALL):
context = Builder._get_context(f.__name__)
context = Builder._get_context(func.__name__)
if not context:
raise RuntimeError(
f"{f.__name__}() requires a Builder context to be in scope"
f"{func.__name__}() requires a Builder context to be in scope"
)
return f(context, select)
return func(context, select)

return getter

Expand Down Expand Up @@ -1221,7 +1231,6 @@ def wrapper(self: Vector, vec: VectorLike):
vec = WorkplaneList.localize(vec) - origin # type: ignore[union-attr]
except AttributeError:
# raised from `WorkplaneList._get_context().workplanes[0]` when context is `None`
# TODO make a specific `NoContextError` and raise that from `_get_context()` ?
pass
return original_op(self, vec)

Expand Down

0 comments on commit 273277c

Please sign in to comment.