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

Fixes does function wrapping #244 #245

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions hamilton/function_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,8 @@ def generate_node(self, fn: Callable, config) -> node.Node:
and the same parameters/types as the original function.
"""

def replacing_function(__fn=fn, **kwargs):
@functools.wraps(fn)
def wrapper_function(**kwargs):
final_kwarg_values = {
key: param_spec.default
for key, param_spec in inspect.signature(fn).parameters.items()
Expand All @@ -754,7 +755,7 @@ def replacing_function(__fn=fn, **kwargs):
final_kwarg_values = does.map_kwargs(final_kwarg_values, self.argument_mapping)
return self.replacing_function(**final_kwarg_values)

return node.Node.from_fn(fn).copy_with(callabl=replacing_function)
return node.Node.from_fn(fn).copy_with(callabl=wrapper_function)
Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, so let's ship this as it fixes it. I'm confused as to how this didn't take the original function:

typ=self.type,
. It should have used the initial type... Need to dig in.

Copy link
Collaborator Author

@skrawcz skrawcz Dec 14, 2022

Choose a reason for hiding this comment

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

No no, that's not the fix.

  1. I found it confusing that the callable created was named the same as the internal function -- hence this change.
  2. The actual fix is the functools.wraps and removing __fn parameter.



class dynamic_transform(function_modifiers_base.NodeCreator):
Expand Down
14 changes: 14 additions & 0 deletions tests/resources/multiple_decorators_together.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pandas as pd

from hamilton.function_modifiers import does, extract_columns


def _sum_multiply(param0: int, param1: int, param2: int) -> pd.DataFrame:
return pd.DataFrame([{"param0a": param0, "param1b": param1, "param2c": param2}])


@extract_columns("param1b")
@does(_sum_multiply)
def to_modify(param0: int, param1: int, param2: int = 2) -> pd.DataFrame:
"""This sums the inputs it gets..."""
pass
15 changes: 15 additions & 0 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tests.resources.extract_columns_execution_count
import tests.resources.functions_with_generics
import tests.resources.layered_decorators
import tests.resources.multiple_decorators_together
import tests.resources.optional_dependencies
import tests.resources.parametrized_inputs
import tests.resources.parametrized_nodes
Expand Down Expand Up @@ -359,6 +360,20 @@ def test_end_to_end_with_column_extractor_nodes():
)


def test_end_to_end_with_multiple_decorators():
"""Tests that a simple function graph with multiple decorators on a function works end-to-end"""
fg = graph.FunctionGraph(
tests.resources.multiple_decorators_together, config={"param0": 3, "param1": 1}
)
nodes = fg.get_nodes()
results = fg.execute(nodes, {}, {})
print(results)
skrawcz marked this conversation as resolved.
Show resolved Hide resolved
df_expected = tests.resources.multiple_decorators_together._sum_multiply(3, 1, 2)
pd.testing.assert_series_equal(results["param1b"], df_expected["param1b"])
pd.testing.assert_frame_equal(results["to_modify"], df_expected)
assert nodes[0].documentation == "This sums the inputs it gets..."


def test_end_to_end_with_config_modifier():
config = {
"fn_1_version": 1,
Expand Down