Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Parses module in more robust way for fn gathering
Browse files Browse the repository at this point in the history
inspect.getmodule(fn) depends on the state of sys.modules. This can
break if you're doing repeated parsing -- E.G. running it then wiping
sys.modules. Instead, we can just ask the function for its module. It's
simpler, and won't break.
  • Loading branch information
elijahbenizzy committed Jan 21, 2023
1 parent 8c48744 commit be4a461
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions hamilton/graph_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from typing import Callable, List, Tuple


def is_submodule(child: ModuleType, parent: ModuleType):
return parent.__name__ in child.__name__
def is_submodule(child: str, parent: str):
return parent in child


def find_functions(function_module: ModuleType) -> List[Tuple[str, Callable]]:
Expand All @@ -18,7 +18,7 @@ def valid_fn(fn):
return (
inspect.isfunction(fn)
and not fn.__name__.startswith("_")
and is_submodule(inspect.getmodule(fn), function_module)
and is_submodule(fn.__module__, function_module.__name__)
)

return [f for f in inspect.getmembers(function_module, predicate=valid_fn)]
2 changes: 1 addition & 1 deletion hamilton/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def from_fn(fn: Callable, name: str = None) -> "Node":
if name is None:
name = fn.__name__
sig = inspect.signature(fn)
module = inspect.getmodule(fn).__name__
module = fn.__module__
return Node(
name,
sig.return_annotation,
Expand Down

0 comments on commit be4a461

Please sign in to comment.