[issue37398] contextlib.ContextDecorator decorating async functions

Nick Coghlan report at bugs.python.org
Sun Aug 1 10:59:17 EDT 2021


Nick Coghlan <ncoghlan at gmail.com> added the comment:

You are correct that this proposal is technically rejected, rather than being a true duplicate of the AsyncContextManager addition.

The core problem with the automatic inference idea is that it isn't possible for the standard library to reliably distinguish at decoration time between "synchronous function that happens to return an awaitable" and "coroutine factory function written as a synchronous function that should nevertheless be treated like a coroutine function".

That ambiguity doesn't exist when ContextDecorator and AsyncContextDecorator are applied to distinct helper functions.

Within a given codebase, you may be able to write a "coroutine argument detection" function that is robust enough for that particular codebase (and iscoroutinefunction() may even be adequate for that purpose), in which case ContextDecorator and AsyncContextDecorator provide the building blocks for implementing the two halves of the solution, leaving only the auto-switching code as project specific.

----------
resolution: duplicate -> rejected

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37398>
_______________________________________


More information about the Python-bugs-list mailing list