mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #9229 from tk0miya/9218_doccomment_for_aliased_class
Close #9218: autodoc: Support variable comment for alias classes
This commit is contained in:
commit
576d0b98a4
1
CHANGES
1
CHANGES
@ -30,6 +30,7 @@ Features added
|
|||||||
allow typehints to be included both in the signature and description
|
allow typehints to be included both in the signature and description
|
||||||
* #4257: autodoc: Add :confval:`autodoc_class_signature` to separate the class
|
* #4257: autodoc: Add :confval:`autodoc_class_signature` to separate the class
|
||||||
entry and the definition of ``__init__()`` method
|
entry and the definition of ``__init__()`` method
|
||||||
|
* #8061, #9218: autodoc: Support variable comment for alias classes
|
||||||
* #3257: autosummary: Support instance attributes for classes
|
* #3257: autosummary: Support instance attributes for classes
|
||||||
* #9129: html search: Show search summaries when html_copy_source = False
|
* #9129: html search: Show search summaries when html_copy_source = False
|
||||||
* #9120: html theme: Eliminate prompt characters of code-block from copyable
|
* #9120: html theme: Eliminate prompt characters of code-block from copyable
|
||||||
|
@ -1676,7 +1676,11 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
|
|||||||
def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
|
def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
|
||||||
if self.doc_as_attr:
|
if self.doc_as_attr:
|
||||||
# Don't show the docstring of the class when it is an alias.
|
# Don't show the docstring of the class when it is an alias.
|
||||||
return None
|
comment = self.get_variable_comment()
|
||||||
|
if comment:
|
||||||
|
return []
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
lines = getattr(self, '_new_docstrings', None)
|
lines = getattr(self, '_new_docstrings', None)
|
||||||
if lines is not None:
|
if lines is not None:
|
||||||
@ -1721,9 +1725,18 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
|
|||||||
tab_width = self.directive.state.document.settings.tab_width
|
tab_width = self.directive.state.document.settings.tab_width
|
||||||
return [prepare_docstring(docstring, ignore, tab_width) for docstring in docstrings]
|
return [prepare_docstring(docstring, ignore, tab_width) for docstring in docstrings]
|
||||||
|
|
||||||
|
def get_variable_comment(self) -> Optional[List[str]]:
|
||||||
|
try:
|
||||||
|
key = ('', '.'.join(self.objpath))
|
||||||
|
analyzer = ModuleAnalyzer.for_module(self.get_real_modname())
|
||||||
|
analyzer.analyze()
|
||||||
|
return list(self.analyzer.attr_docs.get(key, []))
|
||||||
|
except PycodeError:
|
||||||
|
return None
|
||||||
|
|
||||||
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
|
def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
|
||||||
) -> None:
|
) -> None:
|
||||||
if self.doc_as_attr:
|
if self.doc_as_attr and not self.get_variable_comment():
|
||||||
try:
|
try:
|
||||||
more_content = StringList([_('alias of %s') % restify(self.object)], source='')
|
more_content = StringList([_('alias of %s') % restify(self.object)], source='')
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -30,3 +30,6 @@ class Quux(List[Union[int, float]]):
|
|||||||
|
|
||||||
|
|
||||||
Alias = Foo
|
Alias = Foo
|
||||||
|
|
||||||
|
#: docstring
|
||||||
|
OtherAlias = Bar
|
||||||
|
@ -327,3 +327,15 @@ def test_class_alias(app):
|
|||||||
'',
|
'',
|
||||||
' alias of :class:`target.classes.Foo`',
|
' alias of :class:`target.classes.Foo`',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_class_alias_having_doccomment(app):
|
||||||
|
actual = do_autodoc(app, 'class', 'target.classes.OtherAlias')
|
||||||
|
assert list(actual) == [
|
||||||
|
'',
|
||||||
|
'.. py:attribute:: OtherAlias',
|
||||||
|
' :module: target.classes',
|
||||||
|
'',
|
||||||
|
' docstring',
|
||||||
|
'',
|
||||||
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user