Skip to content

Commit

Permalink
fixed type error
Browse files Browse the repository at this point in the history
  • Loading branch information
blondered committed Feb 4, 2025
1 parent 9dbb668 commit b48e5dd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions rectools/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@


def _serialize_feature_name(spec: tp.Any) -> Hashable:
error_msg = f"""
Serialization for feature name '{spec}' is not supported.
Please convert your feature names and category feature values to strings, numbers, booleans
or their tuples.
"""
type_error = TypeError(
f"""
Serialization for feature name '{spec}' is not supported.
Please convert your feature names and category feature values to strings, numbers, booleans
or their tuples.
"""
)
if isinstance(spec, (list, np.ndarray)):
raise ValueError(error_msg)
raise type_error
if isinstance(spec, tuple):
return tuple(_serialize_feature_name(item) for item in spec)
if isinstance(spec, (int, float, str, bool)):
return spec
if np.issubdtype(spec, np.number) or np.issubdtype(spec, np.bool_):
if np.issubdtype(spec, np.number) or np.issubdtype(spec, np.bool_): # str is handled by isinstance(spec, str)
return spec.item()
raise ValueError(error_msg)
raise type_error


FeatureName = tpe.Annotated[AnyFeatureName, PlainSerializer(_serialize_feature_name, when_used="json")]
Expand Down
2 changes: 1 addition & 1 deletion tests/dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,5 @@ def test_basic(self, feature_name: AnyFeatureName, expected: Hashable) -> None:

@pytest.mark.parametrize("feature_name", (np.array([1]), [1], np.array(["name"]), np.array([True])))
def test_raises_on_incorrect_input(self, feature_name: tp.Any) -> None:
with pytest.raises(ValueError):
with pytest.raises(TypeError):
_serialize_feature_name(feature_name)

0 comments on commit b48e5dd

Please sign in to comment.