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

Fixes default parameter calculation #711

Merged
merged 1 commit into from
Feb 22, 2024
Merged
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
4 changes: 2 additions & 2 deletions hamilton/function_modifiers/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_function_signatures_compatible(
dummy_param_values = {
key: SENTINEL_ARG_VALUE
for key, param_spec in fn_signature.parameters.items()
if param_spec.default != inspect.Parameter.empty
if param_spec.default is not inspect.Parameter.empty
}
# Then we update with the dummy values. Again, replacing doesn't matter (we'll be mimicking it later)
dummy_param_values.update({key: SENTINEL_ARG_VALUE for key in fn_signature.parameters})
Expand Down Expand Up @@ -214,7 +214,7 @@ def wrapper_function(**kwargs):
final_kwarg_values = {
key: param_spec.default
for key, param_spec in inspect.signature(fn).parameters.items()
if param_spec.default != inspect.Parameter.empty
if param_spec.default is not inspect.Parameter.empty
}
final_kwarg_values.update(kwargs)
final_kwarg_values = does.map_kwargs(final_kwarg_values, self.argument_mapping)
Expand Down
2 changes: 1 addition & 1 deletion hamilton/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def _check_keyword_args_only(func: Callable) -> bool:
"""Checks if a function only takes keyword arguments."""
sig = inspect.signature(func)
for param in sig.parameters.values():
if param.default == inspect.Parameter.empty and param.kind not in [
if param.default is inspect.Parameter.empty and param.kind not in [
inspect.Parameter.KEYWORD_ONLY,
inspect.Parameter.VAR_KEYWORD,
]:
Expand Down
2 changes: 1 addition & 1 deletion hamilton/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class DependencyType(Enum):

@staticmethod
def from_parameter(param: inspect.Parameter):
if param.default == inspect.Parameter.empty:
if param.default is inspect.Parameter.empty:
return DependencyType.REQUIRED
return DependencyType.OPTIONAL

Expand Down
13 changes: 13 additions & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import inspect
import sys
from typing import Any, Literal, TypeVar

Expand Down Expand Up @@ -104,3 +105,15 @@ def annotated_func(first: ArrayN[np.float64], other: float = 2.0) -> ArrayN[np.f
)
def test_tags_match_query(tags: dict, query: dict, expected: bool):
assert matches_query(tags, query) == expected


def test_from_parameter_default_override_equals():
class BrokenEquals:
def __eq__(self, other):
raise ValueError("I'm broken")

def foo(b: BrokenEquals = BrokenEquals()):
pass

param = DependencyType.from_parameter(inspect.signature(foo).parameters["b"])
assert param == DependencyType.OPTIONAL