koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.6k stars 290 forks source link

Add use of pendulum for date/datetime management #643

Open dreinon opened 2 years ago

dreinon commented 2 years ago

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.

Funding

Fund with Polar

koxudaxi commented 2 years ago

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

karolzlot commented 2 years ago

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

dreinon commented 2 years ago

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

karolzlot commented 2 years ago

@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.

dreinon commented 2 years ago

@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 🙂

karolzlot commented 2 years ago

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.

vlcinsky commented 1 year ago

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.

Woody1193 commented 5 months ago

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