|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""Pins the cross-references in the docs build's ``Warns:`` sections. |
| 3 | +
|
| 4 | +GitHub issue #2094: napoleon turns a ``Raises:`` entry into a ``:raises:`` |
| 5 | +field, which the Python domain links to the exception, but writes a ``Warns:`` |
| 6 | +entry into a plain field with the class name in bold, linking nothing -- and a |
| 7 | +``~`` prefix was printed as it stands. ``parse_warns_section`` in |
| 8 | +:file:`docs/source/conf.py`, which ``setup`` installs over napoleon's own parser, |
| 9 | +writes each warning class as an ``exc`` role that searches the way the |
| 10 | +``:raises:`` field does. |
| 11 | +
|
| 12 | +The tests run napoleon itself with the build's settings, and resolve the role |
| 13 | +and the ``:raises:`` field's node through the Python domain side by side. They |
| 14 | +need Sphinx and the pinned ``sphinx-autodoc-typehints``, and skip without them, |
| 15 | +but run no build. |
| 16 | +
|
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import inspect |
| 22 | +import re |
| 23 | +import types |
| 24 | +import unittest |
| 25 | +from typing import TYPE_CHECKING |
| 26 | +from unittest import mock |
| 27 | + |
| 28 | +from tests.project.test_docs_ambiguous_xrefs_1775_unit import (HAS_SPHINX, REF_CONTEXT, SPHINX_SKIP_REASON, |
| 29 | + connections, load_conf) |
| 30 | + |
| 31 | +if TYPE_CHECKING: |
| 32 | + from typing import Any |
| 33 | + |
| 34 | +#: Test tier (#1958): drives a hook of docs/source/conf.py, which the docs build runs |
| 35 | +#: on one interpreter. |
| 36 | +__tier__ = 'LATEST' |
| 37 | + |
| 38 | +#: Where the warning classes are documented, and one of them. |
| 39 | +WARNINGS = 'pcapkit.utilities.warnings' |
| 40 | +REGISTRY_WARNING = f'{WARNINGS}.RegistryWarning' |
| 41 | + |
| 42 | +#: A Google ``Warns:`` section in each form an entry's name takes. |
| 43 | +GOOGLE = '''Do something. |
| 44 | +
|
| 45 | +Warns: |
| 46 | + RegistryWarning: If bare. |
| 47 | + pcapkit.utilities.warnings.EngineWarning: If dotted, over |
| 48 | + two lines. |
| 49 | + ~pcapkit.utilities.warnings.FormatWarning: If shortened. |
| 50 | + AttributeWarning, UserWarning: If several, one a builtin. |
| 51 | +
|
| 52 | +''' |
| 53 | + |
| 54 | +#: What the section above becomes. |
| 55 | +GOOGLE_WARNS = [ |
| 56 | + ':Warns: * :exc:`.RegistryWarning` -- If bare.', |
| 57 | + ' * :exc:`.pcapkit.utilities.warnings.EngineWarning` -- If dotted, over', |
| 58 | + ' two lines.', |
| 59 | + ' * :exc:`~.pcapkit.utilities.warnings.FormatWarning` -- If shortened.', |
| 60 | + ' * :exc:`.AttributeWarning`, :exc:`UserWarning` -- If several, one a builtin.', |
| 61 | +] |
| 62 | + |
| 63 | +#: An ``exc`` role, as the hook writes it. |
| 64 | +EXC_ROLE = re.compile(r':exc:`([^`]+)`') |
| 65 | + |
| 66 | + |
| 67 | +@unittest.skipUnless(HAS_SPHINX, SPHINX_SKIP_REASON) |
| 68 | +class WarnsSectionTests(unittest.TestCase): |
| 69 | + """Napoleon links the warning classes of a ``Warns:`` section.""" |
| 70 | + |
| 71 | + conf = None # type: Any |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def setUpClass(cls) -> 'None': |
| 75 | + from sphinx.ext.napoleon.docstring import GoogleDocstring # pylint: disable=import-outside-toplevel |
| 76 | + |
| 77 | + # ``setup`` replaces napoleon's parser on the class; put napoleon's back after. |
| 78 | + patcher = mock.patch.object(GoogleDocstring, '_parse_warns_section', |
| 79 | + GoogleDocstring.__dict__['_parse_warns_section']) |
| 80 | + patcher.start() |
| 81 | + cls.addClassCleanup(patcher.stop) |
| 82 | + cls.conf = load_conf() |
| 83 | + cls.wired = connections(cls.conf) |
| 84 | + |
| 85 | + def config(self) -> 'Any': |
| 86 | + """Napoleon's settings, as the build reads them from ``conf.py``.""" |
| 87 | + from sphinx.ext.napoleon import Config # pylint: disable=import-outside-toplevel |
| 88 | + return Config(**{key: getattr(self.conf, key) for key in dir(self.conf) |
| 89 | + if key.startswith('napoleon_')}) |
| 90 | + |
| 91 | + def google(self, text: 'str', what: 'str' = 'function') -> 'list[str]': |
| 92 | + """``text`` as napoleon's Google parser writes it.""" |
| 93 | + from sphinx.ext.napoleon.docstring import GoogleDocstring # pylint: disable=import-outside-toplevel |
| 94 | + return GoogleDocstring(text, self.config(), what=what).lines() |
| 95 | + |
| 96 | + def numpy(self, text: 'str') -> 'list[str]': |
| 97 | + """``text`` as napoleon's NumPy parser writes it.""" |
| 98 | + from sphinx.ext.napoleon.docstring import NumpyDocstring # pylint: disable=import-outside-toplevel |
| 99 | + return NumpyDocstring(text, self.config(), what='function').lines() |
| 100 | + |
| 101 | + def test_setup_installs_the_parser(self) -> 'None': |
| 102 | + """Without it, napoleon's own parser writes the name in bold and nothing links it.""" |
| 103 | + from sphinx.ext.napoleon.docstring import GoogleDocstring # pylint: disable=import-outside-toplevel |
| 104 | + self.assertIs(GoogleDocstring.__dict__['_parse_warns_section'], |
| 105 | + getattr(self.conf, 'parse_warns_section', None)) |
| 106 | + |
| 107 | + def test_google_section_links_every_form_of_name(self) -> 'None': |
| 108 | + """Bare, dotted, ``~``-prefixed and several names, each as an ``exc`` role.""" |
| 109 | + lines = self.google(GOOGLE) |
| 110 | + start = next(index for index, line in enumerate(lines) if line.startswith(':Warns:')) |
| 111 | + self.assertEqual(lines[start:start + len(GOOGLE_WARNS)], GOOGLE_WARNS) |
| 112 | + |
| 113 | + def test_numpy_section_is_linked_too(self) -> 'None': |
| 114 | + """``NumpyDocstring`` inherits the parser, so its ``Warns`` section is linked.""" |
| 115 | + lines = self.numpy('Do something.\n\nWarns\n-----\nRegistryWarning\n If bare.\n') |
| 116 | + self.assertIn(':Warns: :exc:`.RegistryWarning` -- If bare.', lines) |
| 117 | + |
| 118 | + def test_name_with_a_type_is_left_alone(self) -> 'None': |
| 119 | + """The ``name (type): description`` form names no warning class by itself.""" |
| 120 | + lines = self.google('Do something.\n\nWarns:\n count (int): If typed.\n') |
| 121 | + self.assertIn(':Warns: **count** (:py:class:`int`) -- If typed.', lines) |
| 122 | + |
| 123 | + def test_a_role_is_kept_as_written(self) -> 'None': |
| 124 | + """An entry already written as a role is not wrapped in a second one.""" |
| 125 | + self.assertEqual(self.conf.warning_reference(':class:`~pcapkit.utilities.warnings.InfoWarning`'), |
| 126 | + ':class:`~pcapkit.utilities.warnings.InfoWarning`') |
| 127 | + |
| 128 | + def test_real_docstring(self) -> 'None': |
| 129 | + """``register_protocol``'s ``Warns:`` entry used to be ``**pcapkit.utilities.warnings.RegistryWarning**``.""" |
| 130 | + from pcapkit.foundation.registry.protocols import register_protocol # pylint: disable=import-outside-toplevel |
| 131 | + lines = self.google(inspect.getdoc(register_protocol)) |
| 132 | + warns = [line for line in lines if line.startswith(':Warns:')] |
| 133 | + self.assertEqual(len(warns), 1) |
| 134 | + self.assertTrue(warns[0].startswith(f':Warns: :exc:`.{REGISTRY_WARNING}` -- '), warns[0]) |
| 135 | + self.assertNotIn(f'**{REGISTRY_WARNING}**', '\n'.join(lines)) |
| 136 | + |
| 137 | + |
| 138 | +@unittest.skipUnless(HAS_SPHINX, SPHINX_SKIP_REASON) |
| 139 | +class RaisesParityTests(unittest.TestCase): |
| 140 | + """A ``Warns:`` role resolves to what a ``:raises:`` field of the same name does.""" |
| 141 | + |
| 142 | + conf = None # type: Any |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def setUpClass(cls) -> 'None': |
| 146 | + cls.conf = load_conf() |
| 147 | + |
| 148 | + def setUp(self) -> 'None': |
| 149 | + # pylint: disable=import-outside-toplevel |
| 150 | + from sphinx.domains.python import ObjectEntry, PythonDomain |
| 151 | + from sphinx.util.docutils import new_document |
| 152 | + |
| 153 | + self.env = types.SimpleNamespace( |
| 154 | + ref_context=dict(REF_CONTEXT), domaindata={}, |
| 155 | + config=types.SimpleNamespace(python_use_unqualified_type_names=False), |
| 156 | + current_document=types.SimpleNamespace(docname='index', default_role=None), |
| 157 | + ) |
| 158 | + self.domain = PythonDomain(self.env) |
| 159 | + self.env.get_domain = lambda name: self.domain |
| 160 | + self.domain.objects[REGISTRY_WARNING] = ObjectEntry('warnings', REGISTRY_WARNING, 'exception', False) |
| 161 | + self.document = new_document('<test>') |
| 162 | + self.document.settings.env = self.env |
| 163 | + |
| 164 | + def raises_field(self, name: 'str') -> 'Any': |
| 165 | + """The node a ``:raises name:`` field becomes, by the Python domain's field type.""" |
| 166 | + from sphinx.domains.python._object import PyGroupedField # pylint: disable=import-outside-toplevel |
| 167 | + field = PyGroupedField('exceptions', label='Raises', rolename='exc', |
| 168 | + names=('raises',), can_collapse=True) |
| 169 | + return field.make_xref('exc', 'py', name, env=self.env) |
| 170 | + |
| 171 | + def warns_role(self, name: 'str') -> 'Any': |
| 172 | + """The node the hook's role for ``name`` becomes.""" |
| 173 | + from sphinx.domains.python import PyXRefRole # pylint: disable=import-outside-toplevel |
| 174 | + text = EXC_ROLE.fullmatch(self.conf.warning_reference(name)).group(1) |
| 175 | + inliner = types.SimpleNamespace(document=self.document, reporter=types.SimpleNamespace( |
| 176 | + get_source_and_line=lambda lineno=None: ('<test>', lineno))) |
| 177 | + result, messages = PyXRefRole()('py:exc', f':py:exc:`{text}`', text, 1, inliner) |
| 178 | + self.assertEqual(messages, []) |
| 179 | + return result[0] |
| 180 | + |
| 181 | + def resolve(self, node: 'Any') -> 'Any': |
| 182 | + """What the Python domain links ``node`` to, after ``resolve_builtins_exactly``.""" |
| 183 | + from docutils import nodes # pylint: disable=import-outside-toplevel |
| 184 | + self.document += nodes.paragraph('', '', node) |
| 185 | + self.conf.resolve_builtins_exactly(None, self.document) |
| 186 | + builder = types.SimpleNamespace(name='html', get_relative_uri=lambda source, target: '') |
| 187 | + with mock.patch('sphinx.domains.python.logger'): |
| 188 | + return self.domain.resolve_xref(self.env, 'index', builder, node['reftype'], |
| 189 | + node['reftarget'], node, nodes.literal('', 'x')) |
| 190 | + |
| 191 | + def test_names_resolve_alike(self) -> 'None': |
| 192 | + """Bare, dotted and ``~``-prefixed, from a module other than the class's own.""" |
| 193 | + for name in ('RegistryWarning', REGISTRY_WARNING, f'~{REGISTRY_WARNING}'): |
| 194 | + with self.subTest(name=name): |
| 195 | + raises = self.resolve(self.raises_field(name)) |
| 196 | + warns = self.resolve(self.warns_role(name)) |
| 197 | + self.assertIsNotNone(raises) |
| 198 | + self.assertIsNotNone(warns) |
| 199 | + self.assertEqual(warns['reftitle'], raises['reftitle']) |
| 200 | + self.assertEqual(warns['reftitle'], REGISTRY_WARNING) |
| 201 | + |
| 202 | + def test_titles_read_alike(self) -> 'None': |
| 203 | + """``~`` shortens the title to the class name, and nothing else does.""" |
| 204 | + for name, title in (('RegistryWarning', 'RegistryWarning'), |
| 205 | + (REGISTRY_WARNING, REGISTRY_WARNING), |
| 206 | + (f'~{REGISTRY_WARNING}', 'RegistryWarning')): |
| 207 | + with self.subTest(name=name): |
| 208 | + self.assertEqual(self.raises_field(name).astext(), title) |
| 209 | + self.assertEqual(self.warns_role(name).astext(), title) |
| 210 | + |
| 211 | + def test_builtin_is_left_to_intersphinx(self) -> 'None': |
| 212 | + """A builtin warning is searched for exactly, so the Python docs link it.""" |
| 213 | + from sphinx.domains.python import ObjectEntry # pylint: disable=import-outside-toplevel |
| 214 | + lookalike = 'pcapkit.const.misc.UserWarning' |
| 215 | + self.domain.objects[lookalike] = ObjectEntry('misc', lookalike, 'attribute', False) |
| 216 | + self.assertIsNone(self.resolve(self.raises_field('UserWarning'))) |
| 217 | + self.assertIsNone(self.resolve(self.warns_role('UserWarning'))) |
| 218 | + |
| 219 | + |
| 220 | +if __name__ == '__main__': |
| 221 | + unittest.main() |
0 commit comments