biqqles / dataclassy

A fast and flexible reimplementation of data classes
https://pypi.org/project/dataclassy
Mozilla Public License 2.0
81 stars 9 forks source link

Late-init fields still appear in `__init__` #61

Open robertodr opened 1 year ago

robertodr commented 1 year ago

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?

biqqles commented 1 year ago

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.