datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Dataclasses.dataclass Support?

Open iot-resister opened this issue 3 years ago • 3 comments

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 avatar Apr 05 '21 17:04 iot-resister

@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 avatar Apr 06 '21 03:04 koxudaxi

@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!

iot-resister avatar Apr 06 '21 19:04 iot-resister

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.

JarnoRFB avatar Dec 20 '21 14:12 JarnoRFB

@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 avatar Jan 26 '23 11:01 pombredanne

@pombredanne I didn't know the behavior of dataclass :sweat_smile: I will implement the dataclass feature. Thank you

koxudaxi avatar Jan 26 '23 15:01 koxudaxi

@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'}]}

koxudaxi avatar Jan 27 '23 00:01 koxudaxi

I have released a new version 0.17.1 The version has an option --output-model-type dataclasses.dataclass Thank you very much!!

koxudaxi avatar Feb 07 '23 11:02 koxudaxi

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 avatar Feb 20 '23 19:02 espdev

@espdev
Thank you for commenting the issue. I have released the PR as 0.19.0.

koxudaxi avatar May 11 '23 15:05 koxudaxi