-
Notifications
You must be signed in to change notification settings - Fork 292
Model subclass instances #492
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
Conversation
CodSpeed Performance ReportMerging #492 Summary
Benchmarks breakdown
|
Codecov Report
📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more Additional details and impacted files@@ Coverage Diff @@
## main #492 +/- ##
==========================================
- Coverage 94.73% 94.65% -0.09%
==========================================
Files 93 93
Lines 11665 11686 +21
Branches 25 25
==========================================
+ Hits 11051 11061 +10
- Misses 607 618 +11
Partials 7 7
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
I'm happy with this approach! Thanks for pinging and tagging me! 🙇 More details: I was having a small doubt I wanted to confirm on the laptop, thinking about non-model fields. E.g. But I realized, first, I would probably take whatever is declared in the FastAPI path operation function as a Pydantic field, and I'm gonna be the one that creates And then I double checked/confirmed it worked as expected by slightly modifying the example to use a Thanks again for doing this! 🙇
list[DataModel] for completeness: |
As discussed elsewhere (but want to make sure it doesn't get lost) — I think that it is more likely to be useful to have strict mode allow subclass instances. (And I think it will definitely be an early feature request if we don't do that now.) The reason I say this is that, over the years, many people requested that FastAPI be stricter about what is accepted during parsing (i.e., not coercing strings to int, etc.). But I think that is largely orthogonal to the behavior of how handling subclasses works, since that is really a pure-python-API consideration (you don't have class information while parsing, of course). Considering that even with the strictest possible settings, type checkers such as mypy and pyright will not produce errors if you pass proper subclass instances to fields, I expect that, at least among the users it affects, it will be much more popular to allow subclass instances than not. (I could be convinced otherwise if someone could share a compelling use case for this, I haven't seen one though; you can use I know I personally would have preferred both to have strict parsing of primitives and to allow subclass instances to pass validation from the python API (i.e., being allowed as inputs to I'll just add a brief note about invariance, to head off any potential attempts to draw an analogy with how mypy will give a type error for The invariance of |
082ccdd
to
f6ea4c9
Compare
Merged this by mistake. @dmontagu your logic matches the "wisdom" of the twitter crowd, I'll change to match that behaviour. |
TODO:
revalidate
works withoutfrom_attributes
__instancecheck__
in python on every validation, that's going to be super slow, we might need our own fastisinstance
check, this in turn will need to work with the nascentPydanticGenericAlias
- this only seems to add ~10%, I think fineThis allows both instances of the model, and instances of subclasses of the model to be validated.
In strict mode, only instances of the exact model type are allowed - this is up for discussion.
In both strict and lax mode, regardless of whether we have an exact instance or subclass instance, the
validate_instances
config flag (can also be set on the validator directly) forces fields to be revalidated.When re-validating,
__fields_set__
from the original instance is reused.Leaking private data and "the FastAPI problem"
A big problem FastAPI had is was that allowing subclasses meant private information on subclasses was being leaked during serialisation.
E.g.:
However this is not a problem with Pydantic V2 since the serializer build for
ResponseModel
usesDataModel
to build the serialization logic for.data
, and only fields ofDataModel
are included.You can run the above on pydantic main now and it works correctly (you'll need to install
pydantic-core
from this branch, and change.json
to.model_dump_json
, but otherwise it'll work.@tiangolo please confirm you're happy with this approach.