Skip to content

Commit df8001b

Browse files
authored
Fixes enumeration check (#313)
* Fixes enumeration check * Updates get_enums function * Update definitions * Change errors to warnings * Adapt tests * Update defs * Check full inheritance chain for enums * Respond to None in get_enums * Update defs * Cleanup return get_inherited_nodes * Update definitions
1 parent c91046e commit df8001b

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

pynxtools/dataconverter/helpers.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,12 @@ def convert_data_dict_path_to_hdf5_path(path) -> str:
264264
return hdf5path
265265

266266

267-
def is_value_valid_element_of_enum(value, elem) -> Tuple[bool, list]:
267+
def is_value_valid_element_of_enum(value, elist) -> Tuple[bool, list]:
268268
"""Checks whether a value has to be specific from the NXDL enumeration and returns options."""
269-
if elem is not None:
270-
has_enums, enums = nexus.get_enums(elem)
271-
if has_enums and (
272-
isinstance(value, list) or value not in enums[0:-1] or value == ""
273-
):
274-
return False, enums
269+
for elem in elist:
270+
enums = nexus.get_enums(elem)
271+
if enums is not None:
272+
return value in enums, enums
275273
return True, []
276274

277275

@@ -465,7 +463,7 @@ def check_optionality_based_on_parent_group(path, nxdl_path, nxdl_root, data, te
465463
if is_nxdl_path_a_child(
466464
nxdl_path, optional_parent_nxdl
467465
) and not all_required_children_are_set(optional_parent, data, nxdl_root):
468-
raise LookupError(
466+
logger.warning(
469467
f"The data entry, {path}, has an optional parent, "
470468
f"{optional_parent}, with required children set. Either"
471469
f" provide no children for {optional_parent} or provide"
@@ -617,9 +615,12 @@ def validate_data_dict(template, data, nxdl_root: ET.Element):
617615
else "NXDL_TYPE_UNAVAILABLE"
618616
)
619617
data[path] = is_valid_data_field(data[path], nxdl_type, path)
620-
is_valid_enum, enums = is_value_valid_element_of_enum(data[path], elem)
618+
elist = nexus.get_inherited_nodes(
619+
nxdl_path, path.rsplit("/", 1)[-1], nxdl_root
620+
)[2]
621+
is_valid_enum, enums = is_value_valid_element_of_enum(data[path], elist)
621622
if not is_valid_enum:
622-
raise ValueError(
623+
logger.warning(
623624
f"The value at {path} should be on of the "
624625
f"following strings: {enums}"
625626
)

pynxtools/definitions

Submodule definitions updated 50 files

pynxtools/nexus-version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2020.10-1637-g2ac33b2cf
1+
v2020.10-1652-g88ff21539

tests/dataconverter/test_helpers.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ def fixture_filled_test_data(template, tmp_path):
335335
),
336336
(
337337
"The value at /ENTRY[my_entry]/NXODD_name[nxodd_name]/type should be on of the following"
338-
" strings: [1st type,2nd type,3rd type,4th type]"
338+
" strings: ['1st type', '2nd type', '3rd type', '4th type']"
339339
),
340340
id="wrong-enum-choice",
341341
),
@@ -430,6 +430,14 @@ def test_validate_data_dict(
430430
captured_logs = caplog.records
431431
helpers.validate_data_dict(template, data_dict, nxdl_root)
432432
assert any(error_message in rec.message for rec in captured_logs)
433+
elif request.node.callspec.id in (
434+
"wrong-enum-choice",
435+
"atleast-one-required-child-not-provided-optional-parent",
436+
):
437+
with caplog.at_level(logging.WARNING):
438+
helpers.validate_data_dict(template, data_dict, nxdl_root)
439+
440+
assert error_message in caplog.text
433441
else:
434442
with pytest.raises(Exception) as execinfo:
435443
helpers.validate_data_dict(template, data_dict, nxdl_root)

0 commit comments

Comments
 (0)