Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd example for construct function #907
Conversation
This comment has been minimized.
This comment has been minimized.
codecov
bot
commented
Oct 17, 2019
•
Codecov Report
@@ Coverage Diff @@
## modify-construct #907 +/- ##
===============================================
Coverage 100% 100%
===============================================
Files 16 16
Lines 2751 2751
Branches 523 523
===============================================
Hits 2751 2751 Continue to review full report at Codecov.
|
This comment has been minimized.
This comment has been minimized.
I added an entry to the exporting models section. I am wondering if this the ideal section for this material? I believe this is just one specific use case that may be worth having explicit documentation for, however since the construct is more broad it should have a more general description in another location. |
Thanks so much, just a few things to fix. |
@@ -84,6 +84,17 @@ as an optional alternative which implements ISO 8601 time diff encoding. | |||
See [below](#custom-json-deserialisation) for details on how to use other libraries for more performant JSON encoding | |||
and decoding. | |||
|
|||
## `model.construct()` |
This comment has been minimized.
This comment has been minimized.
samuelcolvin
Oct 17, 2019
Owner
best to move this to https://pydantic-docs.helpmanual.io/usage/models/#helper-functions
That section will need more work, but at least if put it there for now it will end up in the right place
@@ -84,6 +84,17 @@ as an optional alternative which implements ISO 8601 time diff encoding. | |||
See [below](#custom-json-deserialisation) for details on how to use other libraries for more performant JSON encoding | |||
and decoding. | |||
|
|||
## `model.construct()` | |||
|
|||
Using the construct function, it is possible to instantiate a model without validation. |
This comment has been minimized.
This comment has been minimized.
samuelcolvin
Oct 17, 2019
Owner
Using the construct function, it is possible to instantiate a model without validation. | |
Using the `construct` function, it is possible to instantiate a model without validation. |
We need a more complete description of why you might use construct
and the risks of using it, but if you're not sure what to say, leave it and I'll do this later.
print(e) | ||
|
||
# Instantiate the model without validation | ||
c = Model.construct(values=valid_data, fields_set=None) |
This comment has been minimized.
This comment has been minimized.
samuelcolvin
Oct 17, 2019
Owner
as per #898 (comment) the signature is going to change here, you might need to merge those changes once they're done and change this.
valid_data = dict(a=5) | ||
invalid_data = dict(a="dog") | ||
|
||
# Validate the model at instantiation | ||
m = Model(**valid_data) | ||
print(m) | ||
try: | ||
Model(**invalid_data) | ||
except ValidationError as e: | ||
print(e) |
This comment has been minimized.
This comment has been minimized.
samuelcolvin
Oct 17, 2019
Owner
I don't think there's a need for this section, it's already covered in other examples.
Best just to give an example of the correct usage of construct()
, then have a prominent note (perhaps a !!! warning
) about misuse of of the method.
This comment has been minimized.
This comment has been minimized.
Agree with all the comments, I will implement them today or tomorrow. |
This comment has been minimized.
This comment has been minimized.
I have not yet done:
|
bfb7254
into
samuelcolvin:modify-construct
This comment has been minimized.
This comment has been minimized.
Thank so much. Will make some additional tweaks in the other PR before merging into master. |
* modify behaviour of the construct method * change construct signature * Add example for construct function (#907) * add example for construct * edit exporting_models * typo * add changes file * code review changes * fix bad copy paste * extend example in docs * use __field_defaults__ in construct
ashears commentedOct 17, 2019
•
edited
Change Summary
Added examples for the construct function, as it was mentioned in #898 that this was needed.
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)