datamodel-code-generator
datamodel-code-generator copied to clipboard
Dataclasses.dataclass Support?
Is your feature request related to a problem? Please describe. I'm always frustrated when I have to install new dependencies when the feature is already in the standard lib. Pydantic and mypy are both competing to do validations. I'd like to just leave it to mypy.
Describe the solution you'd like
datamodel-codegen --input schema.json --input-file-type jsonschema --decorator-class dataclasses.dataclass --output test.py
Describe alternatives you've considered
datamodel-codegen --input schema.json --input-file-type jsonschema --base-class dataclasses.dataclass --output test.py
See: https://github.com/koxudaxi/pydantic-pycharm-plugin/issues/266
@iot-resister Thank you for creating this issue. This code generator is designed that we can add another output type.
But, dataclasses.dataclass
don't cover all-case.
For example, allOf
will be an inheritance model. dataclass doesn't support inheritance.
However, some may users want it for using simple schemas. TypeDict may good too.
I hope to hear the user's comments.
@koxudaxi
I wouldn't even mind manually editing allOf
for those cases when I'm consuming an API with that.
TypeDict would be great! I hope people chime in!
There is already a dataclass template https://github.com/koxudaxi/datamodel-code-generator/blob/7fa641f9694afeac7dd5a6164df2e12270f580a4/datamodel_code_generator/model/template/pydantic/dataclass.jinja2 Unfortunately, I wasn't able to figure out how to use it with the command line. Would be awesome if @koxudaxi could provide some guidance.
In general, I think TypedDict and attrs support would be great additions. I personally use the library to keep a typescript and a python library in sync so support of all jsonschema features is not necessary for me.
@koxudaxi Dear Koudai-san, you wrote above:
But,
dataclasses.dataclass
don't cover all-case. For example,allOf
will be an inheritance model. dataclass doesn't support inheritance.
I think that dataclass (and attrs that dataclass was derived from) now both support subclassing ... but I am not sure if this what you meant by inheritance?
This is with Python 3.9
>>> from dataclasses import dataclass
>>> @dataclass
... class Foo:
... bar: str = None
...
>>> @dataclass
... class Bar(Foo):
... baz: str = None
...
>>> Foo(bar='a')
Foo(bar='a')
>>> Bar(bar='a', baz='2')
Bar(bar='a', baz='2')
>>> isinstance(Bar(bar='a', baz='2'), Foo)
True
@pombredanne I didn't know the behavior of dataclass :sweat_smile: I will implement the dataclass feature. Thank you
@pombredanne
I found another problem.
Do you know how can we represent of custom-root-types model with @dataclass
?
The generator often generate the root model.
from pydantic import BaseModel
class Pet(BaseModel):
name: str
class Pets(BaseModel):
__root__: list[Pet]
pets = Pets.parse_obj([{'name': 'dog'}, {'name': 'cat'}])
print(f'{pets=}')
# pets=Pets(__root__=[Pet(name='dog'), Pet(name='cat')])
print(f'{pets.dict()=}')
# pets.dict()={'__root__': [{'name': 'dog'}, {'name': 'cat'}]}
I have released a new version 0.17.1
The version has an option --output-model-type dataclasses.dataclass
Thank you very much!!
Hello @koxudaxi,
I have an issue with dataclass models with Optional fields. The generator generates invalid code like this:
@dataclass
class Entry:
foo: str
bar: Optional[str] = None
spam: str
This is wrong code for dataclasses. Is there a workaround?
@espdev
Thank you for commenting the issue.
I have released the PR as 0.19.0.