Skip to content
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
2 changes: 1 addition & 1 deletion src/power_grid_model/validation/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def all_valid_enum_values(
was not a valid value in the supplied enum type.
"""
valid = [nan_type(component, field)] + list(enum)
invalid = np.isin(data[component][field], valid, invert=True)
invalid = np.isin(data[component][field], np.array(valid, dtype=np.int8), invert=True)
if invalid.any():
ids = data[component]["id"][invalid].flatten().tolist()
return [InvalidEnumValueError(component, field, ids, enum)]
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/validation/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ class MyEnum(IntEnum):
alpha = 2
bravo = 5

# TODO replace this hack with some patch to power_grid_meta_data or nan_type
from power_grid_model import power_grid_meta_data

power_grid_meta_data["input"]["test"] = {"nans": {"value": -128}}

valid = {"test": np.array([(1, 2), (2, 5)], dtype=[("id", "i4"), ("value", "i4")])}
errors = all_valid_enum_values(valid, "test", "value", MyEnum)
assert not errors
Expand All @@ -274,6 +279,18 @@ class MyEnum(IntEnum):
errors = all_valid_enum_values(invalid, "test", "value", MyEnum)
assert len(errors) == 1
assert InvalidEnumValueError("test", "value", [2], MyEnum) in errors
# TODO replace this hack with some patch to power_grid_meta_data or nan_type
del power_grid_meta_data["input"]["test"]

# try with a real enum LoadGenType
# this is a bug in numpy
from power_grid_model import LoadGenType, initialize_array

valid = {"sym_load": initialize_array("input", "sym_load", 20)}
valid["sym_load"]["id"] = np.arange(20)
valid["sym_load"]["type"] = 0
errors = all_valid_enum_values(valid, "sym_load", "type", LoadGenType)
assert not errors


def test_all_valid_ids():
Expand Down