dataclassy icon indicating copy to clipboard operation
dataclassy copied to clipboard

Late-init fields still appear in `__init__`

Open robertodr opened this issue 2 years ago • 1 comments

I'm comparing this package to the standard dataclasses.

In this case, the __init__ method will only accepts the wheels argument:

from dataclasses import dataclass, field


@dataclass(eq=False)
class Vehicle:

    wheels: int
    _wheels: int = field(init=False, repr=False)

However, the equivalent with dataclassy:

from dataclassy import dataclass


@dataclass(eq=False)
class Vehicle:
    wheels: int
    _wheels: int = None

will accept both wheels and _wheels as arguments. Is this intended?

robertodr avatar Mar 10 '23 09:03 robertodr

Yes, it's intended. Every annotation corresponds to an argument to __init__.

I checked the 'Differences' table and it isn't clear so I understand the confusion. (Setting a default to None is simply a way of avoiding having to pass it as an argument. I never considered that you might actually want to hide an argument, to be honest I'm still not sure I see the utility.)

A field without an annotation is not included in the arguments to __init__, but this means it acts like a class variable (is not copied upon instantiation) which is probably not what you want.

biqqles avatar Mar 21 '23 21:03 biqqles