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

Convert to use pydantic #70

Merged
merged 1 commit into from
Jan 18, 2024
Merged

Convert to use pydantic #70

merged 1 commit into from
Jan 18, 2024

Conversation

GDYendell
Copy link
Member

@GDYendell GDYendell commented Dec 18, 2023

Fixes #65
Closes #9 as not needed

@codecov-commenter
Copy link

codecov-commenter commented Dec 18, 2023

Codecov Report

Attention: 14 lines in your changes are missing coverage. Please review.

Comparison is base (3e0640a) 88.55% compared to head (aaaca3f) 88.55%.
Report is 3 commits behind head on main.

Files Patch % Lines
src/pvi/_yaml_utils.py 82.05% 7 Missing ⚠️
src/pvi/_pv_group.py 40.00% 3 Missing ⚠️
src/pvi/__main__.py 87.50% 1 Missing ⚠️
src/pvi/_convert/_asyn_convert.py 83.33% 1 Missing ⚠️
src/pvi/_format/screen.py 96.42% 1 Missing ⚠️
src/pvi/device.py 99.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #70      +/-   ##
==========================================
- Coverage   88.55%   88.55%   -0.01%     
==========================================
  Files          22       23       +1     
  Lines        1363     1328      -35     
==========================================
- Hits         1207     1176      -31     
+ Misses        156      152       -4     

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

@GDYendell GDYendell force-pushed the pydantic branch 3 times, most recently from fc9c408 to 3e228d0 Compare December 18, 2023 16:05
@GDYendell
Copy link
Member Author

Here it is @gilesknap

@GDYendell
Copy link
Member Author

GDYendell commented Jan 11, 2024

One thing that I don't like about this is that the type discriminator field of the models is not considered special and that means serialization cannot use exclude_default=True, because the Literal strings are the default and only possible value for the field. It can only use exclude_none=True. This means that any field of a model that shouldn't appear in the converted output cannot use default values, it must use None and manually convert to the default value in a getter or in the caller code.

I did quite a lot of searching and I think this is currently an unsolved problem in pydantic v2. It is quite a lot of extra work for the user to make sure the discriminator is included in the serialized output so that the code knows what specific type to instantiate when deserializing. Maybe there will be a better way in future.

@gilesknap
Copy link
Member

One thing that I don't like about this is that the type discriminator field of the models is not considered special and that means serialization cannot use exclude_default=True, because the Literal strings are the default and only possible value for the field. It can only use exclude_none=True. This means that any field of a model that shouldn't appear in the converted output cannot use default values, it must use None and manually convert to the default value in a getter or in the caller code.

I did quite a lot of searching and I think this is currently an unsolved problem in pydantic v2. It is quite a lot of extra work for the user to make sure the discriminator is included in the serialized output so that the code knows what specific type to instantiate when deserializing. Maybe there will be a better way in future.

I also see this problem in ibek. It makes the yaml more verbose that necessary but is not an absolute disaster.

@gilesknap
Copy link
Member

I'm not sure I can do a meaningful review of this code without spending a week or so getting my head into it. I will instead, try out this version against ibek and see if the example usage that I have still works.

Will try to get that done this afternoon 15 Jan.

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.

I tried this out in an ioc-adsimdetector example. I noticed that ibek's call to the constructor of IndexEntry needed to have its positional arguments made into keyword.

I then verified that it ran OK and generated the same bob files as the previous version.

So I think it's good to go.

@GDYendell GDYendell merged commit d500bcd into main Jan 18, 2024
14 checks passed
@GDYendell GDYendell deleted the pydantic branch January 18, 2024 16:30
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.

Update to use pydantic Try using discriminator
3 participants