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

input is too short validating a datetime when month and day are not zero padded, but dash delimited #9308

Closed
1 task done
akrherz opened this issue Apr 23, 2024 · 2 comments
Assignees
Labels
Change Suggested alteration to pydantic, not a new feature nor a bug V3 Under consideration for V3

Comments

@akrherz
Copy link

akrherz commented Apr 23, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

IMHO, the provision of a date with dashes and without zero padded month, day values, is valid. python strptime allows this. I agree that this is ambiguous when no dashes are provided.

Example Code

from datetime import datetime
from pydantic import BaseModel, Field

class MyModel(BaseModel):
    dt: datetime = Field(...)

print(datetime.strptime("2024-4-5", "%Y-%m-%d"))
print(MyModel(dt="2024-4-5"))
2024-04-05 00:00:00
Traceback (most recent call last):
  File "/tmp/example.py", line 10, in <module>
    print(MyModel(dt="2024-4-5"))
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/miniconda3/envs/prod/lib/python3.11/site-packages/pydantic/main.py", line 171, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 1 validation error for MyModel
dt
  Input should be a valid datetime or date, input is too short [type=datetime_from_date_parsing, input_value='2024-4-5', input_type=str]
    For further information visit https://errors.pydantic.dev/2.6/v/datetime_from_date_parsing

Python, Pydantic & OS Version

pydantic version: 2.6.4
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=false
                 install path: /opt/miniconda3/envs/prod/lib/python3.11/site-packages/pydantic
               python version: 3.11.9 | packaged by conda-forge | (main, Apr 19 2024, 18:36:13) [GCC 12.3.0]
                     platform: Linux-5.14.0-437.el9.x86_64-x86_64-with-glibc2.34
             related packages: typing_extensions-4.11.0 fastapi-0.110.2
                       commit: unknown

Note, I am stuck on an old version awaiting a release with #9227 fixed. My apologies if this issue does not exist with the current release :(

@akrherz akrherz added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 23, 2024
@akrherz
Copy link
Author

akrherz commented Apr 25, 2024

Just denoting that this issue still exists on 2.7.1 as I can upgrade to it now :)

@sydney-runkle
Copy link
Member

@akrherz,

Thanks for the report! Just to clarify - I believe #9227 has been resolved with 2.7.1 - please open a new issue if you're still experiencing problems in that regard!

Regarding your proposed change, I think that would have to wait until V3. Our docs do explicitly state that said type is not allowed, even in lax mode: https://docs.pydantic.dev/latest/api/standard_library_types/#datetimedatetime.

Closing for now, as I don't think this is a change we're likely to make, given that the desired behavior can easily be achieved with a custom validator. That being said, if this issue gets a bunch of traction / support, we could certainly consider for V3.

@sydney-runkle sydney-runkle added Change Suggested alteration to pydantic, not a new feature nor a bug V3 Under consideration for V3 and removed bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Apr 28, 2024
@sydney-runkle sydney-runkle self-assigned this Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Change Suggested alteration to pydantic, not a new feature nor a bug V3 Under consideration for V3
Projects
None yet
Development

No branches or pull requests

2 participants