Giter Club home page Giter Club logo

Comments (5)

J-L0 avatar J-L0 commented on June 11, 2024 1

My assumption is that this tool will be used in applications such as API and Message Producers. In that case, let's say the schema contains uuid and fomat, and the received messages would be mapped to dataclass. In this case, you should have received a string, but the dataclass will ask for a uuid.UUID. Isn't this a little difficult to use?

I understand what you are saying, but I considered it quite inconvenient in actual use.

Your assumptions seems very reasonable to me. However, I also find it unintuitive to lose information that is contained in the OpenAPI Specification with the dataclass option when the library clearly is able to preserve it as seen in the Pydantic case.

For context, here is a use case that would greatly benefit from the more detailed type hint:

# %% Imports
from __future__ import annotations
from dataclasses import dataclass
from uuid import UUID
from mashumaro import DataClassDictMixin


# %% Generated code
@dataclass
class Input(DataClassDictMixin):
    id: UUID


# %% Usage
api_input_dict = {"id": "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"}

input_object = Input.from_dict(api_input_dict)

assert isinstance(input_object.id, UUID)  # True

As you rightly say, using dataclass without some means of converting a received string into the specified type would be inconvinient. mashumaro is able to derive the code necessary for that conversion from a dataclass definition with sufficiently detailed type hints and since datamodel-code-generator allows customization of the base class there already is a convinient way to integrate this.

About Mashumaro: "This library works by taking the schema of the data and generating a specific decoder and encoder for exactly that schema, taking into account the specifics of serialization format."

Thank you for considering this scenario.

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on June 11, 2024

@maddin1991

When i generate my models as dataclass the generater does not consider string formats like uuid, date ... The output is just of type string which is different when i generate pydantic models.

This is a very good question.
I envisioned taking data defined in a schema and importing or dumping it into an output data class.
Since the dataclass does not cast, I figured that just a string or number would be a better state to use!

However, what you say makes sense, and defining a type such as uuid is not a bad idea.
How about this being offered as a CLI option?

from datamodel-code-generator.

maddin1991 avatar maddin1991 commented on June 11, 2024

I'm not that deep into dataclasses to understand what you mean by that they dont cast but i would expect the same behaviour like when generating pydantic models. A CLI option would be great but in my opinion the expectance is that this option is enabled as default.

Could you take this apportunity to explain why there is a improved-datamodel-codegen which explicitly lists many string formats as implemented while this repo does not. Whats the reason to have this two versions in parallel, or is there even a difference? Because the differences are not clear to me from the docs.

Thank you and merry christmas! :)

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on June 11, 2024

@maddin1991

I'm not that deep into dataclasses to understand what you mean by that they dont cast but i would expect the same behaviour like when generating pydantic models. A CLI option would be great but in my opinion the expectance is that this option is enabled as default.

My assumption is that this tool will be used in applications such as API and Message Producers.
In that case, let's say the schema contains uuid and fomat, and the received messages would be mapped to dataclass. In this case, you should have received a string, but the dataclass will ask for a uuid.UUID.
Isn't this a little difficult to use?

I understand what you are saying, but I considered it quite inconvenient in actual use. However, I think we need a model output that reflects the format you are talking about. (whether as an option or default).

Could you take this apportunity to explain why there is a improved-datamodel-codegen which explicitly lists many string formats as implemented while this repo does not.

I don't know the repo 🤔 Who manages the pypi?

which explicitly lists many string formats as implemented

Could you show me the list?

from datamodel-code-generator.

koxudaxi avatar koxudaxi commented on June 11, 2024

@J-L0
I knew Mashumaro existed, but I didn't know they could do this. Thanks for letting me know.
Certainly, a strict type reflecting format is needed for dataclass as well in the way you presented it.
So we will add a new option (even if it means preventing destructive changes), let's also describe how to parse with Mashumaro in the documentation.

from datamodel-code-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.