-
-
Notifications
You must be signed in to change notification settings - Fork 329
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
dataclass generator improvements #2102
dataclass generator improvements #2102
Conversation
CodSpeed Performance ReportMerging #2102 will not alter performanceComparing Summary
|
88014ad
to
19debe8
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2102 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 38 38
Lines 4222 4250 +28
Branches 979 984 +5
=========================================
+ Hits 4222 4250 +28
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
d092c64
to
604c0c7
Compare
@anis-campos Since this change alters both the generated code and its behavior, could you please implement the kw_only feature as a new CLI option? I would appreciate it if you could address these points. Thank you for your contribution, and I look forward to reviewing the updated version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left the comment.
e81eefa
to
c29da70
Compare
@koxudaxi I added a cli flag, let me know if the naming is right and if I could have changed less signatures with a different strategy to pass the info down to the model template. |
@koxudaxi I'm also adding the support for |
783fb89
to
2e07b26
Compare
Allow dataclass models to be properly generated with discriminator field
Thanks to keyword only, dataclass models can use inheritance and no have issues with default values
2e07b26
to
9984613
Compare
9984613
to
16fa487
Compare
applying `--output-datetime-class` from koxudaxi#2100 to dataclass to map date, time and date time to the python `datetime` objects instead of strings.
16fa487
to
4d67a81
Compare
thanks @meliache , I appreciate your comment. I don't think it is linked, the issues is referencing pyantic output, I'm reworking only the dataclass generator. side note, I would close the issue you linked, executing the example I see no issue with the pydantic output I have the default values just fine # generated by datamodel-codegen:
# filename: openapi.yaml
# timestamp: 2024-10-15T09:02:19+00:00
from __future__ import annotations
from typing import Literal, Union
from pydantic import BaseModel, Field, RootModel
class StartEvent(BaseModel):
type: Literal['start'] = 'start'
class EndEvent(BaseModel):
type: Literal['end'] = 'end'
class ResponseEvent(RootModel[Union[StartEvent, EndEvent]]):
root: Union[StartEvent, EndEvent] = Field(..., discriminator='type') |
You are right, the linked issue seems to be already resolved in v0.26.1. In my project I had been using v0.25.9 and I had seen the same issue. In the release notes to v0.26.0 and v0.26.1 I saw nothing that indicated that explicitly indicated that the issue might be fixed. In this PR you at least mentioned that you worked on discriminator support, so on a whim I decided to try this branch, but then I tried v0.26.1 and there it also worked already. |
Use apply_discriminator_type for dataclasses
Allow dataclass models to be properly generated with discriminator field
Fix dataclass inheritance
Thanks to keyword only, dataclass models can use inheritance and no have issues with default values
Support datetime types in dataclass fields
applying
--output-datetime-class
from #2100 to dataclass to map date, time and date time to the pythondatetime
objects instead of strings.fixes #1966