import types
import asyncio


async def apply_coroutine(func: types.FunctionType, *args, **kwargs) -> object:
    """
    Call a function with the supplied arguments.
    If the result is a coroutine, await it.

    >>> async def foobar(): return 'hi'
    >>> async def async_foobar(): return 'hi'
    >>> await apply_coroutine(foobar)
    'hi'
    >>> await apply_coroutine(async_foobar)
    'hi'

    :param func: function to run as coroutiune if one
    :param \\*args: args to call function with
    :param \\**kwargs: kwargs to call function with
    """
    result = func(*args, **kwargs)
    if asyncio.iscoroutine(result):
        return await result
    return result