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

Combine @dataclass and @dataslots into one decorator? #14

Closed
TylerYep opened this issue Oct 1, 2020 · 1 comment
Closed

Combine @dataclass and @dataslots into one decorator? #14

TylerYep opened this issue Oct 1, 2020 · 1 comment

Comments

@TylerYep
Copy link

TylerYep commented Oct 1, 2020

Hi, would it be possible to combine @DataClass and @dataslots into one decorator? It seems redundant in a lot of cases to have two stacked decorators, and having to add the extra import from dataclasses import dataclass line in these files.

Thanks!

@starhel
Copy link
Owner

starhel commented Oct 1, 2020

That's probably should be something like

def dataclass_with_slots(_cls=None, *, add_dict: bool = False, add_weakref: bool = False, **dataclass_params):
    def wrap(cls):
        cls = dataclass(**dataclass_params)(cls)
        return dataslots(add_dict=add_dict, add_weakref=add_weakref)(cls)

    return wrap if _cls is None else wrap(_cls)

but this could be problematic in the future because of conflict of argument names, it blocks adding kwargs to dataslots if needed and there's no help from IDE with arguments to dataclass. It's also less intuitive for users imho.

You can easily add combined decorator to your code. If you don't need any parameters to dataclass and dataslots you can even add to your code something like:

def dataclass_with_slots(cls):
    return dataslots(dataclass(cls))

@TylerYep TylerYep closed this as completed Oct 6, 2020
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

2 participants