datamodel-code-generator
datamodel-code-generator copied to clipboard
Use default keyword argument in generated optional fields
Is your feature request related to a problem? Please describe.
The generator produces code that is not compatible with PEP 681's dataclass_transform interface. For more details see
https://github.com/microsoft/pyright/issues/7166
Describe the solution you'd like
I would like the generator to use the default= keyword argument instead of using the positional argument.
Describe alternatives you've considered
I can work around it by ignoring the pyright lint errors or by explicitly adding optional fields.
Additional context
Example spec:
type: object
properties:
fieldWithNoDescription:
type: string
fieldWithDescription:
type: string
description: text describing the field
Produces:
from typing import Optional
from pydantic import BaseModel, Field
class ModelWithOptionalFields(BaseModel):
fieldWithNoDescription: Optional[str] = None
fieldWithDescription: Optional[str] = Field(
None,
description="Optional field with a default value of None and a description",
)
But I would like it to produce:
from typing import Optional
from pydantic import BaseModel, Field
class ModelWithOptionalFields(BaseModel):
fieldWithNoDescription: Optional[str] = None
fieldWithDescription: Optional[str] = Field(
default=None,
description="Optional field with a default value of None and a description",
)