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

UI fix code display for temporary modules #860

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions hamilton/ad_hoc_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import types
import uuid
from types import ModuleType
from typing import Callable
from typing import Callable, Optional


def _copy_func(f):
Expand Down Expand Up @@ -60,9 +60,9 @@ def create_temporary_module(*functions: Callable, module_name: str = None) -> Mo
return module


def module_from_source(source: str) -> ModuleType:
def module_from_source(source: str, module_name: Optional[str] = None) -> ModuleType:
"""Create a temporary module from source code"""
module_name = _generate_unique_temp_module_name()
module_name = module_name if module_name else _generate_unique_temp_module_name()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This probably should be using a hash of the source. E.G. so we don't repeat it a ton of times. Not sttrictly necessary but feels good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's best to have module_name as a required argument. However, didn't want to make that change now to limit the scope of this PR

module_object = ModuleType(module_name)
code_object = compile(source, module_name, "exec")
sys.modules[module_name] = module_object
Expand Down
36 changes: 27 additions & 9 deletions ui/sdk/src/hamilton_sdk/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import hashlib
import inspect
import json
import linecache
import logging
import operator
import os
Expand Down Expand Up @@ -67,15 +68,19 @@ def _hash_module(
f"attribute or it is None. This happens with lazy loaders."
)
continue
# Check if the module is in the same top level package
if value.__package__ != module.__package__ and not value.__package__.startswith(
module.__package__
):
logger.debug(
f"Skipping hash for module {value.__name__} because it is in a different "
f"package {value.__package__} than {module.__package__}"
)
continue

# Modules imported in a temporary module have no `__package__` attribute
if module.__package__:
# Check if the module is in the same top level package
if value.__package__ != module.__package__ and not value.__package__.startswith(
module.__package__
):
logger.debug(
f"Skipping hash for module {value.__name__} because it is in a different "
f"package {value.__package__} than {module.__package__}"
)
continue

# Recursively hash the sub-module
hash_object = _hash_module(value, hash_object, seen_modules)

Expand Down Expand Up @@ -688,6 +693,11 @@ def extract_task_updates_from_tracking_state(


def _slurp_code(fg: graph.FunctionGraph, repo_base: str) -> List[dict]:
"""Get the source code from modules. Returns a list with a dictionary for each module.

The `path` attribute needs to match the `path` of code artifacts generated by
`extract_code_artifacts_from_function_graph()`
"""
modules = set()
for node_ in fg.nodes.values():
originating_functions = node_.originating_functions
Expand All @@ -702,6 +712,14 @@ def _slurp_code(fg: graph.FunctionGraph, repo_base: str) -> List[dict]:
module_path = os.path.relpath(module.__file__, repo_base)
with open(module.__file__, "r") as f:
out.append({"path": module_path, "contents": f.read()})
# for temporary modules registed via `module_from_source`
else:
# get source code from the linecache; returns a tuple (size, mtime, lines, fullname)
source_lines = linecache.cache[module.__name__][2]
source = "".join(source_lines)
# the path won't have a `.py` suffix to match `extract_code_artifacts_from_function_grap()`
module_path = os.path.relpath(module.__name__, repo_base)
out.append({"path": module_path, "contents": source})
return out


Expand Down