mirror of
https://github.com/p2p-ld/nwb-linkml.git
synced 2025-01-09 21:54:27 +00:00
make the tests pass again - add validator to do the opposite of coerce_value, try to pass the input as the `value
` field of the model. fix test assumptions and model creation.
model update to follow
This commit is contained in:
parent
734088f18e
commit
8993014832
7 changed files with 37 additions and 13 deletions
|
@ -59,9 +59,7 @@ class MapScalar(DatasetMap):
|
|||
slots:
|
||||
- name: MyScalar
|
||||
description: A scalar
|
||||
multivalued: false
|
||||
range: int32
|
||||
required: false
|
||||
|
||||
"""
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ from linkml_runtime.utils.formatutils import remove_empty_items
|
|||
from linkml_runtime.utils.schemaview import SchemaView
|
||||
|
||||
from nwb_linkml.includes.base import (
|
||||
BASEMODEL_CAST_WITH_VALUE,
|
||||
BASEMODEL_COERCE_CHILD,
|
||||
BASEMODEL_COERCE_VALUE,
|
||||
BASEMODEL_GETITEM,
|
||||
|
@ -55,6 +56,7 @@ class NWBPydanticGenerator(PydanticGenerator):
|
|||
'object_id: Optional[str] = Field(None, description="Unique UUID for each object")',
|
||||
BASEMODEL_GETITEM,
|
||||
BASEMODEL_COERCE_VALUE,
|
||||
BASEMODEL_CAST_WITH_VALUE,
|
||||
BASEMODEL_COERCE_CHILD,
|
||||
)
|
||||
split: bool = True
|
||||
|
|
|
@ -16,7 +16,7 @@ BASEMODEL_GETITEM = """
|
|||
BASEMODEL_COERCE_VALUE = """
|
||||
@field_validator("*", mode="wrap")
|
||||
@classmethod
|
||||
def coerce_value(cls, v: Any, handler) -> Any:
|
||||
def coerce_value(cls, v: Any, handler, info) -> Any:
|
||||
\"\"\"Try to rescue instantiation by using the value field\"\"\"
|
||||
try:
|
||||
return handler(v)
|
||||
|
@ -27,7 +27,29 @@ BASEMODEL_COERCE_VALUE = """
|
|||
try:
|
||||
return handler(v["value"])
|
||||
except (IndexError, KeyError, TypeError):
|
||||
raise e1
|
||||
raise ValueError(
|
||||
f"coerce_value: Could not use the value field of {type(v)} "
|
||||
f"to construct {cls.__name__}.{info.field_name}, "
|
||||
f"expected type: {cls.model_fields[info.field_name].annotation}"
|
||||
) from e1
|
||||
"""
|
||||
|
||||
BASEMODEL_CAST_WITH_VALUE = """
|
||||
@field_validator("*", mode="wrap")
|
||||
@classmethod
|
||||
def cast_with_value(cls, v: Any, handler, info) -> Any:
|
||||
\"\"\"Try to rescue instantiation by casting into the model's value fiel\"\"\"
|
||||
try:
|
||||
return handler(v)
|
||||
except Exception as e1:
|
||||
try:
|
||||
return handler({"value": v})
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
f"cast_with_value: Could not cast {type(v)} as value field for "
|
||||
f"{cls.__name__}.{info.field_name},"
|
||||
f" expected_type: {cls.model_fields[info.field_name].annotation}"
|
||||
) from e1
|
||||
"""
|
||||
|
||||
BASEMODEL_COERCE_CHILD = """
|
||||
|
|
|
@ -54,7 +54,7 @@ def test_walk_field_values(nwb_core_fixture):
|
|||
text_models = list(nwb_core_fixture.walk_field_values(nwb_core_fixture, "dtype", value="text"))
|
||||
assert all([d.dtype == "text" for d in text_models])
|
||||
# 135 known value from regex search
|
||||
assert len(text_models) == len([d for d in dtype_models if d.dtype == "text"]) == 135
|
||||
assert len(text_models) == len([d for d in dtype_models if d.dtype == "text"]) == 155
|
||||
|
||||
|
||||
def test_build_result(linkml_schema_bare):
|
||||
|
|
|
@ -135,8 +135,9 @@ def test_roll_down_inheritance():
|
|||
child = child_ns_adapter.get("Child")
|
||||
# overrides simple attrs
|
||||
assert child.doc == "child"
|
||||
# gets unassigned parent attrs
|
||||
assert "b" in [attr.name for attr in child.attributes]
|
||||
# we don't receive attrs that aren't overridden in the child,
|
||||
# instead we let python/linkml inheritance handle that for us
|
||||
assert "b" not in [attr.name for attr in child.attributes]
|
||||
# overrides values while preserving remaining values when set
|
||||
attr_a = [attr for attr in child.attributes if attr.name == "a"][0]
|
||||
assert attr_a.value == "z"
|
||||
|
@ -146,7 +147,8 @@ def test_roll_down_inheritance():
|
|||
# preserve unset values in child datasets
|
||||
assert child.datasets[0].dtype == parent_cls.datasets[0].dtype
|
||||
assert child.datasets[0].dims == parent_cls.datasets[0].dims
|
||||
# gets undeclared attrs in child datasets
|
||||
# we *do* get undeclared attrs in child datasets,
|
||||
# since those are not handled by python/linkml inheritance
|
||||
assert "d" in [attr.name for attr in child.datasets[0].attributes]
|
||||
# overrides set values in child datasets while preserving unset
|
||||
c_attr = [attr for attr in child.datasets[0].attributes if attr.name == "c"][0]
|
||||
|
|
|
@ -114,14 +114,14 @@ def _icephys_stimulus_and_response(
|
|||
n_samples = generator.integers(20, 50)
|
||||
stimulus = VoltageClampStimulusSeries(
|
||||
name=f"vcss_{i}",
|
||||
data=VoltageClampStimulusSeriesData(value=[i] * n_samples),
|
||||
data=VoltageClampStimulusSeriesData(value=np.array([i] * n_samples, dtype=float)),
|
||||
stimulus_description=f"{i}",
|
||||
sweep_number=i,
|
||||
electrode=electrode,
|
||||
)
|
||||
response = VoltageClampSeries(
|
||||
name=f"vcs_{i}",
|
||||
data=VoltageClampSeriesData(value=[i] * n_samples),
|
||||
data=VoltageClampSeriesData(value=np.array([i] * n_samples, dtype=float)),
|
||||
stimulus_description=f"{i}",
|
||||
electrode=electrode,
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue