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

feat: avoid reconstructing models used as fields of another model on validation #2193

Conversation

PrettyWood
Copy link
Member

@PrettyWood PrettyWood commented Dec 10, 2020

Change Summary

Right now we always copy a BaseModel when validated, which should not be the default behaviour IMHO.

While working on this I thought about "feature flags" to be able to change the behaviour of all pydantic models but probably other classes / metaclasses Don't know if we want to put everything in the BaseConfig or if we want to separate what's global and local to a BaseModel.

ℹ️ I opened a discussion #2194 and added some documentation if we stick to the current way in #2211
Depending on what's decided, I'll update this PR and add some documentation

Related issue number

closes #265
closes #1246
closes #1837

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 Dec 10, 2020

Codecov Report

Merging #2193 (c06a3b0) into master (a1ac464) will increase coverage by 0.04%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #2193      +/-   ##
==========================================
+ Coverage   99.88%   99.93%   +0.04%     
==========================================
  Files          22       22              
  Lines        4351     4351              
  Branches      875      875              
==========================================
+ Hits         4346     4348       +2     
+ Misses          5        3       -2     
Impacted Files Coverage Δ
pydantic/_hypothesis_plugin.py 97.41% <0.00%> (+1.72%) ⬆️

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 a1ac464...c06a3b0. Read the comment docs.

@PrettyWood PrettyWood force-pushed the feat/inherit-model-avoid-reconstruction branch from 4da511e to e189506 Compare December 18, 2020 22:01
Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

seems fine, but we need docs too.

pydantic/main.py Outdated Show resolved Hide resolved
@@ -0,0 +1,3 @@
**Possible Breaking Change:** Inherited models as fields are not reconstructed (copied) anymore on validation
To keep old behaviour, set `copy_on_model_validation = True` in the `Config` of your `BaseModel`.
To change it globally for all your _pydantic_ models, you can set `BaseConfig.copy_on_model_validation = True`
Copy link
Member

Choose a reason for hiding this comment

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

probably best to move some of this to the docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the section from the other PR to explain how to change the behaviour globally.
Added the feature flag in the doc and change the default value to avoid any breaking change.
Ready for another review :)

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

otherwise LGTM.

docs/examples/model_config_change_globally_baseconfig.py Outdated Show resolved Hide resolved
@samuelcolvin samuelcolvin merged commit f11b3ae into pydantic:master Feb 12, 2021
@PrettyWood PrettyWood deleted the feat/inherit-model-avoid-reconstruction branch February 12, 2021 13:26
@samuelcolvin
Copy link
Member

Hi @PrettyWood unfortunately, merging this has caused tests to fail: https://github.com/samuelcolvin/pydantic/runs/1887580866

Any chance you could look into it?

When I get a chance, I'll move these binary builds to be run on PR tests as well as master to avoid problems like this. There's no way you could have predicted this, in fact I have no idea why those tests are any different to earlier tests with pydantic compiled.

@PrettyWood
Copy link
Member Author

Damn sorry...on it!

mcgfeller added a commit to mcgfeller/DDH that referenced this pull request Dec 28, 2022
mcgfeller added a commit to mcgfeller/DDH that referenced this pull request Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants