mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Closes #1062: sphinx.ext.autodoc use __init__ method signature for class signature.
This commit is contained in:
2
CHANGES
2
CHANGES
@@ -1,6 +1,8 @@
|
||||
Release 1.2 (in development)
|
||||
============================
|
||||
|
||||
* #1062: sphinx.ext.autodoc use __init__ method signature for class signature.
|
||||
|
||||
* PR#111: Respect add_autodoc_attrgetter() even when inherited-members is set.
|
||||
Thanks to A. Jesse Jiryu Davis.
|
||||
|
||||
|
||||
@@ -1011,6 +1011,18 @@ class ClassDocumenter(ModuleLevelDocumenter):
|
||||
def format_signature(self):
|
||||
if self.doc_as_attr:
|
||||
return ''
|
||||
|
||||
# get __init__ method signature from __init__.__doc__
|
||||
if self.env.config.autodoc_docstring_signature:
|
||||
# only act if the feature is enabled
|
||||
init_doc = MethodDocumenter(self.directive, '__init__')
|
||||
init_doc.object = self.get_attr(self.object, '__init__', None)
|
||||
init_doc.objpath = ['__init__']
|
||||
result = init_doc._find_signature()
|
||||
if result is not None:
|
||||
# use args only for Class signature
|
||||
return '(%s)' % result[0]
|
||||
|
||||
return ModuleLevelDocumenter.format_signature(self)
|
||||
|
||||
def add_directive_header(self, sig):
|
||||
|
||||
@@ -177,6 +177,20 @@ def test_format_signature():
|
||||
assert formatsig('class', 'C', C, None, None) == '(a, b=None)'
|
||||
assert formatsig('class', 'C', D, 'a, b', 'X') == '(a, b) -> X'
|
||||
|
||||
#__init__ have signature at first line of docstring
|
||||
class F2:
|
||||
'''some docstring for F2.'''
|
||||
def __init__(self, *args, **kw):
|
||||
'''
|
||||
__init__(a1, a2, kw1=True, kw2=False)
|
||||
|
||||
some docstring for __init__.
|
||||
'''
|
||||
class G2(F2, object):
|
||||
pass
|
||||
for C in (F2, G2):
|
||||
assert formatsig('class', 'C', C, None, None) == '(a1, a2, kw1=True, kw2=False)'
|
||||
|
||||
# test for methods
|
||||
class H:
|
||||
def foo1(self, b, *c):
|
||||
|
||||
Reference in New Issue
Block a user