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

Pydantic should parse simple yyyy-MM-dd for datetime #8363

Closed
3 of 13 tasks
aersam opened this issue Dec 13, 2023 · 7 comments · Fixed by pydantic/pydantic-core#1124
Closed
3 of 13 tasks

Pydantic should parse simple yyyy-MM-dd for datetime #8363

aersam opened this issue Dec 13, 2023 · 7 comments · Fixed by pydantic/pydantic-core#1124
Assignees
Milestone

Comments

@aersam
Copy link

aersam commented Dec 13, 2023

Initial Checks

  • I have searched Google & GitHub for similar requests and couldn't find anything
  • I have read and followed the docs and still think this feature is missing

Description

from pydantic import BaseModel
from datetime import datetime
import json

datetime.fromisoformat("2021-01-01")  # this works fine


class Model(BaseModel):
    dt: datetime


Model.model_validate_json(json.dumps({"dt": "2021-01-01T00:00"})) # works, of course
Model.model_validate_json(json.dumps({"dt": "2021-01-01"})) # fails

I think pydantic is too strict here, especially since datetime.fromisoformat works without trouble.

Affected Components

@sydney-runkle
Copy link
Member

@aersam,

Agreed, we will implement support for this in pydantic-core :).

@sydney-runkle
Copy link
Member

@aersam,

Just added support for this, will be released in 2.6.0 :)

@aersam
Copy link
Author

aersam commented Dec 19, 2023

Thanks a lot! Will make some of my fast api url's a bit cleaner 🙂

@miikka
Copy link

miikka commented Jan 30, 2024

Hey, what's the best way to disable this new feature? We do not want to allow yyyy-MM-dd for datetimes.

@sydney-runkle
Copy link
Member

@miikka,

You can use strict mode on that datetime type (or at the model level):

from datetime import datetime, timezone
from typing import Annotated

from pydantic import BaseModel, Strict, ValidationError

StrictDatetime = Annotated[datetime, Strict()]

class Model(BaseModel):
    dt_strict: StrictDatetime
    dt_lax: datetime

try:
   m = Model(dt_strict='2021-01-01', dt_lax='2021-01-01')
   print(repr(m))
except ValidationError as e:
    print(e)
    """
    1 validation error for Model
    dt_strict
    Input should be a valid datetime [type=datetime_type, input_value='2021-01-01', input_type=str]
        For further information visit https://errors.pydantic.dev/2.6/v/datetime_type
    """

@miikka
Copy link

miikka commented Feb 2, 2024

@sydney-runkle Thanks, that's neat! Unfortunately it turns out that doesn't help with FastAPI's path parameters coercion (which uses Pydantic under the hood with TypeAdapter.validate_python which does not accept strings at all for strict mode datetimes) but maybe that's better discussed somewhere else than in this issue.

@sydney-runkle
Copy link
Member

@miikka,

Feel free to open another issue and we can continue discussion there!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants