Skip to content

Commit

Permalink
Replace Optional with | None
Browse files Browse the repository at this point in the history
  • Loading branch information
gumyr committed Dec 17, 2024
1 parent 127d048 commit 5b88e87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/build123d/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,18 +468,18 @@ def cast(cls, obj: TopoDS_Shape) -> Self:
@overload
def split(
self, tool: TrimmingTool, keep: Literal[Keep.TOP, Keep.BOTTOM]
) -> Optional[Self] | Optional[list[Self]]:
) -> Self | list[Self] | None:
"""split and keep inside or outside"""

@overload
def split(self, tool: TrimmingTool, keep: Literal[Keep.BOTH]) -> tuple[
Optional[Self] | Optional[list[Self]],
Optional[Self] | Optional[list[Self]],
Self | list[Self] | None,
Self | list[Self] | None,
]:
"""split and keep inside and outside"""

@overload
def split(self, tool: TrimmingTool) -> Optional[Self] | Optional[list[Self]]:
def split(self, tool: TrimmingTool) -> Self | list[Self] | None:
"""split and keep inside (default)"""

def split(self, tool: TrimmingTool, keep: Keep = Keep.TOP):
Expand All @@ -494,8 +494,8 @@ def split(self, tool: TrimmingTool, keep: Keep = Keep.TOP):
Returns:
Shape: result of split
Returns:
Optional[Self] | Optional[list[Self]],
Tuple[Optional[Self] | Optional[list[Self]]]: The result of the split operation.
Self | list[Self] | None,
Tuple[Self | list[Self] | None]: The result of the split operation.
- **Keep.TOP**: Returns the top as a `Self` or `list[Self]`, or `None`
if no top is found.
Expand Down Expand Up @@ -3204,22 +3204,22 @@ def faces_intersected_by_axis(
@overload
def split_by_perimeter(
self, perimeter: Union[Edge, Wire], keep: Literal[Keep.INSIDE, Keep.OUTSIDE]
) -> Optional[Face] | Optional[Shell] | Optional[ShapeList[Face]]:
) -> Face | Shell | ShapeList[Face] | None:
"""split_by_perimeter and keep inside or outside"""

@overload
def split_by_perimeter(
self, perimeter: Union[Edge, Wire], keep: Literal[Keep.BOTH]
) -> tuple[
Optional[Face] | Optional[Shell] | Optional[ShapeList[Face]],
Optional[Face] | Optional[Shell] | Optional[ShapeList[Face]],
Face | Shell | ShapeList[Face] | None,
Face | Shell | ShapeList[Face] | None,
]:
"""split_by_perimeter and keep inside and outside"""

@overload
def split_by_perimeter(
self, perimeter: Union[Edge, Wire]
) -> Optional[Face] | Optional[Shell] | Optional[ShapeList[Face]]:
) -> Face | Shell | ShapeList[Face] | None:
"""split_by_perimeter and keep inside (default)"""

def split_by_perimeter(
Expand All @@ -3241,8 +3241,8 @@ def split_by_perimeter(
ValueError: keep must be one of Keep.INSIDE|OUTSIDE|BOTH
Returns:
Union[Optional[Shell], Optional[Face],
Tuple[Optional[Shell], Optional[Face]]]: The result of the split operation.
Union[Face | Shell | ShapeList[Face] | None,
Tuple[Face | Shell | ShapeList[Face] | None]: The result of the split operation.
- **Keep.INSIDE**: Returns the inside part as a `Shell` or `Face`, or `None`
if no inside part is found.
Expand Down
19 changes: 19 additions & 0 deletions tools/refactor_topo.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,24 @@ def leave_Annotation(
return updated_node


class OptionalToPipeTransformer(cst.CSTTransformer):
def leave_Annotation(
self, original_node: cst.Annotation, updated_node: cst.Annotation
) -> cst.Annotation:
# Match Optional[...] annotations
if m.matches(updated_node.annotation, m.Subscript(value=m.Name("Optional"))):
subscript = updated_node.annotation
if isinstance(subscript, cst.Subscript) and subscript.slice:
# Extract the inner type of Optional
inner_type = subscript.slice[0].slice.value
# Replace Optional[X] with X | None
new_annotation = cst.BinaryOperation(
left=inner_type, operator=cst.BitOr(), right=cst.Name("None")
)
return updated_node.with_changes(annotation=new_annotation)
return updated_node


def main():
# Define paths
script_dir = Path(__file__).parent
Expand Down Expand Up @@ -765,6 +783,7 @@ def main():
# Parse source file and collect imports
source_tree = cst.parse_module(topo_file.read_text())
source_tree = source_tree.visit(UnionToPipeTransformer())
source_tree = source_tree.visit(OptionalToPipeTransformer())
# transformed_module = source_tree.visit(UnionToPipeTransformer())
# print(transformed_module.code)

Expand Down

0 comments on commit 5b88e87

Please sign in to comment.