-
Notifications
You must be signed in to change notification settings - Fork 110
Mark schema fields with None
default as Optional
to pass pydantic v2 validation
#651
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
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #651 +/- ##
==========================================
- Coverage 76.07% 75.92% -0.16%
==========================================
Files 83 83
Lines 7031 7031
Branches 1042 1042
==========================================
- Hits 5349 5338 -11
- Misses 1362 1374 +12
+ Partials 320 319 -1
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danielzuegner Are you able to add a test covering deserialization with optional fields missing?
@janosh Is this strictly necessary, given that it's pydantic functionality? |
I'm assuming this is related to a change of behavior in Pydantic v2, although I have not been able to find documentation of this yet. |
To clarify the change this PR makes is that there was an implicit More generally, this kind of functionality is implicitly tested by the validation performed by pydantic itself, which is how this error was detected. Given that this is a regression and the currently released version of atomate2 is broken for elastic workflows (#659) unless this is addressed, I would suggest we prioritize merging or otherwise fixing if this PR is not sufficient. Perhaps there's something I'm missing here, I'm not sure. My comment here is, in part, because I'm reluctant to add tests for areas I'm not as familiar with, but I would like to see the existing functionality restored. |
@mkhorton While I completely agree that this needs to be urgently fixed, I would like to add that adding tests for this part would be very relevant (e.g., in a future PR). The changes in pydantic disrupted atomate2 quite a lot. |
Exactly, which should make the test code minimal. Just putting the 2 lines @danielzuegner posted above in a test function might do the trick and would allow us to catch breaking from atomate2.common.schemas.elastic import ElasticDocument
ElasticDocument.model_validate_json(ElasticDocument().json()) |
I'm on it, will push something in the next minutes/ hour or so |
@janosh done, added some tests which passed locally for me on my machine. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests look great! Thanks @danielzuegner!
I fixed the ruff error about a missing shebang but some tests are failing with
return PhononBSDOSDoc.from_forces_born(
File "/opt/hostedtoolcache/Python/3.9.18/x64/lib/python3.9/site-packages/atomate2/common/schemas/phonons.py", line 300, in from_forces_born
kpath_dict, kpath_concrete = cls.get_kpath(
AttributeError: type object 'ModelMetaclass' has no attribute 'get_kpath'
Maybe @JaGeo can comment if this has come up before.
No, it hasn't |
@@ -190,6 +190,7 @@ class PhononBSDOSDoc(StructureMetadata): | |||
|
|||
uuids: Optional[PhononUUIDs] = Field("Field including all relevant uuids") | |||
|
|||
@classmethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are there two classmethods?
get_kpath is a static method. Maybe that is why? |
…c.get_kpath access static method from class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All green here. Thanks again @danielzuegner for this fix and @JaGeo for the pointer on where the CI error came from.
Thanks for fixing this, @danielzuegner @mkhorton and @janosh ! |
Thanks @danielzuegner, and @janosh for merging! |
Thanks to everyone who helped getting this merged. Glad to see this fixed. |
Summary
This PR marks all
Field
s withNone
default value asOptional[type]
in the type annotation. This enables to pass the Pydantic validation when deserializing. For instance, the following fails for me withatomate=0.0.12
andpydantic=2.5.2
:gives:
This means that unless all fields are not
None
in aElasticDocument
, deserializing with validation will fail. This PR fixes the issue.The behavior change from
pydantic
v1 to v2 is also confirmed in this GitHub issue.Checklist
Work-in-progress pull requests are encouraged, but please put [WIP] in the pull request
title.
Before a pull request can be merged, the following items must be checked:
The easiest way to handle this is to run the following in the correct sequence on
your local machine. Start with running
ruff
andruff format
on your new code. This willautomatically reformat your code to PEP8 conventions and fix many linting issues.
Run ruff on your code.
type check your code.
Note that the CI system will run all the above checks. But it will be much more
efficient if you already fix most errors prior to submitting the PR. It is highly
recommended that you use the pre-commit hook provided in the repository. Simply run
pre-commit install
and a check will be run prior to allowing commits.