>
> For simple situations you can call super in the __post_init__ method and
>> things will work fine:
>>
>
> But not for the OP's case: he wanted to pass extra parameters in -- and
> the dataclass' __init__ won't accept extra arguments.
>
>
> Can’t you just create InitVar attributes for the extra args you want to
> pass through in that case?
>

InitVar fields for all the desired parent class init parameters can often
solve the problem.

But it can be painful to have to manually provide every parameter
explicitly when normally (when not using a dataclass) you'd just add *args
and **kwargs to the init signature and call super().__init__(*args,
**kwargs).

Which is what the OP is after.

It becomes more painful the more parameters the parent has- parameters
which the dataclass may not even care about. It not only makes the class
definition long, it adds so these additional parameters to the init
signature, which is icky for introspection and discoverability. Lots of
"What the heck is this parameter doing here?" head scratching for future me
(because I forget everything).

There's currently not a very compelling solution, AFAIK, to be able to use
dataclasses in these kinds of situations ("these kinds" = any situation
other than the most simple) other than the solution Christopher Barker
suggested: using a mixin approach that treats the dataclass parameters
specially. So I just haven't.

I did write a decorator of my own that replaces the dataclass init with one
that calls super().__init__(*args, **kwargs) first before proceeding with
the one written by dataclasses... I can't find it at the moment. But that
has its own problems; one being the IDE doesn't know the init has been
rewritten this way and so will complain about parameters sent to the
dataclass that it doesn't know about.

>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4LVOHYQNTZSFXAOB5USEMQO6USNLBXD2/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to