Skip to content

Commit

Permalink
.with_materializers() added helpful error message (#986)
Browse files Browse the repository at this point in the history
* added helpful error message

* added unit test; specified condition

---------

Co-authored-by: zilto <tjean@DESKTOP-V6JDCS2>
  • Loading branch information
zilto and zilto authored Jun 27, 2024
1 parent e753f4e commit b836e19
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions hamilton/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# required if we want to run this code stand alone.
import typing
import uuid
from collections.abc import Sequence # typing.Sequence is deprecated in >=3.9
from datetime import datetime
from types import ModuleType
from typing import Any, Callable, Collection, Dict, List, Optional, Set, Tuple, Union
Expand Down Expand Up @@ -1757,6 +1758,18 @@ def with_materializers(
:param materializers: materializers to add to the dataflow
:return: self
"""
if any(
m for m in materializers if not isinstance(m, (ExtractorFactory, MaterializerFactory))
):
if len(materializers) == 1 and isinstance(materializers[0], Sequence):
raise ValueError(
"`.with_materializers()` received a sequence. Unpack it by prepending `*` e.g., `*[to.json(...), from_.parquet(...)]`"
)
else:
raise ValueError(
f"`.with_materializers()` only accepts materializers. Received instead: {materializers}"
)

self.materializers.extend(materializers)
return self

Expand Down
25 changes: 25 additions & 0 deletions tests/test_hamilton_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,31 @@ def test_builder_materializer_and_execution_materializer(tmp_path):
assert "static_saver" in additional.keys()


def test_builder_materializer_error():
static_saver = to.json(
id="static_saver",
dependencies=["json_to_save_1"],
path="/file.json",
)

materializers = [static_saver]
with pytest.raises(ValueError):
(
Builder()
.with_modules(tests.resources.test_for_materialization)
.with_materializers(materializers)
.build()
)

# also check that using `*` leads to no issue
(
Builder()
.with_modules(tests.resources.test_for_materialization)
.with_materializers(*materializers)
.build()
)


def test_materialize_checks_required_input(tmp_path):
dr = Builder().with_modules(tests.resources.dummy_functions).build()

Expand Down

0 comments on commit b836e19

Please sign in to comment.