From 66eb54801548f47dafc2a0d79c2b182b857f2574 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sat, 12 Feb 2022 00:28:18 +0900 Subject: [PATCH] Fix #10181: napoleon_use_ivar adds unexpected prefix to vars 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: #5129 and #5977 --- CHANGES | 4 ++++ doc/extdev/deprecated.rst | 5 +++++ sphinx/ext/napoleon/docstring.py | 5 ++++- tests/test_ext_napoleon_docstring.py | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index cca11712e01..4d1fb30852b 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,8 @@ Incompatible changes Deprecated ---------- +* ``sphinx.ext.napoleon.docstring.GoogleDocstring._qualify_name()`` + Features added -------------- @@ -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 diff --git a/doc/extdev/deprecated.rst b/doc/extdev/deprecated.rst index 8c3576b2cc0..64b0a061cbc 100644 --- a/doc/extdev/deprecated.rst +++ b/doc/extdev/deprecated.rst @@ -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 diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py index d1d348590e3..9b9834cfad1 100644 --- a/sphinx/ext/napoleon/docstring.py +++ b/sphinx/ext/napoleon/docstring.py @@ -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 @@ -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: @@ -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:] diff --git a/tests/test_ext_napoleon_docstring.py b/tests/test_ext_napoleon_docstring.py index f0399a89442..b9d18def156 100644 --- a/tests/test_ext_napoleon_docstring.py +++ b/tests/test_ext_napoleon_docstring.py @@ -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)