Skip to content

Commit

Permalink
Exposes more information for check_output decorator errors
Browse files Browse the repository at this point in the history
If the user does something that involves the mistyping `target_` or
tries to use a key word argument that doesn't map to a validator for the given
output type, then the error will be cryptic. This adds context that it's
for the check_output decorator and which function it's for.
  • Loading branch information
skrawcz committed Apr 13, 2023
1 parent 2e4df4e commit c6b33e0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion hamilton/data_quality/default_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,6 @@ def resolve_default_validators(
f"No registered subclass of BaseDefaultValidator is available "
f"for arg: {key} and type {output_type}. This either means (a) this arg-type "
f"contribution isn't supported or (b) this has not been added yet (but should be). "
f"In the case of (b), we welcome contributions. Get started at github.com/dagworks-inc/hamilton"
f"In the case of (b), we welcome contributions. Get started at github.com/dagworks-inc/hamilton."
)
return validators
19 changes: 13 additions & 6 deletions hamilton/function_modifiers/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,19 @@ def builds_dataframe(...) -> pd.DataFrame:
"""

def get_validators(self, node_to_validate: node.Node) -> List[dq_base.DataValidator]:
return default_validators.resolve_default_validators(
node_to_validate.type,
importance=self.importance,
available_validators=self.default_decorator_candidates,
**self.default_validator_kwargs,
)
try:
return default_validators.resolve_default_validators(
node_to_validate.type,
importance=self.importance,
available_validators=self.default_decorator_candidates,
**self.default_validator_kwargs,
)
except ValueError as e:
raise ValueError(
f"Could not resolve validators for @check_output for function [{node_to_validate.name}]. "
f"Please check that `target_` is set correctly if you're using that argument.\n"
f"Actual error: {e}"
) from e

def __init__(
self,
Expand Down
16 changes: 16 additions & 0 deletions tests/function_modifiers/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ def test_data_quality_constants_for_api_consistency():
# simple tests to test data quality constants remain the same
assert IS_DATA_VALIDATOR_TAG == "hamilton.data_quality.contains_dq_results"
assert DATA_VALIDATOR_ORIGINAL_OUTPUT_TAG == "hamilton.data_quality.source_node"


def test_check_output_validation_error():
"""Tests that we wrap an error raised appropriately."""
decorator = check_output(
importance="warn",
dtype=np.int64,
)

def fn(input: pd.Series) -> pd.DataFrame:
return pd.DataFrame({"a": input})

node_ = node.Node.from_fn(fn)
with pytest.raises(ValueError) as e:
decorator.transform_node(node_, config={}, fn=fn)
assert "Could not resolve validators for @check_output for function [fn]" in str(e)

0 comments on commit c6b33e0

Please sign in to comment.