Skip to content

Commit 14f9562

Browse files
committed
catch case where a key with a concept name has an equivalent non-variadic key in the template already
1 parent 6daffdc commit 14f9562

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/pynxtools/dataconverter/helpers.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class ValidationProblem(Enum):
7373
ReservedSuffixWithoutField = auto()
7474
ReservedPrefixInWrongContext = auto()
7575
InvalidNexusTypeForNamedConcept = auto()
76+
KeysWithAndWithoutConcept = auto()
7677

7778

7879
class Collector:
@@ -185,6 +186,11 @@ def _log(self, path: str, log_type: ValidationProblem, value: Optional[Any], *ar
185186
f"The type ('{args[0] if args else '<unknown>'}') of the given concept '{path}' "
186187
f"conflicts with another existing concept of the same name, which is of type '{value.type}'."
187188
)
189+
elif log_type == ValidationProblem.KeysWithAndWithoutConcept:
190+
value = cast(Any, value)
191+
logger.warning(
192+
f"The key '{path}' uses the valid concept name '{args[0]}', but there is another valid key {value} that uses the non-variadic name of the node.'"
193+
)
188194

189195
def collect_and_log(
190196
self,

src/pynxtools/dataconverter/validation.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,30 @@ def find_instance_name_conflicts(mapping: MutableMapping[str, str]) -> None:
898898

899899
# Determine the parent path up to just before this match
900900
parent_path = key[: match.start()]
901+
child_path = key[match.start() :].split("/", 1)[-1]
902+
903+
# Here we check if for this key with a concept name, another valid key
904+
# with a non-concept name exists.
905+
non_concept_key = f"{parent_path}{instance_name}/{child_path}"
906+
907+
if non_concept_key in mapping:
908+
try:
909+
node = add_best_matches_for(non_concept_key, tree)
910+
if node is not None:
911+
collector.collect_and_log(
912+
key,
913+
ValidationProblem.KeysWithAndWithoutConcept,
914+
non_concept_key,
915+
concept_name,
916+
)
917+
collector.collect_and_log(
918+
key, ValidationProblem.KeyToBeRemoved, "key"
919+
)
920+
keys_to_remove.append(key)
921+
continue
922+
except TypeError:
923+
pass
924+
901925
instance_usage[(instance_name, parent_path)].append((concept_name, key))
902926

903927
for (instance_name, parent_path), entries in sorted(instance_usage.items()):

tests/dataconverter/test_validation.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,20 @@ def listify_template(data_dict: Template):
234234
@pytest.mark.parametrize(
235235
"data_dict,error_messages",
236236
[
237+
pytest.param(
238+
alter_dict(
239+
TEMPLATE,
240+
"/ENTRY[my_entry]/NOTE[required_group2]/description",
241+
"an additional description",
242+
),
243+
[
244+
"The key '/ENTRY[my_entry]/NOTE[required_group2]/description' uses the valid concept name 'NOTE', "
245+
"but there is another valid key /ENTRY[my_entry]/required_group2/description that uses the non-variadic "
246+
"name of the node.'",
247+
"The key /ENTRY[my_entry]/NOTE[required_group2]/description will not be written.",
248+
],
249+
id="same-concept-with-and-without-concept-name",
250+
),
237251
pytest.param(
238252
alter_dict(
239253
alter_dict(

0 commit comments

Comments
 (0)