-
Notifications
You must be signed in to change notification settings - Fork 158
Updates from_functions and from_modules to use the same base "compile" #1275
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
Open
elijahbenizzy
wants to merge
1
commit into
refactor/build-from-functions
Choose a base branch
from
build-from-function-changes
base: refactor/build-from-functions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
import pathlib | ||
import uuid | ||
from enum import Enum | ||
from types import ModuleType | ||
from types import FunctionType, ModuleType | ||
from typing import Any, Callable, Collection, Dict, FrozenSet, List, Optional, Set, Tuple, Type | ||
|
||
import hamilton.lifecycle.base as lifecycle_base | ||
|
@@ -142,17 +142,18 @@ def update_dependencies( | |
return nodes | ||
|
||
|
||
def create_function_graph( | ||
def compile_to_nodes( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new parameter 'allow_node_level_overrides' and corresponding error message look good. Consider clarifying in the docstring that this option controls node‐level duplicate detection, as opposed to module overrides. |
||
*functions: List[Tuple[str, Callable]], | ||
config: Dict[str, Any], | ||
adapter: lifecycle_base.LifecycleAdapterSet = None, | ||
fg: Optional["FunctionGraph"] = None, | ||
allow_module_overrides: bool = False, | ||
allow_node_level_overrides: bool = False, | ||
) -> Dict[str, node.Node]: | ||
"""Creates a graph of all available functions & their dependencies. | ||
:param modules: A set of modules over which one wants to compute the function graph | ||
:param config: Dictionary that we will inspect to get values from in building the function graph. | ||
:param adapter: The adapter that adapts our node type checking based on the context. | ||
:param allow_node_level_overrides: Whether or not to allow node names to override each other | ||
:return: list of nodes in the graph. | ||
If it needs to be more complicated, we'll return an actual networkx graph and get all the rest of the logic for free | ||
""" | ||
|
@@ -170,7 +171,7 @@ def create_function_graph( | |
for n in fm_base.resolve_nodes(f, config): | ||
if n.name in config: | ||
continue # This makes sure we overwrite things if they're in the config... | ||
if n.name in nodes and not allow_module_overrides: | ||
if n.name in nodes and not allow_node_level_overrides: | ||
raise ValueError( | ||
f"Cannot define function {n.name} more than once." | ||
f" Already defined by function {f}" | ||
|
@@ -713,13 +714,42 @@ def __init__( | |
self.nodes = nodes | ||
self.adapter = adapter | ||
|
||
@staticmethod | ||
def compile( | ||
modules: List[ModuleType], | ||
functions: List[FunctionType], | ||
config: Dict[str, Any], | ||
adapter: lifecycle_base.LifecycleAdapterSet = None, | ||
allow_node_overrides: bool = False, | ||
) -> "FunctionGraph": | ||
"""Base level static function for compiling a function graph. Note | ||
that this can both use functions (E.G. passing them directly) and modules | ||
(passing them in and crawling. | ||
|
||
:param modules: Modules to use | ||
:param functions: Functions to use | ||
:param config: Config to use for setting up the DAG | ||
:param adapter: Adapter to use for node resolution | ||
:param allow_node_overrides: Whether or not to allow node level overrides. | ||
:return: The compiled function graph | ||
""" | ||
module_functions = sum([find_functions(module) for module in modules], []) | ||
nodes = compile_to_nodes( | ||
*module_functions, | ||
*functions, | ||
config=config, | ||
adapter=adapter, | ||
allow_node_level_overrides=allow_node_overrides, | ||
) | ||
return FunctionGraph(nodes, config, adapter) | ||
|
||
@staticmethod | ||
def from_modules( | ||
*modules: ModuleType, | ||
config: Dict[str, Any], | ||
adapter: lifecycle_base.LifecycleAdapterSet = None, | ||
allow_module_overrides: bool = False, | ||
): | ||
) -> "FunctionGraph": | ||
"""Initializes a function graph from the specified modules. Note that this was the old | ||
way we constructed FunctionGraph -- this is not a public-facing API, so we replaced it | ||
with a constructor that takes in nodes directly. If you hacked in something using | ||
|
@@ -732,28 +762,28 @@ def from_modules( | |
:return: a function graph. | ||
""" | ||
|
||
functions = sum([find_functions(module) for module in modules], []) | ||
return FunctionGraph.from_functions( | ||
*functions, | ||
return FunctionGraph.compile( | ||
modules=modules, | ||
functions=[], | ||
config=config, | ||
adapter=adapter, | ||
allow_module_overrides=allow_module_overrides, | ||
allow_node_overrides=allow_module_overrides, | ||
) | ||
|
||
@staticmethod | ||
def from_functions( | ||
*functions, | ||
*functions: FunctionType, | ||
config: Dict[str, Any], | ||
adapter: lifecycle_base.LifecycleAdapterSet = None, | ||
allow_module_overrides: bool = False, | ||
) -> "FunctionGraph": | ||
nodes = create_function_graph( | ||
*functions, | ||
return FunctionGraph.compile( | ||
modules=[], | ||
functions=functions, | ||
config=config, | ||
adapter=adapter, | ||
allow_module_overrides=allow_module_overrides, | ||
allow_node_overrides=allow_module_overrides, | ||
) | ||
return FunctionGraph(nodes, config, adapter) | ||
|
||
def with_nodes(self, nodes: Dict[str, Node]) -> "FunctionGraph": | ||
"""Creates a new function graph with the additional specified nodes. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we want this? Why can't we just put it in the
.with_modules
?E.g. this opens up issues of precedence when using both modules and functions.