Skip to content

Commit

Permalink
Include YAML path with Device validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GDYendell committed Mar 28, 2024
1 parent 50cb1be commit 62609fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/pvi/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
BaseModel,
ConfigDict,
Field,
ValidationError,
field_validator,
model_validator,
)
Expand Down Expand Up @@ -463,7 +464,11 @@ def deserialize(cls, yaml: Path) -> Device:
"""
serialized = cls.validate_yaml(yaml)
return cls(**serialized)
try:
return cls(**serialized)
except ValidationError:
print(f"\nFailed to validate `{yaml}` as Device:\n")
raise

def deserialize_parents(self, yaml_paths: list[Path]):
"""Populate Device with Components from Device yaml of parent classes.
Expand Down
9 changes: 9 additions & 0 deletions tests/bad.pvi.device.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
label: label
parent: parent
children:

- type: SignalR
name: OutA
red_pv: OUTA
read_widget:
type: LED
6 changes: 6 additions & 0 deletions tests/test_device_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def device():


DEVICE_YAML = Path(__file__).parent / "test.pvi.device.yaml"
BAD_DEVICE_YAML = Path(__file__).parent / "bad.pvi.device.yaml"


def test_serialize(device: Device):
Expand All @@ -72,6 +73,11 @@ def test_deserialize(device: Device):
assert d == device


def test_deserialize_raises():
with pytest.raises(ValidationError):
Device.deserialize(BAD_DEVICE_YAML)


def test_validate_fails():
class NotTypedModel(BaseModel):
pass
Expand Down

0 comments on commit 62609fe

Please sign in to comment.