-
-
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
StringConstraints: Bug #9381
Comments
The email validation process relies on the email_validator package. This validate_email function of the package returns an object with a normalized form of the email address, in which domain is in lowercase. Line 742 in b844ea7
|
It's StringConstraints issue after EmailStr validation it has to apply lower/upper case as required but strip_whitespace doesn't make any other parameters work in StringContraints. |
Another example that might be relevant
Result
|
Thanks for reporting this in a separate issue. I've added this to our upcoming milestone! |
from typing import Annotated
from pydantic import BaseModel, EmailStr
from pydantic.experimental.pipeline import validate_as
EStr = Annotated[EmailStr, validate_as(str).str_strip().validate_as(...).str_lower()]
class Foo(BaseModel):
bar: EStr
print(repr(Foo(bar="uSeR@ExAmPlE.com")))
# Foo(bar='user@example.com') |
Nice! In the future I'd hope we can get Does this end up with a string or an |
That produces a string, but This probably makes the most sense contextually: validate_as(str).str_strip().str_lower().validate_as(...) |
Interesting case showcasing the bug: from pydantic import BaseModel, EmailStr, StringConstraints
from typing import Annotated
class Foo(BaseModel):
foo: EmailStr
bar: Annotated[EmailStr, StringConstraints(to_lower=True)]
baz: Annotated[EmailStr, StringConstraints(to_lower=True, strip_whitespace=True)]
# foo: NEmail
print(repr(
Foo(
foo=" uSeR@ExAmPlE.com ",
bar=" uSeR@ExAmPlE.com ",
baz=" uSeR@ExAmPlE.com "
)
))
#> Foo(foo='uSeR@example.com', bar='user@example.com', baz='uSeR@example.com') |
Aha, the culprit! You can see in the core schema for pretty_print_core_schema(Foo.__pydantic_core_schema__)
"""
{
'type': 'model',
'cls': <class '__main__.Foo'>,
'schema': {
'type': 'model-fields',
'fields': {
'foo': {
'type': 'model-field',
'schema': {
'function': {'type': 'no-info', 'function': <bound method EmailStr._validate of <class 'pydantic.networks.EmailStr'>>},
'schema': {'type': 'str'},
'type': 'function-after'
}
},
'bar': {
'type': 'model-field',
'schema': {
'type': 'chain',
'steps': [
{
'function': {'type': 'no-info', 'function': <bound method EmailStr._validate of <class 'pydantic.networks.EmailStr'>>},
'schema': {'type': 'str'},
'type': 'function-after'
},
{'type': 'str', 'to_lower': True}
]
}
},
'baz': {
'type': 'model-field',
'schema': {
'type': 'chain',
'steps': [
{
'function': {'type': 'no-info', 'function': <bound method EmailStr._validate of <class 'pydantic.networks.EmailStr'>>},
'schema': {'type': 'str'},
'type': 'function-after'
},
{'type': 'str', 'strip_whitespace': True}
]
}
}
},
'model_name': 'Foo'
},
'ref': '__main__.Foo:4822756464'
}
""" |
I've opened a PR that fixes this issue. Specifically, the first commit is where the fix is :). |
see below, lowercase/uppercase is set to true and strip whitespaces is set to true. Interestingly, we see the second part of email is always lowercase. This is very strange behavior. Though, I can exclude strip_whitespace as EmailStr takes care of striping whitespaces.
I'm bit confused when to use and not to use
strip_whitespace
. Should we usestrip_whitespace
with others params?Originally posted by @SDAravind in #8577 (comment)
The text was updated successfully, but these errors were encountered: