Add automatic constructor for classes

I actually wrote the __post_init__ of my implementation to handle the case where you want to do something but not to every attribute by having the init generator pick up the argument names defined in the post init function.

eg: if you write something roughly like:

@dataclass_like
class Ex:
    a: str
    b: Path
    
    def __post_init__(self, b: str | Path):
        self.b = Path(b)

You end up with:

class Ex:
    a: str
    b: Path

    def __init__(self, a: str, b: str | Path):
        self.a = a
        self.__post_init__(b=b)

    def __post_init__(self, b: str | Path):
        self.b = Path(b)

I do still generate __eq__ and __repr__ by default as I find those generally useful features to have on every python class and find those even more tedious to write than __init__ (It’s rare that I need hashability on these classes, if I need that I also need to make the class ‘immutable’).

1 Like