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

Validation of Iterable[T] might want revisiting in V3 #9266

Open
davidhewitt opened this issue Apr 17, 2024 · 1 comment
Open

Validation of Iterable[T] might want revisiting in V3 #9266

davidhewitt opened this issue Apr 17, 2024 · 1 comment
Labels
V3 Under consideration for V3

Comments

@davidhewitt
Copy link
Contributor

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]):

from collections.abc import Iterable
from pydantic import BaseModel

class A(BaseModel):
    elements: Iterable[int]

# elements is a ValidatorIterator
a = 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.
@davidhewitt davidhewitt added the V3 Under consideration for V3 label Apr 17, 2024
@davidhewitt
Copy link
Contributor Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
V3 Under consideration for V3
Projects
None yet
Development

No branches or pull requests

1 participant