Skip to content

Commit

Permalink
Bug fix for "validation error when passing zero" (#522)
Browse files Browse the repository at this point in the history
* Update is_valid_data_field functionto have to return two values

* Cancel accidental change in pyproject.toml

* Fix linting
  • Loading branch information
GinzburgLev authored Jan 17, 2025
1 parent 9966036 commit ebea8c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/pynxtools/dataconverter/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,11 @@ def _log(self, path: str, log_type: ValidationProblem, value: Optional[Any], *ar

if log_type == ValidationProblem.UnitWithoutDocumentation:
logger.warning(
f"The unit, {path} = {value}, "
"is being written but has no documentation"
f"The unit, {path} = {value}, is being written but has no documentation"
)
elif log_type == ValidationProblem.InvalidEnum:
logger.warning(
f"The value at {path} should be on of the "
f"following strings: {value}"
f"The value at {path} should be on of the following strings: {value}"
)
elif log_type == ValidationProblem.MissingRequiredGroup:
logger.warning(f"The required group, {path}, hasn't been supplied.")
Expand Down Expand Up @@ -344,7 +342,7 @@ def get_all_defined_required_children_for_elem(xml_element):
list_of_children_to_add.add(f"{name_to_add}/@units")
elif tag == "group":
nxdlpath = (
f'{xml_element.get("nxdlpath")}/{get_nxdl_name_from_elem(child)}'
f"{xml_element.get('nxdlpath')}/{get_nxdl_name_from_elem(child)}"
)
nxdlbase = xml_element.get("nxdlbase")
nx_name = nxdlbase[nxdlbase.rfind("/") + 1 : nxdlbase.rfind(".nxdl")]
Expand Down Expand Up @@ -691,24 +689,29 @@ def is_valid_data_field(value, nxdl_type, path):
If it fails to convert, it raises an Exception.
As a default it just returns the value again.
Returns two values: first, boolean (True if the the value corresponds to nxdl_type,
False otherwise) and second, result of attempted conversion or the original value
(if conversion is not needed or impossible)
"""
accepted_types = NEXUS_TO_PYTHON_DATA_TYPES[nxdl_type]
output_value = value

if not isinstance(value, dict) and not is_valid_data_type(value, accepted_types):
try:
if accepted_types[0] is bool and isinstance(value, str):
value = convert_str_to_bool_safe(value)
if value is None:
raise ValueError
return accepted_types[0](value)
output_value = accepted_types[0](value)
except ValueError:
collector.collect_and_log(
path, ValidationProblem.InvalidType, accepted_types, nxdl_type
)
return False, value

if nxdl_type == "NX_POSINT" and not is_positive_int(value):
collector.collect_and_log(path, ValidationProblem.IsNotPosInt, value)
return False, value

if nxdl_type in ("ISO8601", "NX_DATE_TIME"):
iso8601 = re.compile(
Expand All @@ -718,8 +721,9 @@ def is_valid_data_field(value, nxdl_type, path):
results = iso8601.search(value)
if results is None:
collector.collect_and_log(path, ValidationProblem.InvalidDatetime, value)
return False, value

return True
return True, output_value


@lru_cache(maxsize=None)
Expand Down
2 changes: 1 addition & 1 deletion src/pynxtools/dataconverter/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ def is_documented(key: str, node: NexusNode) -> bool:
f"{key}", ValidationProblem.MissingUnit, node.unit
)

return is_valid_data_field(mapping[key], node.dtype, key)
return is_valid_data_field(mapping[key], node.dtype, key)[0]

def recurse_tree(
node: NexusNode,
Expand Down

0 comments on commit ebea8c1

Please sign in to comment.