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

StringConstraints: Bug #9381

Open
SDAravind opened this issue May 3, 2024 · 4 comments
Open

StringConstraints: Bug #9381

SDAravind opened this issue May 3, 2024 · 4 comments
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@SDAravind
Copy link

I see the argument for changing it, but it's a very annoying breaking change to understand, so my instinct is we don't change this, even in a major update.

As for the reason - my guess is that the best explanation is "because that's how it was first done" and there hasn't been a strong (enough) argument to change it.

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 use strip_whitespace with others params?

from pydantic import BaseModel, EmailStr, StringConstraints
from typing import Annotated

EStr = Annotated[EmailStr, StringConstraints(to_lower=True, strip_whitespace=True)]


class Foo(BaseModel):
    bar: EStr


Foo(bar="[email protected]")
>>>  Foo(bar='[email protected]')

Originally posted by @SDAravind in #8577 (comment)

@RajatRajdeep
Copy link

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.

parts = email_validator.validate_email(email, check_deliverability=False)

@SDAravind
Copy link
Author

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.

@austinyu
Copy link

austinyu commented May 9, 2024

Another example that might be relevant

from pydantic import BaseModel, StringConstraints, NameEmail 
from typing import Annotated

EStr = Annotated[StringConstraints(to_lower=True), NameEmail]

class User(BaseModel):
    email: EStr

user = User(email='Fred Bloggs <[email protected]>')
print(user)

Result

# Expected
email=NameEmail(name='Fred Bloggs', email='[email protected]')
# Actual
email=NameEmail(name='Fred Bloggs', email='[email protected]')

@sydney-runkle sydney-runkle added the bug V2 Bug related to Pydantic V2 label May 14, 2024
@sydney-runkle sydney-runkle added this to the 2.7 fixes milestone May 16, 2024
@sydney-runkle
Copy link
Member

@SDAravind,

Thanks for reporting this in a separate issue. I've added this to our upcoming milestone!

@sydney-runkle sydney-runkle modified the milestones: 2.7 fixes, v2.8.0 May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V2 Bug related to Pydantic V2
Projects
None yet
Development

No branches or pull requests

4 participants