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 · 4 comments
Open

Support for frozen dataclasses #2143

Sheemap opened this issue Nov 2, 2024 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers

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

@koxudaxi koxudaxi added enhancement New feature or request good first issue Good for newcomers labels Jan 13, 2025
@koxudaxi
Copy link
Owner

@Sheemap
Thank you for bringing this up! I completely agree that having frozen dataclasses would be really useful. We have many users who prefer immutable data structures, so this would be a great addition. When you have time, feel free to submit a PR - either leveraging the existing flag or adding a new one. I'd be happy to review and help get it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants