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
merged 7 commits into from Oct 19, 2019

Conversation

ashears
Copy link
Contributor

@ashears 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)

@codecov
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
@ashears
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
Collaborator

@samuelcolvin 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()`
Copy link
Collaborator

@samuelcolvin samuelcolvin Oct 17, 2019

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

@@ -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.
Copy link
Collaborator

@samuelcolvin samuelcolvin Oct 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Collaborator

@samuelcolvin samuelcolvin Oct 17, 2019

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.

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)
Copy link
Collaborator

@samuelcolvin samuelcolvin Oct 17, 2019

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.

@ashears
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
5 tasks
@ashears
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 pydantic:modify-construct Oct 19, 2019
10 checks passed
@samuelcolvin
Copy link
Collaborator

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 issue 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
andreshndz pushed a commit to cuenca-mx/pydantic that referenced this issue Jan 17, 2020
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants