Skip to content

Commit

Permalink
Unpack partial in iscoroutinefunction (#894)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Jan 11, 2024
1 parent 3e90912 commit 937b021
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
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 comments on commit 937b021

Please sign in to comment.