Skip to content

Module nonebot_plugin_marshoai.plugin.func_call.utils


func copy_signature(func: F) -> Callable[[Callable[..., Any]], F]

Description: 复制函数签名和文档字符串的装饰器

Source code or View on GitHub
python
def copy_signature(func: F) -> Callable[[Callable[..., Any]], F]:

    def decorator(wrapper: Callable[..., Any]) -> F:

        @wraps(func)
        def wrapped(*args: Any, **kwargs: Any) -> Any:
            return wrapper(*args, **kwargs)
        return wrapped
    return decorator

func async_wrap(func: F) -> F

Description: 装饰器,将同步函数包装为异步函数

Arguments:

  • func (F): 函数对象

Return: F: 包装后的函数对象

Source code or View on GitHub
python
def async_wrap(func: F) -> F:

    @wraps(func)
    async def wrapper(*args: Any, **kwargs: Any) -> Any:
        return func(*args, **kwargs)
    return wrapper

func is_coroutine_callable(call: Callable[..., Any]) -> bool

Description: 判断是否为async def 函数 请注意:是否为 async def 函数与该函数是否能被await调用是两个不同的概念,具体取决于函数返回值是否为awaitable对象

Arguments:

  • call: 可调用对象

Return: bool: 是否为async def函数

Source code or View on GitHub
python
def is_coroutine_callable(call: Callable[..., Any]) -> bool:
    if inspect.isroutine(call):
        return inspect.iscoroutinefunction(call)
    if inspect.isclass(call):
        return False
    func_ = getattr(call, '__call__', None)
    return inspect.iscoroutinefunction(func_)

The document is being improved. Suggestions are welcome.