Skip to content

Commit

Permalink
Catch error when _describe() is not implemented properly (#4035)
Browse files Browse the repository at this point in the history
* Added describe output validation

Signed-off-by: Elena Khaustova <[email protected]>

* Implemented tests for bad describe

Signed-off-by: Elena Khaustova <[email protected]>

* Moved validation to __repr__

Signed-off-by: Elena Khaustova <[email protected]>

* Applied suggested changes

Signed-off-by: Elena Khaustova <[email protected]>

---------

Signed-off-by: Elena Khaustova <[email protected]>
Co-authored-by: Nok Lam Chan <[email protected]>
  • Loading branch information
ElenaKhaustova and noklam authored Jul 30, 2024
1 parent 52458c2 commit c37e1e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 11 additions & 1 deletion kedro/io/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,17 @@ def _pretty_repr(self, object_description: dict[str, Any]) -> str:
return f"{type(self).__module__}.{type(self).__name__}({', '.join(str_keys)})"

def __repr__(self) -> str:
return self._pretty_repr(self._describe())
object_description = self._describe()
if isinstance(object_description, dict) and all(
isinstance(key, str) for key in object_description
):
return self._pretty_repr(object_description)

self._logger.warning(
f"'{type(self).__module__}.{type(self).__name__}' is a subclass of AbstractDataset and it must "
f"implement the '_describe' method following the signature of AbstractDataset's '_describe'."
)
return f"{type(self).__module__}.{type(self).__name__}()"

@abc.abstractmethod
def load(self) -> _DO:
Expand Down
21 changes: 21 additions & 0 deletions tests/io/test_core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import pprint
import shutil
from decimal import Decimal
Expand Down Expand Up @@ -223,6 +224,26 @@ def test_str_representation_none(self):
== f"tests.io.test_core.MyDataset(filepath={filepath_str})"
)

@pytest.mark.parametrize(
"describe_return",
[None, {"key_1": "val_1", 2: "val_2"}],
)
def test_repr_bad_describe(self, describe_return, caplog):
class BadDescribeDataset(MyDataset):
def _describe(self):
return describe_return

warning_message = (
"'tests.io.test_core.BadDescribeDataset' is a subclass of AbstractDataset and it must "
"implement the '_describe' method following the signature of AbstractDataset's '_describe'."
)

with caplog.at_level(logging.WARNING):
assert (
repr(BadDescribeDataset()) == "tests.io.test_core.BadDescribeDataset()"
)
assert warning_message in caplog.text

def test_get_filepath_str(self):
path = get_filepath_str(PurePosixPath("example.com/test.csv"), "http")
assert isinstance(path, str)
Expand Down

0 comments on commit c37e1e3

Please sign in to comment.