Fix #4415: autodoc classifies inherited staticmethods as regular methods

This commit is contained in:
Takeshi KOMIYA
2018-01-19 14:27:14 +09:00
parent cbb10d61f7
commit 295323d5aa
5 changed files with 29 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ Bugs fixed
----------
* #4415: autodoc classifies inherited classmethods as regular methods
* #4415: autodoc classifies inherited staticmethods as regular methods
Testing
--------

View File

@@ -32,7 +32,7 @@ from sphinx.application import ExtensionError
from sphinx.util import logging
from sphinx.util.inspect import Signature, isdescriptor, safe_getmembers, \
safe_getattr, object_description, is_builtin_class_method, \
isenumattribute, isclassmethod, getdoc
isenumattribute, isclassmethod, isstaticmethod, getdoc
from sphinx.util.docstrings import prepare_docstring
if False:
@@ -1268,7 +1268,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
self.directivetype = 'classmethod'
# document class and static members before ordinary ones
self.member_order = self.member_order - 1
elif isinstance(obj, staticmethod):
elif isstaticmethod(obj, cls=self.parent, name=self.object_name):
self.directivetype = 'staticmethod'
# document class and static members before ordinary ones
self.member_order = self.member_order - 1

View File

@@ -168,6 +168,26 @@ def isclassmethod(obj):
return False
def isstaticmethod(obj, cls=None, name=None):
# type: (Any, Any, unicode) -> bool
"""Check if the object is staticmethod."""
if isinstance(obj, staticmethod):
return True
elif cls and name:
# trace __mro__ if the method is defined in parent class
#
# .. note:: This only works with new style classes.
for basecls in getattr(cls, '__mro__', []):
meth = basecls.__dict__.get(name)
if meth:
if isinstance(meth, staticmethod):
return True
else:
return False
return False
def isdescriptor(x):
# type: (Any) -> bool
"""Check if the object is some kind of descriptor."""

View File

@@ -68,6 +68,10 @@ class Base(object):
def inheritedclassmeth(cls):
"""Inherited class method."""
@staticmethod
def inheritedstaticmeth(cls):
"""Inherited static method."""
class Derived(Base):
def inheritedmeth(self):

View File

@@ -716,6 +716,7 @@ def test_generate():
options.inherited_members = True
should.append(('method', 'target.Class.inheritedmeth'))
should.append(('method', 'target.Class.inheritedclassmeth'))
should.append(('method', 'target.Class.inheritedstaticmeth'))
assert_processes(should, 'class', 'Class')
# test special members
@@ -801,6 +802,7 @@ def test_generate():
' .. py:attribute:: Class._private_inst_attr',
' .. py:classmethod:: Class.inheritedclassmeth()',
' .. py:method:: Class.inheritedmeth()',
' .. py:staticmethod:: Class.inheritedstaticmeth()',
],
'class', 'Class', member_order='bysource', all_members=True)
del directive.env.ref_context['py:module']