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

No "post-init only" marker #57

Open
AJR1991 opened this issue Aug 10, 2021 · 1 comment
Open

No "post-init only" marker #57

AJR1991 opened this issue Aug 10, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@AJR1991
Copy link

AJR1991 commented Aug 10, 2021

I want to use slots in CPython using dataclassy, but I also want to be able to hide initializers for certain variables from the user. For example:

@dataclass(slots=True)
class TestDataClass:
    normal: float
    _hidden: str = post_init("Test") 
    _post: float = post_init()

    def __post_init__(self):
        self._post = 3.141 * self.normal * self.normal

What I'm doing here is making _hidden and _post unable to be initialized as part of the given args on construction (meaning you can't set it via _hidden="hello") . It's possible to do this kind of thing without slots by just initializing it in __post_init__ but with slots, you can't without having to define __slots__ at the top, which looks ugly.

Is it possible to add this functionality? Or am I misreading documentation and this is already possible?

@biqqles
Copy link
Owner

biqqles commented Oct 12, 2021

No, it's not already possible. I originally didn't want to implement something like late_init (taking the name from Kotlin) because I think it means checking the type before generating __init__ which we don't do for anything else, so it would be messy. Even factory is implemented without needing to do this. I also thought it was neat just to be able to simply assign attributes in __post_init__. That said it never occurred to me that missing it out would be a problem for slotted classes.

I need to think about this more to work out how best to implement the feature.

@biqqles biqqles added the enhancement New feature or request label Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants