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

revalidate models #177

Merged
merged 2 commits into from Jul 18, 2022
Merged

revalidate models #177

merged 2 commits into from Jul 18, 2022

Conversation

samuelcolvin
Copy link
Member

@samuelcolvin samuelcolvin commented Jul 18, 2022

fix #155 and fix #21

@codecov
Copy link

codecov bot commented Jul 18, 2022

Codecov Report

Merging #177 (cf04609) into main (cf04609) will not change coverage.
The diff coverage is n/a.

❗ Current head cf04609 differs from pull request most recent head 53d8987. Consider uploading reports for the commit 53d8987 to get more accurate results

@@           Coverage Diff           @@
##             main     #177   +/-   ##
=======================================
  Coverage   97.75%   97.75%           
=======================================
  Files          45       45           
  Lines        4371     4371           
  Branches       32       32           
=======================================
  Hits         4273     4273           
  Misses         98       98           

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 cf04609...53d8987. Read the comment docs.

@samuelcolvin samuelcolvin changed the title revaliate models revalidate models Jul 18, 2022
@samuelcolvin
Copy link
Member Author

@tiangolo could you confirm you're happy with this behaviour for fastAPI.

Summary of behavior

The validator checks the input value type - effectively type(input_value) == OutputClass, if True

  • when revalidate_models=False, the input value is output with no further changes.
  • when revalidate_models=True, the input value is completely re-validated using from_attributes, __fields_set__ is copied to the output model if it exists.

There's no isinstance() check anymore.

Note, one potential confusion with this - if you have revalidate_models=True (which I guess will be the default?), and you pass a model instance to the validator, you must have from_attributes=True, otherwise we can't do the re-validation.

Perhaps we make them both true by default. @PrettyWood do you agree?

Perhaps we could:

  • change the default for from_attributes on a type-dict validator depending on whether it's the child of a model-class validator
  • raise an error if from_attributes!=True on a type-dict when it's the child of a model-class validator with revalidate_models=True

???

@samuelcolvin samuelcolvin mentioned this pull request Jul 18, 2022
@tiangolo
Copy link
Member

This looks great! Indeed, it solves the use case in FastAPI. This means I won't have to do the hacky trick to "clone" a field for response_model in FastAPI, so this will be a lot safer and faster. 🎉 🚀

I agree that it makes sense to have both revalidate_models=True and from_attributes=True by default. But I'm of course biased towards what's most useful for FastAPI and SQLModel.

@PrettyWood
Copy link
Member

I understand it's better for FastAPI but personally would have kept false by default. I feel like avoiding copies and stuff is the best option in most validation usecases and will allow other libraries to be built on top of pydantic-core. I would have hence just raised an error if both attributes were not set properly.

@samuelcolvin samuelcolvin marked this pull request as ready for review July 18, 2022 16:43
@samuelcolvin
Copy link
Member Author

I would have hence just raised an error if both attributes were not set properly.

Can do, but we'll need a default in pydantic anyway, we don't want to force people to set config on ever model.

@samuelcolvin
Copy link
Member Author

let's keep this as it is and consider defaults in pydantic, further down the road.

@samuelcolvin samuelcolvin merged commit f29eec1 into main Jul 18, 2022
@samuelcolvin samuelcolvin deleted the revalidate-models branch July 18, 2022 16:53
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.

Validating existing models make sure all values are copied
3 participants