mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add sphinx.util.inspect:iscoroutinefunction()
This commit is contained in:
@@ -15,7 +15,7 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
import typing
|
import typing
|
||||||
import warnings
|
import warnings
|
||||||
from functools import partial
|
from functools import partial, partialmethod
|
||||||
from inspect import ( # NOQA
|
from inspect import ( # NOQA
|
||||||
isclass, ismethod, ismethoddescriptor, isroutine
|
isclass, ismethod, ismethoddescriptor, isroutine
|
||||||
)
|
)
|
||||||
@@ -129,7 +129,7 @@ def isenumattribute(x):
|
|||||||
def ispartial(obj):
|
def ispartial(obj):
|
||||||
# type: (Any) -> bool
|
# type: (Any) -> bool
|
||||||
"""Check if the object is partial."""
|
"""Check if the object is partial."""
|
||||||
return isinstance(obj, partial)
|
return isinstance(obj, (partial, partialmethod))
|
||||||
|
|
||||||
|
|
||||||
def isclassmethod(obj):
|
def isclassmethod(obj):
|
||||||
@@ -212,6 +212,18 @@ def isbuiltin(obj):
|
|||||||
return inspect.isbuiltin(obj) or ispartial(obj) and inspect.isbuiltin(obj.func)
|
return inspect.isbuiltin(obj) or ispartial(obj) and inspect.isbuiltin(obj.func)
|
||||||
|
|
||||||
|
|
||||||
|
def iscoroutinefunction(obj):
|
||||||
|
# type: (Any) -> bool
|
||||||
|
"""Check if the object is coroutine-function."""
|
||||||
|
if inspect.iscoroutinefunction(obj):
|
||||||
|
return True
|
||||||
|
elif ispartial(obj) and inspect.iscoroutinefunction(obj.func):
|
||||||
|
# partialed
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def safe_getattr(obj, name, *defargs):
|
def safe_getattr(obj, name, *defargs):
|
||||||
# type: (Any, str, str) -> object
|
# type: (Any, str, str) -> object
|
||||||
"""A getattr() that turns all exceptions into AttributeErrors."""
|
"""A getattr() that turns all exceptions into AttributeErrors."""
|
||||||
|
|||||||
@@ -5,7 +5,11 @@ def func():
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
async def coroutinefunc():
|
||||||
|
pass
|
||||||
|
|
||||||
partial_func = partial(func)
|
partial_func = partial(func)
|
||||||
|
partial_coroutinefunc = partial(coroutinefunc)
|
||||||
|
|
||||||
builtin_func = print
|
builtin_func = print
|
||||||
partial_builtin_func = partial(print)
|
partial_builtin_func = partial(print)
|
||||||
|
|||||||
@@ -19,6 +19,11 @@ class Base():
|
|||||||
|
|
||||||
partialmeth = partialmethod(meth)
|
partialmeth = partialmethod(meth)
|
||||||
|
|
||||||
|
async def coroutinemeth(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
partial_coroutinemeth = partialmethod(coroutinemeth)
|
||||||
|
|
||||||
|
|
||||||
class Inherited(Base):
|
class Inherited(Base):
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -397,6 +397,22 @@ def test_isstaticmethod(app):
|
|||||||
assert inspect.isstaticmethod(Inherited.meth, Inherited, 'meth') is False
|
assert inspect.isstaticmethod(Inherited.meth, Inherited, 'meth') is False
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.sphinx(testroot='ext-autodoc')
|
||||||
|
def test_iscoroutinefunction(app):
|
||||||
|
from target.functions import coroutinefunc, func, partial_coroutinefunc
|
||||||
|
from target.methods import Base
|
||||||
|
|
||||||
|
assert inspect.iscoroutinefunction(func) is False # function
|
||||||
|
assert inspect.iscoroutinefunction(coroutinefunc) is True # coroutine
|
||||||
|
assert inspect.iscoroutinefunction(partial_coroutinefunc) is True # partial-ed coroutine
|
||||||
|
assert inspect.iscoroutinefunction(Base.meth) is False # method
|
||||||
|
assert inspect.iscoroutinefunction(Base.coroutinemeth) is True # coroutine-method
|
||||||
|
|
||||||
|
# partial-ed coroutine-method
|
||||||
|
partial_coroutinemeth = Base.__dict__['partial_coroutinemeth']
|
||||||
|
assert inspect.iscoroutinefunction(partial_coroutinemeth) is True
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.sphinx(testroot='ext-autodoc')
|
@pytest.mark.sphinx(testroot='ext-autodoc')
|
||||||
def test_isfunction(app):
|
def test_isfunction(app):
|
||||||
from target.functions import builtin_func, partial_builtin_func
|
from target.functions import builtin_func, partial_builtin_func
|
||||||
|
|||||||
Reference in New Issue
Block a user