Skip to content

Commit 41e6ec3

Browse files
authored
Merge pull request #242 from linkml/add-expand-all-option-to-normalizer
Add ability to expand all dicts to ExpandedDict on referencevalidator.
2 parents bea8840 + 319591a commit 41e6ec3

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

linkml_runtime/processing/referencevalidator.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ class ReferenceValidator:
278278
skip_normalization: bool = None
279279
"""If True, then only perform validation, not normalization"""
280280

281+
expand_all: bool = None
282+
"""If True, then expand all SimpleDict and CompactDict objects to ExpandedDicts"""
283+
281284
def __post_init__(self):
282285
self.derived_schema = self.schemaview.materialize_derived_schema()
283286

@@ -416,6 +419,8 @@ def infer_slot_collection_form(self, parent_slot: SlotDefinition) -> CollectionF
416419
return CollectionForm.List
417420
if parent_slot.inlined_as_list:
418421
return CollectionForm.List
422+
if self.expand_all:
423+
return CollectionForm.ExpandedDict
419424
simple_dict_value_slot = self._slot_as_simple_dict_value_slot(parent_slot)
420425
if simple_dict_value_slot:
421426
return CollectionForm.SimpleDict
@@ -1000,12 +1005,18 @@ def _matches_slot_expression(
10001005

10011006

10021007
@click.command
1003-
@click.option("--schema", "-s", required=True)
1004-
@click.option("--target", "-C")
1005-
@click.option("--report-file", "-R", type=click.File("w"), default=sys.stderr)
1008+
@click.option("--schema", "-s", required=True,
1009+
help="Path to LinkML schema")
1010+
@click.option("--target", "-C",
1011+
help="name of target class or element to normalize/validate against")
1012+
@click.option("--report-file", "-R", type=click.File("w"), default=sys.stderr,
1013+
show_default=True,
1014+
help="path to file for reports")
10061015
@click.option("--output", "-o", type=click.File("w"), default=sys.stdout)
1016+
@click.option("--expand-all/--no-expand-all",
1017+
help="If True, expand all Dicts to ExpandedDicts")
10071018
@click.argument("input")
1008-
def cli(schema: str, target: str, input: str, report_file: TextIO, output: TextIO) -> None:
1019+
def cli(schema: str, target: str, input: str, report_file: TextIO, output: TextIO, **kwargs) -> None:
10091020
"""
10101021
Normalizes and validates a YAML document against a schema.
10111022
@@ -1026,7 +1037,7 @@ def cli(schema: str, target: str, input: str, report_file: TextIO, output: TextI
10261037
:return:
10271038
"""
10281039
sv = SchemaView(schema)
1029-
normalizer = ReferenceValidator(sv)
1040+
normalizer = ReferenceValidator(sv, **kwargs)
10301041
with open(input) as f:
10311042
input_object = yaml.safe_load(f)
10321043
report = Report()

linkml_runtime/utils/schemaview.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ def induced_slot(self, slot_name: SLOT_NAME, class_name: CLASS_NAME = None, impo
11421142
else:
11431143
slot = self.get_slot(slot_name, imports, attributes=True)
11441144
if slot is None:
1145-
raise ValueError(f"Slot {slot_name} as an attribute of {class_name} ancestors"
1145+
raise ValueError(f"No such slot {slot_name} as an attribute of {class_name} ancestors "
11461146
"or as a slot definition in the schema")
11471147
# copy the slot, as it will be modified
11481148
induced_slot = deepcopy(slot)

tests/test_processing/test_referencevalidator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ def test_01_infer_collection_form(self):
291291
f"{slot} -> {expected}",
292292
)
293293
doc.tr(_slot_metaslot_values(slot) + [expected])
294+
normalizer.expand_all = True
295+
inferred_form = normalizer.infer_slot_collection_form(slot)
296+
if expected in [CollectionForm.SimpleDict, CollectionForm.CompactDict]:
297+
expected = CollectionForm.ExpandedDict
298+
self.assertEqual(expected, inferred_form, f"expand_all={normalizer.expand_all}")
299+
294300

295301
def test_02_ensure_collection_forms(self):
296302
"""Test normalization into a collection form."""

0 commit comments

Comments
 (0)