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
SHOULD/MUST/OPTIONAL fields in models #453
Conversation
Codecov Report
@@ Coverage Diff @@
## master #453 +/- ##
==========================================
+ Coverage 91.48% 91.58% +0.10%
==========================================
Files 61 61
Lines 3065 3103 +38
==========================================
+ Hits 2804 2842 +38
Misses 261 261
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
2d1f9b4
to
be897e5
Compare
c535c76
to
932ba34
Compare
932ba34
to
ecdb1e1
Compare
optimade/models/utils.py
Outdated
for key in kwargs: | ||
if key not in set( | ||
list(_PYDANTIC_FIELD_KWARGS) | ||
+ ["unit", "pattern", "uniqueItems", "support", "queryable", "sortable"] |
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.
I feel like there must be a better way of doing this, but customising pydantic Field
s doesn't seem well documented. This "works" and should catch any potential typos we could make when creating fields. It's a bit of a leaky abstraction...
tests/models/test_utils.py
Outdated
detail = re.escape( | ||
"Not creating StrictField ((Ellipsis,), {'random_key': 'disallowed'}) with forbidden keywords ['random_key']." | ||
) | ||
detail = "forbidden keywords" |
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.
Here (and below) the full warning/error detail wouldn't match, even when using re.escape. Not sure why...
481309c
to
4feee5f
Compare
Some of these were done on purpose, since we were either already testing this, or I found that the additional validation was time-consuming and irrelevant. The latter may be the case for the fields with constant values... |
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.
Some comments.
I am not sure why you're getting the issues concerning the tests and `re' matching. But I've provided a suggested change for you to try out :)
I wonder if we can do this in Config
of a parent model instead, but perhaps not. Perhaps this is the best way. It's at least quite elegant in the end, I think. At least for what it is.
Otherwise we should create a custom JSON encoder that can do all this and be used when creating the OpenAPI specification?
+1
Yeah, I think the only other option is to define OptimadeModel(BaseModel) and StrictModel(BaseModel), but I think that would involve a fair bit more hackery.
Yep, I think we have no other choice if we want this stuff in the schema. |
I only changed them because I didn't like that I had to allow |
fa6b682
to
784e6de
Compare
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.
Like for the other PR, very minor nit-picky things, mainly doc strings.
And just like the other PR, a great acknowledgement of your work here @ml-evs, thanks!
I'm pretty sure the |
8452de5
to
64c9a62
Compare
Yep, fair, reverted. |
2293a25
to
df40d6b
Compare
Have squashed this down to 3 commits, so ready when you are happy with it @CasperWA. |
Annoyingly GH was hiding some remaining unresolved comments, so feel free to disagree with me on those still... |
df40d6b
to
022be53
Compare
a6c66fc
to
cb3979e
Compare
cb3979e
to
f40f894
Compare
Co-authored-by: Casper Welzel Andersen <casper.andersen@epfl.ch>
- Added tests.models.test_utils - Use StrictField and OptimadeField wherever possible
- Fixed fields with missing support/queryable - Fixed some docstring formatting - Made Person->name a MUST - Add support levels for subfields - Promote subfields to full OptimadeFields
f40f894
to
1ec5005
Compare
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.
Great! This should be about ready to get in 👍 cheers @ml-evs
Following the discussion in #399, this PR is a place to try out ways of incorporating the "SHOULD" level of support for some fields in the specification. This PR does the following:
Field
(namelyOptimadeField
andStrictField
).StrictField
disallows keys outside of the pydanticField
signature, plus a couple of extras that we use (unit
,pattern
anduniqueItems
), unfortunately. It emits a warning if a description is not supplied. Any tips on how to do this in a more pydantic way would be appreciated...OptimadeField
callsStrictField
but also forces all fields to supply aqueryable
and asupport
attribute for all fieldsswitch away frompattern=...
toregex=...
insideField(...)
where possible so that regexes are applied automatically on validation.decsription
that means the schema never saw their descriptions... this is basically whatStrictField
will prevent in the future.