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

No ValidationError anymore when using 'yyyy-MM-DD' string with AwareDatetime type #8867

Closed
1 task done
davidhuser opened this issue Feb 21, 2024 · 5 comments
Closed
1 task done
Labels
bug V2 Bug related to Pydantic V2
Milestone

Comments

@davidhuser
Copy link

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

In pydantic 2.6.0 the support for yyyy-MM-DD string for datetimes was added (issue + PR).

When using AwareDatetime, no ValidationError is raised anymore in 2.6.0+. However, since no Timezone information is attached to a yyyy-MM-DD string, I would expect a ValidationError to be raised.

Potential workaround: As mentioned in this issue a StrictDatetime could be possible, if so, how would a StrictUTCDatetime type work?

Example Code

from datetime import timezone
from typing import Annotated

from pydantic import AfterValidator, AwareDatetime, BaseModel, ValidationError


def validate_utc(dt: AwareDatetime | None) -> AwareDatetime | None:
    """Validate that the pydantic.AwareDatetime is in UTC."""
    if dt:
        if dt.tzinfo.utcoffset(dt) != timezone.utc.utcoffset(dt):
            raise ValueError('Timezone must be UTC')
    return dt


DatetimeUTC = Annotated[AwareDatetime, AfterValidator(validate_utc)]


class UTCModel(BaseModel):
    dt_aware: DatetimeUTC


if __name__ == '__main__':
    try:
        m = UTCModel(dt_aware='2023-01-01')
        print(repr(m))
    except ValidationError as e:
        print(e)
    """
    UTCModel(dt_aware=datetime.datetime(2023, 1, 1, 0, 0, tzinfo=TzInfo(UTC)))
    """

Python, Pydantic & OS Version

poetry run python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.6.1
        pydantic-core version: 2.16.2
          pydantic-core build: profile=release pgo=true
                 install path: /Users/xxx/Library/Caches/pypoetry/virtualenvs/myrepo-fwpETFsa-py3.11/lib/python3.11/site-packages/pydantic
               python version: 3.11.6 (main, Nov  2 2023, 04:39:43) [Clang 14.0.3 (clang-1403.0.22.14.1)]
                     platform: macOS-14.3.1-arm64-arm-64bit
             related packages: typing_extensions-4.9.0 fastapi-0.109.2 mypy-1.8.0 email-validator-2.1.0.post1 pydantic-settings-2.2.1
                       commit: unknown
@davidhuser davidhuser added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 21, 2024
@davidhuser
Copy link
Author

One solution could be as per this comment

class UTCModel(BaseModel):
    dt_aware: DatetimeUTC = Field(..., strict=True)

however, all the previously lax and valid formats will now fail as well, e.g.

input_data = datetime.now(timezone.utc).isoformat()

@sydney-runkle
Copy link
Member

@davidhuser,

Thanks for reporting this. I'm adding this to the 2.6.2 milestone! Will keep you updated as I work on a fix.

@sydney-runkle sydney-runkle removed the pending Awaiting a response / confirmation label Feb 21, 2024
@sydney-runkle sydney-runkle added this to the v2.6.2 milestone Feb 21, 2024
@sydney-runkle
Copy link
Member

Ah, so this was actually fixed by my fix for this: #8867.

Going to mark this as resolved, and will work on the 2.6.2 release now!

@davidhuser
Copy link
Author

thanks for looking into it. Your link self-references to this issue, out of curiosity where was the fix implemented?

@sydney-runkle
Copy link
Member

Ah, meant to link to this issue: #8762. Should be out now with 2.6.2!

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

2 participants