-
-
Notifications
You must be signed in to change notification settings - Fork 322
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
with pydantic v2, use pydantic.AwareDatetime instead of datetime #1735
Conversation
Openapi spec says that type string with format "date-time" must have a timezone. Therefore, we need pydantic.AwareDatetime here.
CodSpeed Performance ReportMerging #1735 will not alter performanceComparing Summary
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1735 +/- ##
=======================================
Coverage ? 99.05%
=======================================
Files ? 37
Lines ? 4112
Branches ? 954
=======================================
Hits ? 4073
Misses ? 25
Partials ? 14
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
@gjcarneiro |
Yes, it certainly does:
(time-offset is the timezone) I discovered this from validating my requests (with response coming from openapispec -> datamodel-codgen -> pydantic -> model_dump_json()) using the python package openapi-core. If you don't provide a timezone, it doesn't validate. I guess there is a workaround without this PR: in pydantic, the datetime type accepts both timezone aware and naive datetime objects. As long as you always provide a datetime aware object, all is good. But it is more precise, and detects the issue earlier, to use |
Thank you for explaining it. |
The issue is fixed in the latest version. |
Hi! This broke quite a few of our use-cases. Would it be possible to make this configurable, so one could toggle with a parameter between NaiveDatetime and AwareDatetime? @koxudaxi ? |
Any news on this topic? thanks |
Note that |
I agree, but this is a pydantic tool, and pydantic provides for multiple types of data fields, forcing only one type into the code is not a correct approach, so in this case I am not arguing the correctness of the alignment to the RFC, but the hasty implementation.. |
I think that, in order to remain compatible with the spec, then the format specifier would have to be something other than |
yes, but in this case pydantic provides differents data types, so they should be respected especially in case they are generated by a json schema , maybe generated by pydantic itself. In any case I think it is a good approach to add an option to the cli arguments, with which to drive to the given type of parser. For example: case 1 case 2 |
Yes, I know pydantic provides different data types. But, maybe I'm missing something, but the OpenAPI and JSONSchema inputs require the use of timezone-aware datetimes. Pydantic is more generic than OpenAPI/JSONSchema. And pydantic is the output format, it is never the input. Unless I'm missing something and there is an input file api spec format that allows for timezone-naive datetime that datamodel-code-generator is supposed to support. I only ever use it with OpenAPI, and OpenAPI is pretty clear: timezone is not optional. That said, if having such an option helps during a transition period to allow for migration of existing users, I don't think that's terrible. As long as it is clearly marked as a deprecated option and supported only for a few years until users migrate. In the long term, it makes little sense to deviate from OpenAPI / JSONSchema standards IMHO. Disclaimer: I am not the maintainer, I just contributed this one PR. And apologies for breaking your code 😬 |
Our fundamental problem is that we do not have timezones in the original data we are consuming, standards be damned. :-) This breaks multiple projects and adds nothing of value for a data-consumer, only for a data-producer. I.e. Ideally I would like to be able to on the producer end enforce timezones, and on the consumer end accept whatever the server provides. Refusing to decode data provided by a non-standards compliant server just isn't useful. :-/ |
Yes, it is. There may need to be options for multiple use cases. Thank you all for the discussion. Would it be helpful to save everyone some time if there was a description of the behavior of the options in the documentation? |
@koxudaxi setting aside the OpenApi specification, would you be open for a PR that would make this configurable? |
Openapi spec says that type string with format "date-time" must have a timezone. Therefore, we need pydantic.AwareDatetime here.