Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for frozen dataclasses #2143

Open
Sheemap opened this issue Nov 2, 2024 · 3 comments
Open

Support for frozen dataclasses #2143

Sheemap opened this issue Nov 2, 2024 · 3 comments

Comments

@Sheemap
Copy link

Sheemap commented Nov 2, 2024

Is your feature request related to a problem? Please describe.
Not really a problem per say. But many of our current models are frozen dataclasses. We try to keep them immutable by default. But unfortunately theres not a flag for it when generating through this tool.

We will probably still explore generating these just as not frozen to start, so not really a blocker. But it would be awesome if we could bring that in soon.

Describe the solution you'd like
It seems like it would be straightforward to utilize the already available --enable-faux-immutability flag.

Describe alternatives you've considered
Could add another flag of --frozen-dataclasses or similar.

Additional context
Nothin that I can think of. If I have time I will try to get a PR up if desired :)

@Iddodo
Copy link

Iddodo commented Nov 5, 2024

My team could also use the addition of such feature (more in the context of Pydantic dataclasses, but I suspect that those would be related).

@JakeSummers
Copy link

Hey @Sheemap and @Iddodo ,

I am curious what you are trying to do.

When I generate PydanticV2 models, with the --enable-faux-immutability flag, I get frozen dataclasses:

class MyTHing(BaseModel):
    model_config = ConfigDict(
        extra="allow",
        frozen=True,
    )
    ...

@Sheemap
Copy link
Author

Sheemap commented Dec 16, 2024

Hi @JakeSummers, In this case specifically we're talking about python's dataclasses module. Which you can generate by passing --output-model-type dataclasses.dataclass. Which gives you this

@dataclass
class MyTHing:
    ...

Theres a frozen param you can pass to the decorator to lock all the fields, making it immutable. But datamodel-code-generator doesn't support this currently.

@dataclass(frozen=True)
class MyTHing:
    ...

This second one is what I'm specifically trying to accomplish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants