Commits

Takayuki Shimizukawa  committed 4aa18c3

Closes #1062: sphinx.ext.autodoc use __init__ method signature for class signature.

  • Participants
  • Parent commits c055abb

Comments (0)

Files changed (3)

 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.
 

File sphinx/ext/autodoc.py

     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):

File tests/test_autodoc.py

         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):