mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Fix #4415: autodoc classifies inherited staticmethods as regular methods
This commit is contained in:
1
CHANGES
1
CHANGES
@@ -17,6 +17,7 @@ Bugs fixed
|
|||||||
----------
|
----------
|
||||||
|
|
||||||
* #4415: autodoc classifies inherited classmethods as regular methods
|
* #4415: autodoc classifies inherited classmethods as regular methods
|
||||||
|
* #4415: autodoc classifies inherited staticmethods as regular methods
|
||||||
|
|
||||||
Testing
|
Testing
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ from sphinx.application import ExtensionError
|
|||||||
from sphinx.util import logging
|
from sphinx.util import logging
|
||||||
from sphinx.util.inspect import Signature, isdescriptor, safe_getmembers, \
|
from sphinx.util.inspect import Signature, isdescriptor, safe_getmembers, \
|
||||||
safe_getattr, object_description, is_builtin_class_method, \
|
safe_getattr, object_description, is_builtin_class_method, \
|
||||||
isenumattribute, isclassmethod, getdoc
|
isenumattribute, isclassmethod, isstaticmethod, getdoc
|
||||||
from sphinx.util.docstrings import prepare_docstring
|
from sphinx.util.docstrings import prepare_docstring
|
||||||
|
|
||||||
if False:
|
if False:
|
||||||
@@ -1268,7 +1268,7 @@ class MethodDocumenter(DocstringSignatureMixin, ClassLevelDocumenter): # type:
|
|||||||
self.directivetype = 'classmethod'
|
self.directivetype = 'classmethod'
|
||||||
# document class and static members before ordinary ones
|
# document class and static members before ordinary ones
|
||||||
self.member_order = self.member_order - 1
|
self.member_order = self.member_order - 1
|
||||||
elif isinstance(obj, staticmethod):
|
elif isstaticmethod(obj, cls=self.parent, name=self.object_name):
|
||||||
self.directivetype = 'staticmethod'
|
self.directivetype = 'staticmethod'
|
||||||
# document class and static members before ordinary ones
|
# document class and static members before ordinary ones
|
||||||
self.member_order = self.member_order - 1
|
self.member_order = self.member_order - 1
|
||||||
|
|||||||
@@ -168,6 +168,26 @@ def isclassmethod(obj):
|
|||||||
return False
|
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):
|
def isdescriptor(x):
|
||||||
# type: (Any) -> bool
|
# type: (Any) -> bool
|
||||||
"""Check if the object is some kind of descriptor."""
|
"""Check if the object is some kind of descriptor."""
|
||||||
|
|||||||
@@ -68,6 +68,10 @@ class Base(object):
|
|||||||
def inheritedclassmeth(cls):
|
def inheritedclassmeth(cls):
|
||||||
"""Inherited class method."""
|
"""Inherited class method."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def inheritedstaticmeth(cls):
|
||||||
|
"""Inherited static method."""
|
||||||
|
|
||||||
|
|
||||||
class Derived(Base):
|
class Derived(Base):
|
||||||
def inheritedmeth(self):
|
def inheritedmeth(self):
|
||||||
|
|||||||
@@ -716,6 +716,7 @@ def test_generate():
|
|||||||
options.inherited_members = True
|
options.inherited_members = True
|
||||||
should.append(('method', 'target.Class.inheritedmeth'))
|
should.append(('method', 'target.Class.inheritedmeth'))
|
||||||
should.append(('method', 'target.Class.inheritedclassmeth'))
|
should.append(('method', 'target.Class.inheritedclassmeth'))
|
||||||
|
should.append(('method', 'target.Class.inheritedstaticmeth'))
|
||||||
assert_processes(should, 'class', 'Class')
|
assert_processes(should, 'class', 'Class')
|
||||||
|
|
||||||
# test special members
|
# test special members
|
||||||
@@ -801,6 +802,7 @@ def test_generate():
|
|||||||
' .. py:attribute:: Class._private_inst_attr',
|
' .. py:attribute:: Class._private_inst_attr',
|
||||||
' .. py:classmethod:: Class.inheritedclassmeth()',
|
' .. py:classmethod:: Class.inheritedclassmeth()',
|
||||||
' .. py:method:: Class.inheritedmeth()',
|
' .. py:method:: Class.inheritedmeth()',
|
||||||
|
' .. py:staticmethod:: Class.inheritedstaticmeth()',
|
||||||
],
|
],
|
||||||
'class', 'Class', member_order='bysource', all_members=True)
|
'class', 'Class', member_order='bysource', all_members=True)
|
||||||
del directive.env.ref_context['py:module']
|
del directive.env.ref_context['py:module']
|
||||||
|
|||||||
Reference in New Issue
Block a user