Formally document utils.maybe_coroutine

This commit is contained in:
Rapptz 2023-01-16 20:59:21 -05:00
parent ed40d55f99
commit 469f006d8b
2 changed files with 27 additions and 0 deletions

View File

@ -91,6 +91,7 @@ __all__ = (
'remove_markdown', 'remove_markdown',
'escape_markdown', 'escape_markdown',
'escape_mentions', 'escape_mentions',
'maybe_coroutine',
'as_chunks', 'as_chunks',
'format_dt', 'format_dt',
'MISSING', 'MISSING',
@ -657,6 +658,30 @@ def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float:
async def maybe_coroutine(f: MaybeAwaitableFunc[P, T], *args: P.args, **kwargs: P.kwargs) -> T: async def maybe_coroutine(f: MaybeAwaitableFunc[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
r"""|coro|
A helper function that will await the result of a function if it's a coroutine
or return the result if it's not.
This is useful for functions that may or may not be coroutines.
.. versionadded:: 2.2
Parameters
-----------
f: Callable[..., Any]
The function or coroutine to call.
\*args
The arguments to pass to the function.
\*\*kwargs
The keyword arguments to pass to the function.
Returns
--------
Any
The result of the function or coroutine.
"""
value = f(*args, **kwargs) value = f(*args, **kwargs)
if _isawaitable(value): if _isawaitable(value):
return await value return await value

View File

@ -1398,6 +1398,8 @@ Utility Functions
.. autofunction:: discord.utils.setup_logging .. autofunction:: discord.utils.setup_logging
.. autofunction:: discord.utils.maybe_coroutine
.. autofunction:: discord.utils.snowflake_time .. autofunction:: discord.utils.snowflake_time
.. autofunction:: discord.utils.time_snowflake .. autofunction:: discord.utils.time_snowflake