-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
strip_whitespace
after coercion, but before constraint checks on custom data types
#6531
Comments
Not much simpler at the minute, but this tp.Annotated[str, Field(min_length=3, max_length=10), as_bv(constr(strip_whitespace=True))] I'm hoping the equivalent of import typing as tp
from pydantic import BeforeValidator, TypeAdapter, constr, Field
def as_bv(t: type) -> BeforeValidator:
"""Converts a type into a BeforeValidator"""
ta = TypeAdapter(t)
return BeforeValidator(lambda v: ta.validate_python(v))
MyT = tp.Annotated[str, Field(min_length=3, max_length=10), as_bv(constr(strip_whitespace=True))]
res = TypeAdapter(MyT).validate_python(" 123456789 ")
print(f"res: '{res}'")
|
This comment relates more to #6506 but I'm keeping it here since this is where I originally posted it. There are fundamental problems with this approach that will not allow us to include it in Pydantic directly. Practically speaking, we've worked very hard to make the entirety of validation happen within a single Rust call (with callbacks to Python when necessary, yes, but avoiding these as much as possible for performance reasons). Not only would inserting a call to a On a perhaps more philosophical note the metadata in That is not to say you can't use this utility function if it works for you, I just don't think it's as simple, universal, and cheap of a win as it seems on the surface. |
@lig why is this "not planned"? It's a step back from Pydantic V1. @adriangb suggested the easiest thing to do would probably be to just add back |
@prryplatypus At the moment solving this is too complicated as it would require some serious work in This is not a step back but a cost of significant performance improvement of Pydantic v2 comparing to Pydantic v1. That said, PRs are always welcome. In this particular case it's important that the change wouldn't affect the performance. |
#6951 maybe solves this |
Initial Checks
Description
Note: See #6469 for the full discussion.
In Pydantic V1, it was possible to do something as simple as this, which resulted in the following operations happening in this order:
However, in Pydantic V2 this case is particularly tricky because the
min_length
constraint gets applied after coercion but before the Python string is created, and hence before theAfterValidator
. So there is really no way to insert custom logic between these two steps.The closest one can get is by using something like this, but this is a lot of mental gymnastics for something that was trivial in V1.
@adriangb suggested the easiest thing to do would probably be to just add back
str_strip_whitespace
toField
or create some sort ofConStr()
marker for annotated (Annotated[str, ConStr(strip_whitespace=True), Field(max_length=10, ...)]
or something like that), and that is why I am creating this feature request.Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeSelected Assignee: @lig
The text was updated successfully, but these errors were encountered: