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
Comments
Agreed, we will implement support for this in |
Just added support for this, will be released in |
Thanks a lot! Will make some of my fast api url's a bit cleaner 🙂 |
Hey, what's the best way to disable this new feature? We do not want to allow |
You can use strict mode on that 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
""" |
@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 |
Feel free to open another issue and we can continue discussion there! |
Initial Checks
Description
I think pydantic is too strict here, especially since datetime.fromisoformat works without trouble.
Affected Components
.model_dump()
and.model_dump_json()
model_construct()
, pickling, private attributes, ORM modeThe text was updated successfully, but these errors were encountered: