diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py index 820de6ee4..a371d0cdc 100644 --- a/sphinx/ext/napoleon/docstring.py +++ b/sphinx/ext/napoleon/docstring.py @@ -30,7 +30,9 @@ _google_section_regex = re.compile(r'^(\s|\w)+:\s*$') _google_typed_arg_regex = re.compile(r'\s*(.+?)\s*\(\s*(.*[^\s]+)\s*\)') _numpy_section_regex = re.compile(r'^[=\-`:\'"~^_*+#<>]{2,}\s*$') _single_colon_regex = re.compile(r'(?\()?' @@ -728,7 +730,7 @@ class GoogleDocstring: after_colon = [] colon = '' found_colon = False - for i, source in enumerate(_xref_regex.split(line)): + for i, source in enumerate(_xref_or_code_regex.split(line)): if found_colon: after_colon.append(source) else: diff --git a/tests/test_ext_napoleon_docstring.py b/tests/test_ext_napoleon_docstring.py index 160079a50..c4c5f1502 100644 --- a/tests/test_ext_napoleon_docstring.py +++ b/tests/test_ext_napoleon_docstring.py @@ -78,15 +78,17 @@ class InlineAttributeTest(BaseDocstringTest): def test_class_data_member(self): config = Config() - docstring = """data member description: + docstring = dedent("""\ + data member description: -- a: b -""" + - a: b + """) actual = str(GoogleDocstring(docstring, config=config, app=None, what='attribute', name='some_data', obj=0)) - expected = """data member description: + expected = dedent("""\ + data member description: -- a: b""" + - a: b""") self.assertEqual(expected, actual) @@ -95,10 +97,30 @@ class InlineAttributeTest(BaseDocstringTest): docstring = """b: data member description with :ref:`reference`""" actual = str(GoogleDocstring(docstring, config=config, app=None, what='attribute', name='some_data', obj=0)) - expected = """data member description with :ref:`reference` + expected = dedent("""\ + data member description with :ref:`reference` -:type: b""" + :type: b""") + self.assertEqual(expected, actual) + def test_class_data_member_inline_no_type(self): + config = Config() + docstring = """data with ``a : in code`` and :ref:`reference` and no type""" + actual = str(GoogleDocstring(docstring, config=config, app=None, + what='attribute', name='some_data', obj=0)) + expected = """data with ``a : in code`` and :ref:`reference` and no type""" + + self.assertEqual(expected, actual) + + def test_class_data_member_inline_ref_in_type(self): + config = Config() + docstring = """:class:`int`: data member description""" + actual = str(GoogleDocstring(docstring, config=config, app=None, + what='attribute', name='some_data', obj=0)) + expected = dedent("""\ + data member description + + :type: :class:`int`""") self.assertEqual(expected, actual)