Skip to content
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

Support pydantic.dataclasses as output format #1906

Open
AltarBeastiful opened this issue Apr 8, 2024 · 0 comments
Open

Support pydantic.dataclasses as output format #1906

AltarBeastiful opened this issue Apr 8, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@AltarBeastiful
Copy link

AltarBeastiful commented Apr 8, 2024

In some projects, I use pydantic.dataclasses to add validation to dataclass models.
This is useful on project that requires dataclasses but still want to use pydantic for validation. All fields and type annotation of pydantic are compatible and can be generated in the same way.

We could easily support this as there is only 2 changes with pydantic.BaseModel:

  • the class definition :
from pydantic import BaseModel

class MyModel(BaseModel):
    id: str

to

from pydantic.dataclasses import dataclass

@dataclass
class MyModel:
    id: str
  • Ordering of default value fields :
    With dataclasses, default value fields must be written after fields with no default value.
    Using BaseModel generation seems to generate fields in definition order without any ordering.
@dataclass
class MyModel:
    id: str
    description: str = None # This field needs to be after id field
@AltarBeastiful AltarBeastiful changed the title Support pydantic.dataclasses Support pydantic.dataclasses as output format Apr 8, 2024
@koxudaxi koxudaxi added the enhancement New feature or request label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants