Skip to content

Date string coerced to datetime infers timezone #8762

@davidhewitt

Description

@davidhewitt

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

Split from #8736 (comment)

Coercion of date strings to midnight datetimes, new in v2.6, infers a UTC timezone.

This is inconsistent with coercion of datetime strings to datetimes, which do not infer a timezone; they only add one when explicitly given.

I believe quite strongly that timezone should not be inferred here, and this is a bug in the new date-to-midnight coercion.

Example Code

from pydantic import TypeAdapter
from datetime import datetime

print(repr(TypeAdapter(datetime).validate_python("2021-01-01").tzinfo))
#> TzInfo(UTC)  # <-- this should be None to be consistent with the case below

print(repr(TypeAdapter(datetime).validate_python("2021-01-01T00:00:00").tzinfo))
#> None

print(repr(TypeAdapter(datetime).validate_python("2021-01-01T00:00:00Z").tzinfo))
#> TzInfo(UTC)

print(repr(TypeAdapter(datetime).validate_python("2021-01-01T00:00:00+01:00").tzinfo))
#> TzInfo(+01:00)

Python, Pydantic & OS Version

pydantic version: 2.6.1
        pydantic-core version: 2.16.2
          pydantic-core build: profile=release pgo=true
                 install path: /home/david/.virtualenvs/pydantic-prod/lib/python3.11/site-packages/pydantic
               python version: 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0]
                     platform: Linux-5.15.133.1-microsoft-standard-WSL2-x86_64-with-glibc2.38
             related packages: mypy-1.4.1 pydantic-settings-2.1.0 email-validator-2.0.0.post2 pyright-1.1.316 typing_extensions-4.6.3
                       commit: unknown

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions