Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include YAML path in validation errors #108

Merged
merged 1 commit into from
Mar 28, 2024
Merged

Include YAML path in validation errors #108

merged 1 commit into from
Mar 28, 2024

Conversation

GDYendell
Copy link
Member

@GDYendell GDYendell commented Mar 27, 2024

This prints the file path with pydantic ValidationErrors raised when trying to create a Device instance from a dictionary loaded from a YAML file.

Fixes #99

@GDYendell GDYendell self-assigned this Mar 27, 2024
Copy link

codecov bot commented Mar 27, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.65%. Comparing base (50cb1be) to head (62609fe).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #108      +/-   ##
==========================================
+ Coverage   89.62%   89.65%   +0.02%     
==========================================
  Files          23       23              
  Lines        1398     1402       +4     
==========================================
+ Hits         1253     1257       +4     
  Misses        145      145              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

gilesknap
gilesknap previously approved these changes Mar 28, 2024
Copy link
Member

@gilesknap gilesknap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I see what you did there.

I think this is a little tidier than my solution of printing a message and re-raising the exception.

My one comment is that it took me a full minute to find the filename in the traceback in your example.

My crude approach puts the filename just before the exception and I found it much more readable.

(I think I have code-blindness - seeing lots of similar text of a screen I can never focus in on the bit I need! - so this may just be a personal issue)

@gilesknap
Copy link
Member

I wouldn't say that re-constructing the validation error was super tidy. So I have a slight preference for catch - print - re-raise.

@GDYendell
Copy link
Member Author

Yeah I think this is just as good and less code:

+ ibek runtime generate /epics/ioc/config/ioc.yaml /epics/ibek-defs/ADCore.ibek.support.yaml /epics/ibek-defs/ADSimDetector.ibek.support.yaml /epics/ibek-defs/asyn.ibek.support.yaml /epics/ibek-defs/autosave.ibek.support.yaml /epics/ibek-defs/epics.ibek.support.yaml /epics/ibek-defs/iocStats.ibek.support.yaml

Failed to validate `/epics/pvi-defs/asynNDArrayDriver.pvi.device.yaml` as Device:

Traceback (most recent call last):

  File "/venv/bin/ibek", line 8, in <module>
    sys.exit(cli())

  File "/venv/lib/python3.10/site-packages/ibek/runtime_cmds/commands.py", line 47, in generate
    pvi_index_entries, pvi_databases = generate_pvi(ioc_instance)

  File "/venv/lib/python3.10/site-packages/ibek/runtime_cmds/commands.py", line 91, in generate_pvi
    device.deserialize_parents([GLOBALS.PVI_DEFS])

  File "/workspaces/pvi/src/pvi/device.py", line 483, in deserialize_parents
    parent_parameters = find_components(self.parent, yaml_paths)

  File "/workspaces/pvi/src/pvi/device.py", line 526, in find_components
    list(find_components(device.parent, yaml_paths))

  File "/workspaces/pvi/src/pvi/device.py", line 523, in find_components
    device = Device.deserialize(device_yaml)

  File "/workspaces/pvi/src/pvi/device.py", line 468, in deserialize
    return cls(**serialized)

  File "/venv/lib/python3.10/site-packages/pydantic/main.py", line 171, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)

pydantic_core._pydantic_core.ValidationError: 2 validation errors for Device
children.0.Group.children.3.SignalR.read_pv
  Field required [type=missing, input_value={'name': 'SerialNumber', ...RBV', 'read_widget': {}}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing
children.0.Group.children.3.SignalR.red_pv
  Extra inputs are not permitted [type=extra_forbidden, input_value='$(P)$(R)SerialNumber_RBV', input_type=str]
    For further information visit https://errors.pydantic.dev/2.6/v/extra_forbidden

@GDYendell GDYendell merged commit da785f1 into main Mar 28, 2024
16 checks passed
@GDYendell GDYendell deleted the validation-error branch March 28, 2024 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Device validation error messages
2 participants