Skip to content

Commit

Permalink
Fix sphinx-doc#10181: napoleon_use_ivar adds unexpected prefix to vars
Browse files Browse the repository at this point in the history
Since 4.0, :ivar: items has not been rendered as hyperlinks.  So any
modules, classes and tilda are now harmful.  This removes the prefixing
filter for napoleon_use_ivar option.

refs: sphinx-doc#5129 and sphinx-doc#5977
  • Loading branch information
tk0miya committed Feb 11, 2022
1 parent a32d609 commit 66eb548
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES
Expand Up @@ -10,6 +10,8 @@ Incompatible changes
Deprecated
----------

* ``sphinx.ext.napoleon.docstring.GoogleDocstring._qualify_name()``

Features added
--------------

Expand All @@ -28,6 +30,8 @@ Bugs fixed
``no-value`` option
* #9529: LaTeX: named auto numbered footnote (ex. ``[#named]``) that is referred
multiple times was rendered to a question mark
* #10181: napoleon: attributes are displayed like class attributes for google
style docstrings when :confval:`napoleon_use_ivar` is enabled
* #10122: sphinx-build: make.bat does not check the installation of sphinx-build
command before showing help

Expand Down
5 changes: 5 additions & 0 deletions doc/extdev/deprecated.rst
Expand Up @@ -22,6 +22,11 @@ The following is a list of deprecated interfaces.
- (will be) Removed
- Alternatives

* - ``sphinx.ext.napoleon.docstring.GoogleDocstring._qualify_name()``
- 4.5
- 6.0
- N/A

* - ``sphinx.ext.autodoc.AttributeDocumenter._datadescriptor``
- 4.3
- 6.0
Expand Down
5 changes: 4 additions & 1 deletion sphinx/ext/napoleon/docstring.py
Expand Up @@ -13,11 +13,13 @@
import collections
import inspect
import re
import warnings
from functools import partial
from typing import Any, Callable, Dict, List, Tuple, Type, Union

from sphinx.application import Sphinx
from sphinx.config import Config as SphinxConfig
from sphinx.deprecation import RemovedInSphinx60Warning
from sphinx.ext.napoleon.iterators import modify_iter
from sphinx.locale import _, __
from sphinx.util import logging
Expand Down Expand Up @@ -631,7 +633,6 @@ def _parse_attributes_section(self, section: str) -> List[str]:
if not _type:
_type = self._lookup_annotation(_name)
if self._config.napoleon_use_ivar:
_name = self._qualify_name(_name, self._obj)
field = ':ivar %s: ' % _name
lines.extend(self._format_block(field, _desc))
if _type:
Expand Down Expand Up @@ -825,6 +826,8 @@ def _partition_field_on_colon(self, line: str) -> Tuple[str, str, str]:
"".join(after_colon).strip())

def _qualify_name(self, attr_name: str, klass: Type) -> str:
warnings.warn('%s._qualify_name() is deprecated.' %
self.__class__.__name__, RemovedInSphinx60Warning)
if klass and '.' not in attr_name:
if attr_name.startswith('~'):
attr_name = attr_name[1:]
Expand Down
16 changes: 16 additions & 0 deletions tests/test_ext_napoleon_docstring.py
Expand Up @@ -482,6 +482,22 @@ def test_attributes_with_class_reference(self):
super-dooper attribute
:type: numpy.ndarray
"""

def test_attributes_with_class_reference(self):
docstring = """\
Attributes:
foo (int): blah blah
bar (str): blah blah
"""

config = Config(napoleon_use_ivar=True)
actual = str(GoogleDocstring(docstring, config, obj=self.__class__))
expected = """\
:ivar foo: blah blah
:vartype foo: int
:ivar bar: blah blah
:vartype bar: str
"""
self.assertEqual(expected, actual)

Expand Down

0 comments on commit 66eb548

Please sign in to comment.