[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 so

[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 keyword-on

[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 post

[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 args'

[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: 'name_en',

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

2021-09-20 Thread thomas . d . mckay
Sorry for the double post, if the first one passed... I typed Enter too soon by accident :( TL;DR: Add a `strict` keyword option to the dataclass constructor which, by default (True), would keep the current behavior, but otherwise (False) would generate an __init__ that accepts arbitrary **kwar

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Thomas Grainger
I'd imagine that context manager here simply wouldn't cancel the current task if the lock can be acquired in time: ``` async with lock.acquire(timeout=1.0): await do_things_with_lock() ``` On Mon, 20 Sep 2021, 14:15 Gustavo Carneiro, wrote: > On Mon, 20 Sept 2021 at 13:15, Chris Ang

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Gustavo Carneiro
On Mon, 20 Sept 2021 at 13:15, Chris Angelico wrote: > On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro > wrote: > > > > Note that you can wrap any of those methods with an asyncio.wait_for(). > > > > try: > >try: > >await asyncio.wait_for(lock.acquire(), 1.0) > >except asyncio.T

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Thomas Grainger
anyio provides a nice context manager that works in both asyncio and trio: ``` import anyio async def async_fn(): with anyio.move_on_after(1.0) as scope: async with lock: scope.deadline = math.inf await do_things_with_lock() ```

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Chris Angelico
On Mon, Sep 20, 2021 at 9:48 PM Gustavo Carneiro wrote: > > Note that you can wrap any of those methods with an asyncio.wait_for(). > > try: >try: >await asyncio.wait_for(lock.acquire(), 1.0) >except asyncio.TimeoutError: # times out after 1 second >print("deadlock!") >

[Python-ideas] Re: Add timeout parameter to Synchronization Primitives in asyncio

2021-09-20 Thread Gustavo Carneiro
Note that you can wrap any of those methods with an asyncio.wait_for(). try: try: await asyncio.wait_for(lock.acquire(), 1.0) except asyncio.TimeoutError: # times out after 1 second print("deadlock!") return do_things_with_lock() finally: lock.release() Although