datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Add use of pendulum for date/datetime management

Open dreinon opened this issue 3 years ago • 8 comments
trafficstars

Is your feature request related to a problem? Please describe. I recently found pendulum and started using it as my default date and datetime management library since the standard python datetime module is not very rich.

Describe the solution you'd like It would be nice if we could choose either to use the standard datetime module or to use pendulum as our default library to type date/datetime properties.

I'm already using pendulum types in pydantic models I coded manually and so far so good! These types are pendulum.Date and pendulum.DateTime and pendulum.Time.

dreinon avatar Dec 01 '21 13:12 dreinon

@dreinon I'm sorry for my late reply. It's interesting. I will add the task to a TODO list. Thank you!!

koxudaxi avatar Jan 11 '22 17:01 koxudaxi

Outdated statements are present on pendulum github page: https://github.com/sdispater/pendulum/pull/556

It may be better idea to support Arrow, as it is currently maintained more than pendulum.

But it would be best to have support of Arrow directly in Pydantic first: https://github.com/samuelcolvin/pydantic/discussions/3721

karolzlot avatar May 07 '22 12:05 karolzlot

@karolzlot I agree with supporting arrow instead, but pendulum worked directly with pydantic. Do you think arrow wouldn't?

dreinon avatar May 07 '22 12:05 dreinon

@dreinon It works ok, but not as well as built-in datetime. Example issue I had: https://stackoverflow.com/questions/68930298/how-to-use-arrow-type-in-fastapi-response-schema (proposed workaround works, but it's a hack and it removes Arrow from pydantic model)

The issue above may not matter for datamodel-code-generator however.

karolzlot avatar May 07 '22 12:05 karolzlot

@karolzlot Right, but looking at the pydantic codebase, I think this is more of a job Arrow needs to do instead of pydantic itself.

The following two lines explain that they only define validators and schema for standard library types. That's why pendulum worked out of the blue, since pendulum is a superset of the date-related standard library types.

In order to make Arrow pydantic-compatible, the following page from the pydantic docs can be followed, that says that any class can be made pydantic compatible by setting a couple methods for validation and schema generation

Hope this is helpful! Thank you 🙂

dreinon avatar May 07 '22 13:05 dreinon

In order to make Arrow pydantic-compatible, the following page from the pydantic docs can be followed, that says that any class can be made pydantic compatible by setting a couple methods for validation and schema generation

@dreinon I think it won't be enough. What is needed is discussed in this issue: https://github.com/samuelcolvin/pydantic/issues/951

Pydantic just doesn't have all needed features yet, although various workarounds are possible.

karolzlot avatar May 08 '22 13:05 karolzlot

Personally I have tried both Arrow (even with small contribution) and pendulum.

My personal preference is pendulum and it works great to me for years.

Today I have found pendulum even supports time intervals/periods.

vlcinsky avatar May 04 '23 14:05 vlcinsky

Not sure if anyone is aware of this, but pydantic-extra-types exists so Pydantic already "supports" pendulum.

Woody1193 avatar Apr 03 '24 05:04 Woody1193