-
Notifications
You must be signed in to change notification settings - Fork 302
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Basic implementation here - #1321
Context:
#1322 (comment)
pydantic/pydantic#9708 (comment)
fail-fast feature might be extended and used not only for sequence types. It might be useful for other types like mappings or models.
I can see that it can be powerful feature when you can from python validator stop validation process by raising an exception.
As was mentioned by @adriangb here - #1322 (comment)
I would like to work on both features if you don't mind.
Please let me know if you have some tips and ideas about implementation!
sydney-runkle, 07pepa, MSDehghan, cvillamizarag and charleswhchan
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
sydney-runkle commentedon Jun 25, 2024
@uriyyo,
That sounds wonderful. Thanks for your amazing work here.
I like the idea of:
fail_fast
that is passed tomodel_validate
FailNow
data structure that can be used in custom validatorsLet's confirm with @davidhewitt and @adriangb before moving forwards :).
davidhewitt commentedon Jun 26, 2024
Seems fine to me. I'm less sure about the
FailNow
structure; it might be better to instead passfail_fast
in theinfo
and then let custom validators decide when to fail fast that way.BMMevius commentedon Nov 18, 2024
I would love to be able to use
FailFast()
onLiterals
. Currently I am having some performance troubles with a large number of different objects that can be passed to a list or Sequence. I am using a field called "name" to differentiate the different models. Do you think thatFailFast()
could help speed things up in this case or is it really an architectural issue?sydney-runkle commentedon Nov 18, 2024
Hmm, I don't think
Fail-fast
makes sense for literals. Could you give an example of your use case here? Guessing that discriminated unions might help here. Thanks!BMMevius commentedon Nov 19, 2024
I have a list like this:
The accompanying models are:
pydantic will try to fit the items in the list for each
Namex
model. Effectively creating a running time ofO(nm)
withn
the length of the list andm
the numberNamex
models. I was hoping that checking for .name first might speed things up.sydney-runkle commentedon Nov 22, 2024
You can use a tagged union for this case :)
https://docs.pydantic.dev/latest/concepts/unions/#discriminated-unions
BMMevius commentedon Dec 8, 2024
Thank you I believe this will work very well. Of course, I do have objects that are not parsed the same as my previous example (how can I forget?). They require 2 properties instead of 1, lets call them
name1
andname2
.name1
is not uniquely identifying the objects and neither isname2
but together they are. I searched the documentation for this case but I cant figure out what to do. I guess discriminated unions are not the way to go in that case?charleswhchan commentedon Jul 9, 2025
Any updates on this, checking to see if this might be added to pydantic v2 in the near future? (or possibly for consideration into v3), especially looking for
Thanks