pydantic / pydantic-extra-types

Extra Pydantic types.
MIT License
181 stars 48 forks source link

pendulum DateTime type doesn't accept python standard datetime #139

Closed antti-ngp closed 3 months ago

antti-ngp commented 7 months ago

Glad the library includes the pendulum DateTime now. I was just starting to use it but noticed it doesn't accept the standard python datetime objects as expected.

Steps to reproduce:

from pydantic_extra_types.pendulum_dt import DateTime
import datetime
import pydantic

class MyTest(pydantic.BaseModel):
    my_pendulum_dt: DateTime

MyTest(my_pendulum_dt=datetime.datetime(2024,2,8,0,0,1))

Expected results:

MyTest(my_pendulum_dt=DateTime(2024, 2, 8, 0, 0, 1))

Actual results:

pydantic_core._pydantic_core.ValidationError: 1 validation error for MyTest
my_pendulum_dt
  value is not a valid timestamp [type=value_error, input_value=datetime.datetime(2024, 2, 8, 0, 0, 1), input_type=datetime]

Suggested fix: Use pendulum.instance() for input of the type datetime.datetime before using pendulum.parse() for other types of input.

07pepa commented 3 months ago

@yezz123 this can be closed already present here with test and merged https://github.com/pydantic/pydantic-extra-types/pull/176