From 1d5c7d1f2014fb3f78b13218f2699db33555da4b Mon Sep 17 00:00:00 2001 From: Takayuki Shimizukawa Date: Mon, 9 Dec 2013 10:55:51 +0000 Subject: [PATCH] Fix: autodoc class __init__ override not removed from docstring. Closes #1138 --- CHANGES | 4 ++++ sphinx/ext/autodoc.py | 14 ++++++++++++-- tests/test_autodoc.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 7df90b9dc..dfa02cf45 100644 --- a/CHANGES +++ b/CHANGES @@ -34,6 +34,10 @@ Bugs fixed * #1269: Fix ResourceWarnings with Python 3.2 or later. +* #1138: Fix: When ``autodoc_docstring_signature = True`` and + ``autoclass_content = 'init'`` or ``'both'``, __init__ line should be + removed from class documentation. + Release 1.2 beta3 (released Oct 3, 2013) ======================================== diff --git a/sphinx/ext/autodoc.py b/sphinx/ext/autodoc.py index fac2d72fd..8d78feb21 100644 --- a/sphinx/ext/autodoc.py +++ b/sphinx/ext/autodoc.py @@ -1071,8 +1071,18 @@ class ClassDocumenter(ModuleLevelDocumenter): # for classes, what the "docstring" is can be controlled via a # config value; the default is only the class docstring if content in ('both', 'init'): - initdocstring = self.get_attr( - self.get_attr(self.object, '__init__', None), '__doc__') + # get __init__ method document 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__'] + init_doc._find_signature() # this effects to get_doc() result + initdocstring = '\n'.join( + ['\n'.join(l) for l in init_doc.get_doc(encoding)]) + else: + initdocstring = self.get_attr( + self.get_attr(self.object, '__init__', None), '__doc__') # for new-style classes, no __init__ means default __init__ if initdocstring == object.__init__.__doc__: initdocstring = None diff --git a/tests/test_autodoc.py b/tests/test_autodoc.py index 1931c0d0c..63c341f1b 100644 --- a/tests/test_autodoc.py +++ b/tests/test_autodoc.py @@ -301,6 +301,39 @@ def test_get_doc(): assert getdocl('class', D) == ['Class docstring', '', 'Init docstring', '', 'Other', ' lines'] + #__init__ have signature at first line of docstring + class E: + """Class docstring""" + def __init__(self, *args, **kw): + """ + __init__(a1, a2, kw1=True, kw2=False) + + Init docstring + """ + + # signature line in the docstring will be kept when + # autodoc_docstring_signature == False + directive.env.config.autodoc_docstring_signature = False + directive.env.config.autoclass_content = 'class' + assert getdocl('class', E) == ['Class docstring'] + directive.env.config.autoclass_content = 'init' + assert getdocl('class', E) == ['__init__(a1, a2, kw1=True, kw2=False)', + '', 'Init docstring'] + directive.env.config.autoclass_content = 'both' + assert getdocl('class', E) == ['Class docstring', '', + '__init__(a1, a2, kw1=True, kw2=False)', + '', 'Init docstring'] + + # signature line in the docstring will be removed when + # autodoc_docstring_signature == True + directive.env.config.autodoc_docstring_signature = True #default + directive.env.config.autoclass_content = 'class' + assert getdocl('class', E) == ['Class docstring'] + directive.env.config.autoclass_content = 'init' + assert getdocl('class', E) == ['Init docstring'] + directive.env.config.autoclass_content = 'both' + assert getdocl('class', E) == ['Class docstring', '', 'Init docstring'] + @with_setup(setup_test) def test_docstring_processing():