My solution, if anyone comes by here looking for one:
from dataclasses import _create_fn
def patched_create_fn(name: str, args: list[str], body: list[str], **kwargs):
if name == '__init__':
args.append('**kwargs')
if body == ['pass']:
body = []
Thanks for the reply Eric (and for dataclasses). Right now, my solution is
pretty much that, except I do it by monkey-patching dataclasses._init_fn which,
I know, isn't the greatest solution I could find. Your double decorator
solution is cleaner. I'll try that instead. I still end up copying
In 3.10 you can specify keyword-only arguments to dataclasses. See
https://docs.python.org/3.10/library/dataclasses.html, search for kw_only.
This doesn't address Thomas's issue in any way, but I thought I'd
mention it.
Eric
On 9/20/2021 3:36 PM, Paul Bryan wrote:
I'm in favour of
I'm in favour of keyword-only arguments in dataclasses; however
accepting arbitrary **kwargs seems pretty exotic to me. As Eric has
suggested, this seems like a job for some kind of wrapper or decorator.
On Mon, 2021-09-20 at 14:28 +, thomas.d.mc...@gmail.com wrote:
> Sorry for the double
You could write your own decorator to add this functionality for you.
Something like:
---
from dataclasses import dataclass
def add_kw(cls):
original_init = cls.__init__
def new_init(self, x, y, **kwargs):
print('init called with additional
Oops, there's an indent error for the `extra_info: dict = field(init=False)`
and that last example should be:
def __post_init__(self, **kwargs)
self.name_translations = {
k: kwargs.pop(k)
for k in kwargs.keys()
if k.startswith('name_') # e.g: