-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Add example for construct function #907
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
Add example for construct function #907
Conversation
Codecov Report
@@ Coverage Diff @@
## modify-construct #907 +/- ##
===============================================
Coverage 100% 100%
===============================================
Files 16 16
Lines 2751 2751
Branches 523 523
===============================================
Hits 2751 2751Continue to review full report at Codecov.
|
|
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. |
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.
Thanks so much, just a few things to fix.
docs/usage/exporting_models.md
Outdated
| @@ -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()` | |||
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.
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
docs/usage/exporting_models.md
Outdated
| @@ -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. | |||
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.
| 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.
docs/examples/ex_construct.py
Outdated
| print(e) | ||
|
|
||
| # Instantiate the model without validation | ||
| c = Model.construct(values=valid_data, fields_set=None) |
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.
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.
docs/examples/ex_construct.py
Outdated
| 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) |
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 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.
|
Agree with all the comments, I will implement them today or tomorrow. |
I have not yet done:
|
|
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
* modify behaviour of the construct method * change construct signature * Add example for construct function (pydantic#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
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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>.mdfile added describing change(see changes/README.md for details)