You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
At the moment the Iterable[T] type validates inputs by calling iter() on them and wrapping the result of that in a ValidatorIterator type which validates elements lazily.
For example, in the following code, a.elements is a wrapper around iter([1, 2, 3]):
fromcollections.abcimportIterablefrompydanticimportBaseModelclassA(BaseModel):
elements: Iterable[int]
# elements is a ValidatorIteratora=A(elements=[1, 2, 3])
This has a bunch of oddities:
The ValidatorIterator wrapper around iter([1, 2, 3]) is more like Iterator[int] than Iterable[int], because it can only be consumed once. Probably the ValidatorIterator wrapper should just wrap [1, 2, 3], so that it can be consumed multiple times. The problem with this is that the current lazy validation would mean validating on every consumption, potentially with a lot of additional work.
Because of the lazy validation, the ValidatorIterator type is a relatively heavy object which carries a lot of state related to validation - e.g. to avoid recursion loops etc.
Most users expect eager validation. Lazy validation is crucial for some users, e.g. fix memory leak with iterable validation pydantic-core#1271 (comment). However I think lazy validation should be clearly opted in to with a very obvious API choice rather than arbitrarily stuffed on the Iterable[T] type. The complication here is generators, which cannot be eagerly validated.
The text was updated successfully, but these errors were encountered:
Consider e.g. a = A(elements=["abc"]) - this would not raise a ValidationError until calling next(a.elements), which is a good example of how lazy validation is both powerful but surprising (credit @alexmojaki)
At the moment the
Iterable[T]
type validates inputs by callingiter()
on them and wrapping the result of that in aValidatorIterator
type which validates elements lazily.For example, in the following code,
a.elements
is a wrapper arounditer([1, 2, 3])
:This has a bunch of oddities:
ValidatorIterator
wrapper arounditer([1, 2, 3])
is more likeIterator[int]
thanIterable[int]
, because it can only be consumed once. Probably theValidatorIterator
wrapper should just wrap[1, 2, 3]
, so that it can be consumed multiple times. The problem with this is that the current lazy validation would mean validating on every consumption, potentially with a lot of additional work.ValidatorIterator
type is a relatively heavy object which carries a lot of state related to validation - e.g. to avoid recursion loops etc.Iterable[T]
type. The complication here is generators, which cannot be eagerly validated.The text was updated successfully, but these errors were encountered: