Shmuel H. <shmuelhaz...@gmail.com> added the comment:
I think it was designed to. However, it is not very usable in production for a number of reasons: 1. It won't work with frozen instances (you'll have to call `object.__setattr__` directly). 2. It gets very messy with more than one or two `InitVar`s which makes it very hard to differentiate between "real" values, `InitVar`s and the init logic: ```python from dataclasses import dataclass, InitVar @dataclass class DataClass: member0_init: InitVar[str] = None member1_init: InitVar[list] = None member0: int = None member1: dict = None def __post_init__(self, member0_init: str, member1_init: list): if member0_init is not None and self.member0 is None: self.member0 = int(member0_init) if member1_init is not None and self.member1 is None: self.member1 = dict(member1_init) ``` That code should be equivalent to: ```python from dataclasses import dataclass from typing import Union @dataclass class DataClass: member0: int member1: dict def __init__(self, member0: Union[int, str], member1: Union[dict, list]): if isinstance(member0, str): member0 = int(member0) if isinstance(member1, list): member1 = dict(member1) self.__default_init__(member0=member0, member1=member1) ``` Which is much closer to regular python code to someone new for dataclasses. I would be happy to hear if you have a better solution; I just think it is pretty simple and straight-forward. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue38444> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com