From b77210d16f11a0df4270f46ae518cefe85845eae Mon Sep 17 00:00:00 2001 From: liamhuber Date: Tue, 21 Jan 2025 09:37:33 -0800 Subject: [PATCH] Make Macro.graph_creator a normal method With accompanying change to applications/invocations, type hint narrowing, and docstrings Signed-off-by: liamhuber --- pyiron_workflow/nodes/macro.py | 12 ++++++------ tests/unit/nodes/test_macro.py | 3 +-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/pyiron_workflow/nodes/macro.py b/pyiron_workflow/nodes/macro.py index f556301c..5cb0d13e 100644 --- a/pyiron_workflow/nodes/macro.py +++ b/pyiron_workflow/nodes/macro.py @@ -13,6 +13,7 @@ from pyiron_snippets.factory import classfactory +from pyiron_workflow.compatibility import Self from pyiron_workflow.io import Inputs from pyiron_workflow.mixin.has_interface_mixins import HasChannel from pyiron_workflow.mixin.injection import OutputsWithInjection @@ -196,7 +197,6 @@ class Macro(Composite, StaticNode, ScrapesIO, ABC): >>> class AddThreeMacro(Macro): ... _output_labels = ["three"] ... - ... @staticmethod ... def graph_creator(self, x): ... add_three_macro(self, one__x=x) ... return self.three @@ -252,7 +252,7 @@ def _setup_node(self) -> None: super()._setup_node() ui_nodes = self._prepopulate_ui_nodes_from_graph_creator_signature() - returned_has_channel_objects = self.graph_creator(self, *ui_nodes) + returned_has_channel_objects = self.graph_creator(*ui_nodes) if returned_has_channel_objects is None: returned_has_channel_objects = () elif isinstance(returned_has_channel_objects, HasChannel): @@ -271,10 +271,9 @@ def _setup_node(self) -> None: remaining_ui_nodes = self._purge_single_use_ui_nodes(ui_nodes) self._configure_graph_execution(remaining_ui_nodes) - @staticmethod @abstractmethod def graph_creator( - self: Macro, *args, **kwargs # noqa: PLW0211 + self: Self, *args, **kwargs ) -> HasChannel | tuple[HasChannel, ...] | None: """Build the graph the node will run.""" @@ -480,7 +479,8 @@ def macro_node_factory( Create a new :class:`Macro` subclass using the given graph creator function. Args: - graph_creator (callable): Function to create the graph for the :class:`Macro`. + graph_creator (callable): Function to create the graph for this subclass of + :class:`Macro`. validate_output_labels (bool): Whether to validate the output labels against the return values of the wrapped function. use_cache (bool): Whether nodes of this type should default to caching their @@ -495,7 +495,7 @@ def macro_node_factory( graph_creator.__name__, (Macro,), # Define parentage { - "graph_creator": staticmethod(graph_creator), + "graph_creator": graph_creator, "__module__": graph_creator.__module__, "__qualname__": graph_creator.__qualname__, "_output_labels": None if len(output_labels) == 0 else output_labels, diff --git a/tests/unit/nodes/test_macro.py b/tests/unit/nodes/test_macro.py index b88ff753..401db81a 100644 --- a/tests/unit/nodes/test_macro.py +++ b/tests/unit/nodes/test_macro.py @@ -172,8 +172,7 @@ def test_creation_from_subclass(self): class MyMacro(Macro): _output_labels = ("three__result",) - @staticmethod - def graph_creator(self, one__x): # noqa: PLW0211 + def graph_creator(self, one__x): add_three_macro(self, one__x) return self.three