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 12, 2023
1 parent 0469db3 commit e534c5b
Showing 1 changed file with 3 additions and 3 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)]

0 comments on commit e534c5b

Please sign in to comment.