Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build with Local Function Directly #793

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions hamilton/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,17 @@ def with_config(self, config: Dict[str, Any]) -> "Builder":
self.config.update(config)
return self

def with_local_modules(self) -> "Builder":
"""Adds the local modules to the modules list.
Copy link
Collaborator

@skrawcz skrawcz Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add more caveats here.
E.g. If people use this, they could run into problems when using some of the adapters because this code will be imported under the module __main__.


:return: self
"""
import inspect

module = inspect.getmodule(inspect.stack()[1][0])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See Thierry's comment. I think we can make this more robust.

self.modules.append(module)
return self

def with_modules(self, *modules: ModuleType) -> "Builder":
"""Adds the specified modules to the modules list.
This can be called multiple times -- later calls will take precedence.
Expand Down
7 changes: 7 additions & 0 deletions tests/test_driver_local_modules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from hamilton.driver import Builder, DefaultGraphExecutor


def test_driver_with_local_modules() -> None:
dr = Builder().with_local_modules().build()
assert isinstance(dr.graph_executor, DefaultGraphExecutor)
assert __name__ == list(dr.graph_modules)[0].__name__
Comment on lines +3 to +7
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test probably needs to pull in a function and exercise the driver.

e.g. define

def a(b:int) -> int:
   return b * 2

Then we should execute the driver to get "a".

That should work right?