Skip to content

Commit

Permalink
Fix autodoc: Async staticmethods/ classmethods are considered as not …
Browse files Browse the repository at this point in the history
…async

Since 3.10.0a7, the instances of staticmethod and classmethod have
`__wrapped__` attribute. It confuses sphinx.util.inspect:iscoroutinefunction().

refs:

* https://docs.python.org/3.10/whatsnew/3.10.html#other-language-changes
* https://bugs.python.org/issue43682
  • Loading branch information
tk0miya committed Apr 10, 2021
1 parent 7b97c8c commit deb603f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Features added
Bugs fixed
----------

* #9078: autodoc: Async staticmethods and classmethods are considered as non
async coroutine-functions
* #8870: The style of toctree captions has been changed with docutils-0.17
* #9001: The style of ``sidebar`` directive has been changed with docutils-0.17

Expand Down
14 changes: 12 additions & 2 deletions sphinx/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,18 @@ def isroutine(obj: Any) -> bool:

def iscoroutinefunction(obj: Any) -> bool:
"""Check if the object is coroutine-function."""
# unwrap staticmethod, classmethod and partial (except wrappers)
obj = unwrap_all(obj, stop=lambda o: hasattr(o, '__wrapped__'))
def iswrappedcoroutine(obj: Any) -> bool:
"""Check if the object is wrapped coroutine-function."""
if isstaticmethod(obj) or isclassmethod(obj) or ispartial(obj):
# staticmethod, classmethod and partial method are not a wrapped coroutine-function
# Note: Since 3.10, staticmethod and classmethod becomes a kind of wrappers
return False
elif hasattr(obj, '__wrapped__'):
return True
else:
return False

obj = unwrap_all(obj, stop=iswrappedcoroutine)
if hasattr(obj, '__code__') and inspect.iscoroutinefunction(obj):
# check obj.__code__ because iscoroutinefunction() crashes for custom method-like
# objects (see https://github.com/sphinx-doc/sphinx/issues/6605)
Expand Down

0 comments on commit deb603f

Please sign in to comment.