8000 Unpack partial in iscoroutinefunction by maximlt · Pull Request #894 · holoviz/param · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Unpack partial in iscoroutinefunction #894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions param/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,16 @@ def full_groupby(l, key=lambda x: x):

def iscoroutinefunction(function):
"""
Whether the function is an asynchronous coroutine function.
"""
if not hasattr(inspect, 'iscoroutinefunction'):
return False
import asyncio
try:
return (
inspect.isasyncgenfunction(function) or
asyncio.iscoroutinefunction(function)
)
except AttributeError:
return False
Whether the function is an asynchronous generator or a coroutine.
"""
# Partial unwrapping not required starting from Python 3.11.0
# See https://github.com/holoviz/param/pull/894#issuecomment-1867084447
while isinstance(function, functools.partial):
function = function.func
return (
inspect.isasyncgenfunction(function) or
inspect.iscoroutinefunction(function)
)


def flatten(line):
Expand Down
30 changes: 29 additions & 1 deletion tests/testutils.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import datetime as dt
import os

from functools import partial

import param
import pytest

from param import guess_param_types, resolve_path
from param.parameterized import bothmethod
from param._utils import _is_mutable_container
from param._utils import _is_mutable_container, iscoroutinefunction


try:
Expand Down Expand Up @@ -393,3 +395,29 @@ class P(param.Parameterized):
)
def test__is_mutable_container(obj, ismutable):
assert _is_mutable_container(obj) is ismutable


async def coro():
return


def test_iscoroutinefunction_coroutine():
assert iscoroutinefunction(coro)


def test_iscoroutinefunction_partial_coroutine():
pcoro = partial(partial(coro))
assert iscoroutinefunction(pcoro)


async def agen():
yield


def test_iscoroutinefunction_asyncgen():
assert iscoroutinefunction(agen)


def test_iscoroutinefunction_partial_asyncgen():
pagen = partial(partial(agen))
assert iscoroutinefunction(pagen)
0