Skip to content

Commit

Permalink
[patch] Extend automatic macro and function node documentation (#447)
Browse files Browse the repository at this point in the history
* [patch] Extend automatic macro and function node documentation

To scrape not just the docstring, but the signature and source code as well

* Format black

---------

Co-authored-by: pyiron-runner <[email protected]>
  • Loading branch information
liamhuber and pyiron-runner authored Sep 7, 2024
1 parent 4b7b224 commit 2a53c16
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
32 changes: 32 additions & 0 deletions pyiron_workflow/mixin/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,38 @@ def _validate_return_count(cls):
f"present, " + error_suffix
)

@staticmethod
def _io_defining_documentation(io_defining_function: callable, title: str):
"""
A helper method for building a docstring for classes that have their IO defined
by some function.
"""
try:
signature = str(inspect.signature(io_defining_function))
except Exception as e:
signature = f"SIGNATURE NOT AVAILABLE -- {type(e).__name__}: {e}"

try:
source = inspect.getsource(io_defining_function)
except Exception as e:
source = f"SOURCE NOT AVAILABLE -- {type(e).__name__}: {e}"

doc = (
"" if io_defining_function.__doc__ is None else io_defining_function.__doc__
)

docs = f"{title.upper()} INFO:\n\n"
docs += "Signature:\n\n"
docs += signature
docs += "\n\n"
docs += "Docstring:\n\n"
docs += doc
docs += "\n"
docs += "Source:\n\n"
docs += source
docs += "\n"
return docs


def no_output_validation_warning(cls: type):
return (
Expand Down
4 changes: 3 additions & 1 deletion pyiron_workflow/nodes/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ def function_node_factory(
"__qualname__": node_function.__qualname__,
"_output_labels": None if len(output_labels) == 0 else output_labels,
"_validate_output_labels": validate_output_labels,
"__doc__": node_function.__doc__,
"__doc__": Function._io_defining_documentation(
node_function, "node_function"
),
"use_cache": use_cache,
},
{},
Expand Down
2 changes: 1 addition & 1 deletion pyiron_workflow/nodes/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def macro_node_factory(
"__qualname__": graph_creator.__qualname__,
"_output_labels": None if len(output_labels) == 0 else output_labels,
"_validate_output_labels": validate_output_labels,
"__doc__": graph_creator.__doc__,
"__doc__": Macro._io_defining_documentation(graph_creator, "graph_creator"),
"use_cache": use_cache,
},
{},
Expand Down

0 comments on commit 2a53c16

Please sign in to comment.