Repository: sphinx-doc/sphinx

Issue:
Sphinx 2.0: napoleon extension fails to parse exception with tilde
**Describe the bug**
Using the napoleon extension with `napoleon_numpy_docstring = False`, an exception with a tilde in front fails to parse.

I have a docstring

```python
"""
    Raises:
        ValueError: description.
        ~mymodule.MyError: description.
"""
```

This regex fails:

https://github.com/sphinx-doc/sphinx/blob/1499a97c28820ca2ff525d675e9c86fee917532d/sphinx/ext/napoleon/docstring.py#L103-L104

at this line:

https://github.com/sphinx-doc/sphinx/blob/1499a97c28820ca2ff525d675e9c86fee917532d/sphinx/ext/napoleon/docstring.py#L703

**To Reproduce**

```python
from sphinx.ext.napoleon.docstring import GoogleDocstring
assert GoogleDocstring._name_rgx.match('~mymodule.MyError') is None
```

**Expected behavior**
Sphinx renders it as if I used `:raises ~mymodule.MyError: description.`

**Environment info**
- Sphinx version: 2.0.0
- Sphinx extensions:  sphinx.ext.napoleon

**Additional context**
Bug introduced by #4046



Discussion:

Patch:
diff --git a/CHANGES b/CHANGES
index e97623ea6..ef568431a 100644
--- a/CHANGES
+++ b/CHANGES
@@ -22,6 +22,9 @@ Bugs fixed
   - sphinx.application.CONFIG_FILENAME
   - :confval:`viewcode_import`
 
+* #6220, #6225: napoleon: AttributeError is raised for raised section having
+  references
+
 Testing
 --------
 
diff --git a/sphinx/ext/napoleon/docstring.py b/sphinx/ext/napoleon/docstring.py
index 00a41afe8..0fea99fb8 100644
--- a/sphinx/ext/napoleon/docstring.py
+++ b/sphinx/ext/napoleon/docstring.py
@@ -100,7 +100,7 @@ class GoogleDocstring:
 
     """
 
-    _name_rgx = re.compile(r"^\s*(:(?P<role>\w+):`(?P<name>[a-zA-Z0-9_.-]+)`|"
+    _name_rgx = re.compile(r"^\s*((?::(?P<role>\S+):)?`(?P<name>[a-zA-Z0-9_.-]+)`|"
                            r" (?P<name2>[a-zA-Z0-9_.-]+))\s*", re.X)
 
     def __init__(self, docstring, config=None, app=None, what='', name='',
@@ -700,9 +700,9 @@ def _parse_raises_section(self, section):
         fields = self._consume_fields(parse_type=False, prefer_type=True)
         lines = []  # type: List[str]
         for _name, _type, _desc in fields:
-            m = self._name_rgx.match(_type).groupdict()
-            if m['role']:
-                _type = m['name']
+            m = self._name_rgx.match(_type)
+            if m and m.group('name'):
+                _type = m.group('name')
             _type = ' ' + _type if _type else ''
             _desc = self._strip_empty(_desc)
             _descs = ' ' + '\n    '.join(_desc) if any(_desc) else ''


Test patch:
diff --git a/tests/test_ext_napoleon_docstring.py b/tests/test_ext_napoleon_docstring.py
index 86ded7d89..a333dc47b 100644
--- a/tests/test_ext_napoleon_docstring.py
+++ b/tests/test_ext_napoleon_docstring.py
@@ -473,12 +473,21 @@ def test_raises_types(self):
         A setting wasn't specified, or was invalid.
     ValueError:
         Something something value error.
+    :py:class:`AttributeError`
+        errors for missing attributes.
+    ~InvalidDimensionsError
+        If the dimensions couldn't be parsed.
+    `InvalidArgumentsError`
+        If the arguments are invalid.
 
 """, """
 Example Function
 
 :raises RuntimeError: A setting wasn't specified, or was invalid.
 :raises ValueError: Something something value error.
+:raises AttributeError: errors for missing attributes.
+:raises ~InvalidDimensionsError: If the dimensions couldn't be parsed.
+:raises InvalidArgumentsError: If the arguments are invalid.
 """),
                       ################################
                       ("""