From 469f006d8bdc4bb59662a57eabf88f11b1b95213 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 16 Jan 2023 20:59:21 -0500 Subject: [PATCH] Formally document utils.maybe_coroutine --- discord/utils.py | 25 +++++++++++++++++++++++++ docs/api.rst | 2 ++ 2 files changed, 27 insertions(+) diff --git a/discord/utils.py b/discord/utils.py index 772d57530..779ee9737 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -91,6 +91,7 @@ __all__ = ( 'remove_markdown', 'escape_markdown', 'escape_mentions', + 'maybe_coroutine', 'as_chunks', 'format_dt', '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: + 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) if _isawaitable(value): return await value diff --git a/docs/api.rst b/docs/api.rst index b8641f188..fce06f784 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -1398,6 +1398,8 @@ Utility Functions .. autofunction:: discord.utils.setup_logging +.. autofunction:: discord.utils.maybe_coroutine + .. autofunction:: discord.utils.snowflake_time .. autofunction:: discord.utils.time_snowflake