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

Add example for construct function #907

Merged

Conversation

@ashears
Copy link
Contributor

ashears commented Oct 17, 2019

Change Summary

Added examples for the construct function, as it was mentioned in #898 that this was needed.

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
Ashton Shears added 3 commits Oct 16, 2019
Ashton Shears
Ashton Shears
@codecov

This comment has been minimized.

Copy link

codecov bot commented Oct 17, 2019

Codecov Report

Merging #907 into modify-construct will not change coverage.
The diff coverage is n/a.

@@               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.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update bb36fd3...9459b60. Read the comment docs.

@ashears ashears changed the title Add ex for construct function Add example for construct function Oct 17, 2019
Ashton Shears
@ashears

This comment has been minimized.

Copy link
Contributor Author

ashears commented Oct 17, 2019

I added an entry to the exporting models section.

I am wondering if this the ideal section for this material?
When I think of using a pydantic model without validation, exporting isn't necessarily what comes to mind. If I try to think from the perspective of someone looking to turn off validation of a model, it makes sense to me to have a small section at the end of the 'validation' section.

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.

Copy link
Owner

samuelcolvin left a comment

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.

Copy link
@samuelcolvin

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.

Copy link
@samuelcolvin

samuelcolvin Oct 17, 2019

Owner
Suggested change
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.

Copy link
@samuelcolvin

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)
Comment on lines 6 to 15

This comment has been minimized.

Copy link
@samuelcolvin

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.

@ashears

This comment has been minimized.

Copy link
Contributor Author

ashears commented Oct 17, 2019

Agree with all the comments, I will implement them today or tomorrow.

@samuelcolvin samuelcolvin mentioned this pull request Oct 18, 2019
3 of 5 tasks complete
Ashton Shears added 3 commits Oct 18, 2019
Ashton Shears
…dantic into add-ex-for-construct
Ashton Shears
@ashears

This comment has been minimized.

Copy link
Contributor Author

ashears commented Oct 18, 2019

  • Moved from exporting_models to models:helper_functions
  • Removed unneeded normal validation example
  • Added a basic warning to the usage doc
  • Changed to the new signature

I have not yet done:

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

@samuelcolvin samuelcolvin merged commit bfb7254 into samuelcolvin:modify-construct Oct 19, 2019
10 checks passed
10 checks passed
Header rules No header rules processed
Details
Pages changed 5 new files uploaded
Details
Redirect rules No redirect rules processed
Details
Mixed content No mixed content detected
Details
codecov/project 100% remains the same compared to bb36fd3
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
deploy/netlify Deploy preview ready!
Details
samuelcolvin.pydantic Build #20191018.37 succeeded
Details
samuelcolvin.pydantic (Job Python36) Job Python36 succeeded
Details
samuelcolvin.pydantic (Job Python37) Job Python37 succeeded
Details
@samuelcolvin

This comment has been minimized.

Copy link
Owner

samuelcolvin commented Oct 19, 2019

Thank so much.

Will make some additional tweaks in the other PR before merging into master.

samuelcolvin added a commit that referenced this pull request Oct 23, 2019
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.