From a28c9ad84225b09ae4968a21e9d24dee38d34c95 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Thu, 28 May 2020 01:46:14 +0900 Subject: [PATCH] Fix #7734: napoleon: overescaped trailing underscore on attribute --- CHANGES | 1 + sphinx/ext/napoleon/docstring.py | 2 +- tests/test_ext_napoleon_docstring.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 48ab995cf..af7255f26 100644 --- a/CHANGES +++ b/CHANGES @@ -75,6 +75,7 @@ Features added :rst:dir:`py:exception:` and :rst:dir:`py:method:` directives * #7596: py domain: Change a type annotation for variables to a hyperlink * #7582: napoleon: a type for attribute are represented like type annotation +* #7734: napoleon: overescaped trailing underscore on attribute * #7683: Add ``allowed_exceptions`` parameter to ``Sphinx.emit()`` to allow handlers to raise specified exceptions diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py index 11409e6f6..32edd7f8f 100644 --- a/sphinx/ext/napoleon/docstring.py +++ b/sphinx/ext/napoleon/docstring.py @@ -318,7 +318,7 @@ class GoogleDocstring: return [line[min_indent:] for line in lines] def _escape_args_and_kwargs(self, name: str) -> str: - if name.endswith('_'): + if name.endswith('_') and getattr(self._config, 'strip_signature_backslash', False): name = name[:-1] + r'\_' if name[:2] == '**': diff --git a/tests/test_ext_napoleon_docstring.py b/tests/test_ext_napoleon_docstring.py index 3027a4cb2..738fd6532 100644 --- a/tests/test_ext_napoleon_docstring.py +++ b/tests/test_ext_napoleon_docstring.py @@ -1394,6 +1394,26 @@ Summary Attributes ---------- +arg_ : type + some description +""" + + expected = """ +:ivar arg_: some description +:vartype arg_: type +""" + + config = Config(napoleon_use_ivar=True) + app = mock.Mock() + actual = str(NumpyDocstring(docstring, config, app, "class")) + + self.assertEqual(expected, actual) + + def test_underscore_in_attribute_strip_signature_backslash(self): + docstring = """ +Attributes +---------- + arg_ : type some description """ @@ -1404,6 +1424,7 @@ arg_ : type """ config = Config(napoleon_use_ivar=True) + config.strip_signature_backslash = True app = mock.Mock() actual = str(NumpyDocstring(docstring, config, app, "class"))