From 5b88e87bad69f26b09c480a55b81c4547214a338 Mon Sep 17 00:00:00 2001 From: gumyr Date: Tue, 17 Dec 2024 11:58:58 -0500 Subject: [PATCH] Replace Optional with | None --- src/build123d/topology.py | 24 ++++++++++++------------ tools/refactor_topo.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/build123d/topology.py b/src/build123d/topology.py index b75fe5e8..b218cb06 100644 --- a/src/build123d/topology.py +++ b/src/build123d/topology.py @@ -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): @@ -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. @@ -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( @@ -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. diff --git a/tools/refactor_topo.py b/tools/refactor_topo.py index 4adbc0bb..6fa5c274 100644 --- a/tools/refactor_topo.py +++ b/tools/refactor_topo.py @@ -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 @@ -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)