mirror of
https://github.com/p2p-ld/nwb-linkml.git
synced 2024-11-10 00:34:29 +00:00
[tests] nocover stuff that's just from the source module
This commit is contained in:
parent
a0d96014e1
commit
c215e69fd8
2 changed files with 36 additions and 29 deletions
|
@ -254,7 +254,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
skips = ('AnyType',)
|
skips = ('AnyType',)
|
||||||
|
|
||||||
for cls in needed_classes:
|
for cls in needed_classes:
|
||||||
if cls in skips:
|
if cls in skips: # pragma: no cover
|
||||||
continue
|
continue
|
||||||
# Find module that contains class
|
# Find module that contains class
|
||||||
module_name = sv.element_by_schema_map()[ElementName(cls)]
|
module_name = sv.element_by_schema_map()[ElementName(cls)]
|
||||||
|
@ -306,7 +306,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
needed_classes.append(cls.is_a)
|
needed_classes.append(cls.is_a)
|
||||||
# get needed classes used as ranges in class attributes
|
# get needed classes used as ranges in class attributes
|
||||||
for slot in class_slots[cls.name]:
|
for slot in class_slots[cls.name]:
|
||||||
if slot.name in self.SKIP_SLOTS:
|
if slot.name in self.SKIP_SLOTS: # pragma: no cover
|
||||||
continue
|
continue
|
||||||
if slot.range in all_classes:
|
if slot.range in all_classes:
|
||||||
needed_classes.append(slot.range)
|
needed_classes.append(slot.range)
|
||||||
|
@ -417,7 +417,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
# all dimensions should be the same dtype
|
# all dimensions should be the same dtype
|
||||||
try:
|
try:
|
||||||
dtype = flat_to_npytyping[list(attrs.values())[0].range]
|
dtype = flat_to_npytyping[list(attrs.values())[0].range]
|
||||||
except KeyError as e:
|
except KeyError as e: # pragma: no cover
|
||||||
warnings.warn(str(e))
|
warnings.warn(str(e))
|
||||||
range = list(attrs.values())[0].range
|
range = list(attrs.values())[0].range
|
||||||
return f'List[{range}] | {range}'
|
return f'List[{range}] | {range}'
|
||||||
|
@ -428,7 +428,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
|
|
||||||
def _get_numpy_slot_range(self, cls:ClassDefinition) -> str:
|
def _get_numpy_slot_range(self, cls:ClassDefinition) -> str:
|
||||||
# if none of the dimensions are optional, we just have one possible array shape
|
# if none of the dimensions are optional, we just have one possible array shape
|
||||||
if all([s.required for s in cls.attributes.values()]):
|
if all([s.required for s in cls.attributes.values()]): # pragma: no cover
|
||||||
return self._make_npytyping_range(cls.attributes)
|
return self._make_npytyping_range(cls.attributes)
|
||||||
# otherwise we need to make permutations
|
# otherwise we need to make permutations
|
||||||
# but not all permutations, because we typically just want to be able to exlude the last possible dimensions
|
# but not all permutations, because we typically just want to be able to exlude the last possible dimensions
|
||||||
|
@ -500,7 +500,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
slist = slist + [candidate]
|
slist = slist + [candidate]
|
||||||
del clist[i]
|
del clist[i]
|
||||||
break
|
break
|
||||||
if not can_add:
|
if not can_add: # pragma: no cover
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"could not find suitable element in {clist} that does not ref {slist}"
|
f"could not find suitable element in {clist} that does not ref {slist}"
|
||||||
)
|
)
|
||||||
|
@ -525,12 +525,14 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
Parent class get class range
|
Parent class get class range
|
||||||
|
|
||||||
Overriding to not use strings in the type hint when a class has an identifier value
|
Overriding to not use strings in the type hint when a class has an identifier value
|
||||||
|
|
||||||
|
Not testing this method except for what we changes
|
||||||
"""
|
"""
|
||||||
sv = self.schemaview
|
sv = self.schemaview
|
||||||
range_cls = sv.get_class(slot_range)
|
range_cls = sv.get_class(slot_range)
|
||||||
|
|
||||||
# Hardcoded handling for Any
|
# Hardcoded handling for Any
|
||||||
if range_cls.class_uri == "linkml:Any":
|
if range_cls.class_uri == "linkml:Any": # pragma: no cover
|
||||||
return "Any"
|
return "Any"
|
||||||
|
|
||||||
# Inline the class itself only if the class is defined as inline, or if the class has no
|
# Inline the class itself only if the class is defined as inline, or if the class has no
|
||||||
|
@ -546,7 +548,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
if (
|
if (
|
||||||
len([x for x in sv.class_induced_slots(slot_range) if x.designates_type]) > 0
|
len([x for x in sv.class_induced_slots(slot_range) if x.designates_type]) > 0
|
||||||
and len(sv.class_descendants(slot_range)) > 1
|
and len(sv.class_descendants(slot_range)) > 1
|
||||||
):
|
): # pragma: no cover
|
||||||
return (
|
return (
|
||||||
"Union["
|
"Union["
|
||||||
+ ",".join([camelcase(c) for c in sv.class_descendants(slot_range)])
|
+ ",".join([camelcase(c) for c in sv.class_descendants(slot_range)])
|
||||||
|
@ -556,26 +558,17 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
return f"{camelcase(slot_range)}"
|
return f"{camelcase(slot_range)}"
|
||||||
|
|
||||||
# For the more difficult cases, set string as the default and attempt to improve it
|
# For the more difficult cases, set string as the default and attempt to improve it
|
||||||
range_cls_identifier_slot_range = "str"
|
range_cls_identifier_slot_range = "str" # pragma: no cover
|
||||||
|
|
||||||
# For mixins, try to use the identifier slot of descendant classes
|
# For mixins, try to use the identifier slot of descendant classes
|
||||||
if (
|
if (
|
||||||
self.gen_mixin_inheritance
|
self.gen_mixin_inheritance
|
||||||
and sv.is_mixin(range_cls.name)
|
and sv.is_mixin(range_cls.name)
|
||||||
and sv.get_identifier_slot(range_cls.name)
|
and sv.get_identifier_slot(range_cls.name)
|
||||||
):
|
): # pragma: no cover
|
||||||
range_cls_identifier_slot_range = self.get_mixin_identifier_range(range_cls)
|
range_cls_identifier_slot_range = self.get_mixin_identifier_range(range_cls)
|
||||||
|
|
||||||
# If the class itself has an identifier slot, it can be allowed to overwrite a value from mixin above
|
return range_cls_identifier_slot_range # pragma: no cover
|
||||||
# if (
|
|
||||||
# sv.get_identifier_slot(range_cls.name) is not None
|
|
||||||
# and sv.get_identifier_slot(range_cls.name).range is not None
|
|
||||||
# ):
|
|
||||||
# range_cls_identifier_slot_range = _get_pyrange(
|
|
||||||
# sv.get_type(sv.get_identifier_slot(range_cls.name).range), sv
|
|
||||||
# )
|
|
||||||
|
|
||||||
return range_cls_identifier_slot_range
|
|
||||||
|
|
||||||
|
|
||||||
def get_class_isa_plus_mixins(self, classes:Optional[List[ClassDefinition]] = None) -> Dict[str, List[str]]:
|
def get_class_isa_plus_mixins(self, classes:Optional[List[ClassDefinition]] = None) -> Dict[str, List[str]]:
|
||||||
|
@ -587,7 +580,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
sv = self.schemaview
|
sv = self.schemaview
|
||||||
if classes is None:
|
if classes is None: # pragma: no cover
|
||||||
classes = sv.all_classes(imports=False).values()
|
classes = sv.all_classes(imports=False).values()
|
||||||
|
|
||||||
parents = {}
|
parents = {}
|
||||||
|
@ -595,7 +588,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
class_parents = []
|
class_parents = []
|
||||||
if class_def.is_a:
|
if class_def.is_a:
|
||||||
class_parents.append(camelcase(class_def.is_a))
|
class_parents.append(camelcase(class_def.is_a))
|
||||||
if self.gen_mixin_inheritance and class_def.mixins:
|
if self.gen_mixin_inheritance and class_def.mixins: # pragma: no cover
|
||||||
class_parents.extend([camelcase(mixin) for mixin in class_def.mixins])
|
class_parents.extend([camelcase(mixin) for mixin in class_def.mixins])
|
||||||
if len(class_parents) > 0:
|
if len(class_parents) > 0:
|
||||||
# Use the sorted list of classes to order the parent classes, but reversed to match MRO needs
|
# Use the sorted list of classes to order the parent classes, but reversed to match MRO needs
|
||||||
|
@ -636,7 +629,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
|
|
||||||
if slot.inlined and not slot.inlined_as_list: # and has_identifier_slot:
|
if slot.inlined and not slot.inlined_as_list: # and has_identifier_slot:
|
||||||
slot_value = "default_factory=dict"
|
slot_value = "default_factory=dict"
|
||||||
else:
|
else: # pragma: no cover
|
||||||
slot_value = "default_factory=list"
|
slot_value = "default_factory=list"
|
||||||
|
|
||||||
return slot_value
|
return slot_value
|
||||||
|
@ -686,7 +679,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
# skip actually generating arraylike classes, just use them to generate
|
# skip actually generating arraylike classes, just use them to generate
|
||||||
# the npytyping annotations
|
# the npytyping annotations
|
||||||
pyschema.classes[class_def.name] = class_def
|
pyschema.classes[class_def.name] = class_def
|
||||||
else:
|
else: # pragma: no cover
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Not sure why this happens
|
# Not sure why this happens
|
||||||
|
@ -729,7 +722,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
# model classes, we allow container classes to also
|
# model classes, we allow container classes to also
|
||||||
# be generic descendants of BaseModel
|
# be generic descendants of BaseModel
|
||||||
# --------------------------------------------------
|
# --------------------------------------------------
|
||||||
if 'DynamicTable' in pyranges:
|
if 'DynamicTable' in pyranges: # pragma: no cover
|
||||||
pyranges.append('BaseModel')
|
pyranges.append('BaseModel')
|
||||||
|
|
||||||
pyranges = list(set(pyranges)) # remove duplicates
|
pyranges = list(set(pyranges)) # remove duplicates
|
||||||
|
@ -739,15 +732,15 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
pyrange = pyranges[0]
|
pyrange = pyranges[0]
|
||||||
elif len(pyranges) > 1:
|
elif len(pyranges) > 1:
|
||||||
pyrange = f"Union[{', '.join(pyranges)}]"
|
pyrange = f"Union[{', '.join(pyranges)}]"
|
||||||
else:
|
else: # pragma: no cover
|
||||||
raise Exception(f"Could not generate python range for {class_name}.{s.name}")
|
raise Exception(f"Could not generate python range for {class_name}.{s.name}")
|
||||||
|
|
||||||
if s.multivalued:
|
if s.multivalued:
|
||||||
if s.inlined or s.inlined_as_list:
|
if s.inlined or s.inlined_as_list:
|
||||||
collection_key = self.generate_collection_key(slot_ranges, s, class_def)
|
collection_key = self.generate_collection_key(slot_ranges, s, class_def)
|
||||||
else:
|
else: # pragma: no cover
|
||||||
collection_key = None
|
collection_key = None
|
||||||
if s.inlined is False or collection_key is None or s.inlined_as_list is True:
|
if s.inlined is False or collection_key is None or s.inlined_as_list is True: # pragma: no cover
|
||||||
pyrange = f"List[{pyrange}]"
|
pyrange = f"List[{pyrange}]"
|
||||||
else:
|
else:
|
||||||
pyrange = f"Dict[{collection_key}, {pyrange}]"
|
pyrange = f"Dict[{collection_key}, {pyrange}]"
|
||||||
|
@ -770,7 +763,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
||||||
)
|
)
|
||||||
return code
|
return code
|
||||||
|
|
||||||
def compile_module(self, module_path:Path=None, module_name:str='test') -> ModuleType: # pragma: no cover - replaced with provider
|
def compile_module(self, module_path:Path=None, module_name:str='test') -> ModuleType:
|
||||||
"""
|
"""
|
||||||
Compiles generated python code to a module
|
Compiles generated python code to a module
|
||||||
:return:
|
:return:
|
||||||
|
|
|
@ -164,7 +164,8 @@ def test_versions(linkml_schema):
|
||||||
|
|
||||||
core_str = NWBPydanticGenerator(
|
core_str = NWBPydanticGenerator(
|
||||||
str(linkml_schema.core_path),
|
str(linkml_schema.core_path),
|
||||||
versions={'imported': 'v4.2.0'}
|
versions={'imported': 'v4.2.0'},
|
||||||
|
split=False
|
||||||
).serialize()
|
).serialize()
|
||||||
|
|
||||||
# the import should be like
|
# the import should be like
|
||||||
|
@ -174,6 +175,19 @@ def test_versions(linkml_schema):
|
||||||
match = re.findall(r'from \.\.\.imported\.v4_2_0.*?MainThing.*?\)', core_str, flags=re.DOTALL)
|
match = re.findall(r'from \.\.\.imported\.v4_2_0.*?MainThing.*?\)', core_str, flags=re.DOTALL)
|
||||||
assert len(match) == 1
|
assert len(match) == 1
|
||||||
|
|
||||||
|
core_str = NWBPydanticGenerator(
|
||||||
|
str(linkml_schema.core_path),
|
||||||
|
versions={'imported': 'v4.2.0'},
|
||||||
|
split=True
|
||||||
|
).serialize()
|
||||||
|
|
||||||
|
# the import should be like
|
||||||
|
# from ...imported.v4_2_0.namespace import (
|
||||||
|
# MainThing
|
||||||
|
# )
|
||||||
|
match = re.findall(r'from \.\.\.imported\.v4_2_0\.namespace.*?MainThing.*?\)', core_str, flags=re.DOTALL)
|
||||||
|
assert len(match) == 1
|
||||||
|
|
||||||
|
|
||||||
def test_arraylike(imported_schema):
|
def test_arraylike(imported_schema):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in a new issue