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

Raise error on empty arguments to group #1008

Merged
merged 2 commits into from
Jul 5, 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
16 changes: 10 additions & 6 deletions hamilton/function_modifiers/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,34 +148,38 @@ def source(dependency_on: Any) -> UpstreamDependency:

def _validate_group_params(
dependency_args: List[ParametrizedDependency],
dependecy_kwargs: Dict[str, ParametrizedDependency],
dependency_kwargs: Dict[str, ParametrizedDependency],
):
"""Validates the following for params to group(...):
1. That either dependency_args or dependency_kwargs is non-empty, but not both.
2. That all values in dependency_args are of type either LiteralDependency or UpstreamDependency.

:param dependency_args: List of dependencies.
:param dependecy_kwargs: Dict of dependencies.
:param dependency_kwargs: Dict of dependencies.
:raises: InvalidDecoratorException if the above conditions are not met.
"""
if dependency_args and dependecy_kwargs:
if dependency_args and dependency_kwargs:
raise InvalidDecoratorException(
"group() can either represent a dictionary or a list of dependencies, not both!"
)
if dependency_args:
elif dependency_args:
for dependency in dependency_args:
if not isinstance(dependency, (LiteralDependency, UpstreamDependency)):
raise InvalidDecoratorException(
f"Dependency: {dependency} is not a valid dependency type for group(), must be "
f"a LiteralDependency or UpstreamDependency."
)
if dependecy_kwargs:
for dependency in dependecy_kwargs.values():
elif dependency_kwargs:
for dependency in dependency_kwargs.values():
if not isinstance(dependency, (LiteralDependency, UpstreamDependency)):
raise InvalidDecoratorException(
f"Dependency: {dependency} is not a valid dependency type for group(), must be "
f"a LiteralDependency or UpstreamDependency."
)
else:
raise InvalidDecoratorException(
"Either dependency_args or dependency_kwargs must be non-empty for group()!"
)


def group(
Expand Down
1 change: 1 addition & 0 deletions tests/function_modifiers/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_validate_group_happy(args, kwargs):
([source("foo"), value("bar")], {"foo": source("foo")}),
([], {"foo": "foo", "bar": source("bar")}),
(["bar"], {}),
({}, {}),
],
)
def test_validate_group_sad(args, kwargs):
Expand Down