diff options
author | Carlton Gibson <carlton@noumenal.es> | 2022-12-18 19:13:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-18 19:13:24 (GMT) |
commit | 532aa4e4e019812d0388920768ede7c04232ebe1 (patch) | |
tree | bac6a6c3f467bc6a7d7ac1210fc5f9929cc2742a /Doc | |
parent | 1cf3d78c92eb07dc09d15cc2e773b0b1b9436825 (diff) | |
download | cpython-532aa4e4e019812d0388920768ede7c04232ebe1.zip cpython-532aa4e4e019812d0388920768ede7c04232ebe1.tar.gz cpython-532aa4e4e019812d0388920768ede7c04232ebe1.tar.bz2 |
gh-94912: Added marker for non-standard coroutine function detection (#99247)
This introduces a new decorator `@inspect.markcoroutinefunction`,
which, applied to a sync function, makes it appear async to
`inspect.iscoroutinefunction()`.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/inspect.rst | 25 | ||||
-rw-r--r-- | Doc/whatsnew/3.12.rst | 6 |
2 files changed, 29 insertions, 2 deletions
diff --git a/Doc/library/inspect.rst b/Doc/library/inspect.rst index 6705577..58b84a3 100644 --- a/Doc/library/inspect.rst +++ b/Doc/library/inspect.rst @@ -343,8 +343,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes): .. function:: iscoroutinefunction(object) - Return ``True`` if the object is a :term:`coroutine function` - (a function defined with an :keyword:`async def` syntax). + Return ``True`` if the object is a :term:`coroutine function` (a function + defined with an :keyword:`async def` syntax), a :func:`functools.partial` + wrapping a :term:`coroutine function`, or a sync function marked with + :func:`markcoroutinefunction`. .. versionadded:: 3.5 @@ -352,6 +354,25 @@ attributes (see :ref:`import-mod-attrs` for module attributes): Functions wrapped in :func:`functools.partial` now return ``True`` if the wrapped function is a :term:`coroutine function`. + .. versionchanged:: 3.12 + Sync functions marked with :func:`markcoroutinefunction` now return + ``True``. + + +.. function:: markcoroutinefunction(func) + + Decorator to mark a callable as a :term:`coroutine function` if it would not + otherwise be detected by :func:`iscoroutinefunction`. + + This may be of use for sync functions that return a :term:`coroutine`, if + the function is passed to an API that requires :func:`iscoroutinefunction`. + + When possible, using an :keyword:`async def` function is preferred. Also + acceptable is calling the function and testing the return with + :func:`iscoroutine`. + + .. versionadded:: 3.12 + .. function:: iscoroutine(object) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 73dc462..0cc4471 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -225,6 +225,12 @@ asyncio a custom event loop factory. (Contributed by Kumar Aditya in :gh:`99388`.) +inspect +------- + +* Add :func:`inspect.markcoroutinefunction` to mark sync functions that return + a :term:`coroutine` for use with :func:`iscoroutinefunction`. + (Contributed Carlton Gibson in :gh:`99247`.) pathlib ------- |