Comments (5)
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.
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.
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.
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.
@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)
- Nullable fields from openapi not translated into optional fields for TypedDict HOT 3
- Two variations of syntaxes for defining dictionaries/free-form objects give different results HOT 1
- allOf ignores the $ref option if it contains enum HOT 3
- String enum with "YES/NO/NOT_APPLICABLE" being converted to "TRUE/FALSE/'NOT_APPLICABLE'" HOT 1
- Pydantic discriminator generation does not take alias mapping into account. HOT 1
- Allow discriminator field literals to be set to enum values HOT 1
- `--use-title-as-name` does not allow multi-module Python output.
- OpenAPI with oneOf prevents direct access to generated model's fields HOT 3
- Bug with oneOf and const HOT 1
- Property name in schema is the same as imported module name, causing RecursionError in pydantic HOT 1
- Default value when using ref HOT 2
- Option '--url' behaves differently from '--input' with same input file
- sqlmodel.SQLModel support HOT 1
- Cannot override a $ref field to make it required
- code generat fail, if json contain specific chinese HOT 2
- Reference to model that yields name collision results in `import as` and erroneous field access
- Serialization of subclasses changed in pydantic v2
- Backslash in docstrings are not escaped
- Generation of confloat warnings HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from datamodel-code-generator.