[Python-ideas] Re: Allow dataclasses's auto-generated __init__ to optionally accept **kwargs and pass them to __post_init_

2021-09-21 Thread thomas . d . mckay
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 = []

[Python-ideas] Re: Allow dataclasses's auto-generated __init__ to optionally accept **kwargs and pass them to __post_init_

2021-09-20 Thread thomas . d . mckay
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

[Python-ideas] Re: Allow dataclasses's auto-generated __init__ to optionally accept **kwargs and pass them to __post_init_

2021-09-20 Thread Eric V. Smith
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

[Python-ideas] Re: Allow dataclasses's auto-generated __init__ to optionally accept **kwargs and pass them to __post_init_

2021-09-20 Thread Paul Bryan
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

[Python-ideas] Re: Allow dataclasses's auto-generated __init__ to optionally accept **kwargs and pass them to __post_init_

2021-09-20 Thread Eric V. Smith
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

[Python-ideas] Re: Allow dataclasses's auto-generated __init__ to optionally accept **kwargs and pass them to __post_init_

2021-09-20 Thread thomas . d . mckay
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: