| 1 |
"""Tests for :mod:`sphinx.ext.napoleon.__init__` module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import functools |
| 6 |
from collections import namedtuple |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from sphinx.application import Sphinx |
| 12 |
from sphinx.ext.napoleon import Config, _process_docstring, _skip_member, setup |
| 13 |
|
| 14 |
TYPE_CHECKING = False |
| 15 |
if TYPE_CHECKING: |
| 16 |
from collections.abc import Callable |
| 17 |
|
| 18 |
from sphinx.ext.autodoc._property_types import _AutodocObjType |
| 19 |
|
| 20 |
|
| 21 |
def simple_decorator[**P, R](f: Callable[P, R]) -> Callable[P, R]: |
| 22 |
"""A simple decorator that does nothing, for tests to use.""" |
| 23 |
|
| 24 |
@functools.wraps(f) |
| 25 |
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: |
| 26 |
return f(*args, **kwargs) |
| 27 |
|
| 28 |
return wrapper |
| 29 |
|
| 30 |
|
| 31 |
def _private_doc() -> None: |
| 32 |
"""module._private_doc.DOCSTRING""" |
| 33 |
pass |
| 34 |
|
| 35 |
|
| 36 |
def _private_undoc() -> None: |
| 37 |
pass |
| 38 |
|
| 39 |
|
| 40 |
def __special_doc__() -> None: # NoQA: N807 |
| 41 |
"""module.__special_doc__.DOCSTRING""" |
| 42 |
pass |
| 43 |
|
| 44 |
|
| 45 |
def __special_undoc__() -> None: # NoQA: N807 |
| 46 |
pass |
| 47 |
|
| 48 |
|
| 49 |
class SampleClass: |
| 50 |
def _private_doc(self) -> None: |
| 51 |
"""SampleClass._private_doc.DOCSTRING""" |
| 52 |
pass |
| 53 |
|
| 54 |
def _private_undoc(self) -> None: |
| 55 |
pass |
| 56 |
|
| 57 |
def __special_doc__(self) -> None: # NoQA: PLW3201 |
| 58 |
"""SampleClass.__special_doc__.DOCSTRING""" |
| 59 |
pass |
| 60 |
|
| 61 |
def __special_undoc__(self) -> None: # NoQA: PLW3201 |
| 62 |
pass |
| 63 |
|
| 64 |
@simple_decorator |
| 65 |
def __decorated_func__(self) -> None: # NoQA: PLW3201 |
| 66 |
"""Doc""" |
| 67 |
pass |
| 68 |
|
| 69 |
|
| 70 |
class SampleError(Exception): |
| 71 |
def _private_doc(self) -> None: |
| 72 |
"""SampleError._private_doc.DOCSTRING""" |
| 73 |
pass |
| 74 |
|
| 75 |
def _private_undoc(self) -> None: |
| 76 |
pass |
| 77 |
|
| 78 |
def __special_doc__(self) -> None: # NoQA: PLW3201 |
| 79 |
"""SampleError.__special_doc__.DOCSTRING""" |
| 80 |
pass |
| 81 |
|
| 82 |
def __special_undoc__(self) -> None: # NoQA: PLW3201 |
| 83 |
pass |
| 84 |
|
| 85 |
|
| 86 |
SampleNamedTuple = namedtuple('SampleNamedTuple', 'user_id block_type def_id') # NoQA: PYI024 |
| 87 |
|
| 88 |
|
| 89 |
class TestProcessDocstring: |
| 90 |
def test_modify_in_place(self) -> None: |
| 91 |
lines = [ |
| 92 |
'Summary line.', |
| 93 |
'', |
| 94 |
'Args:', |
| 95 |
' arg1: arg1 description', |
| 96 |
] |
| 97 |
app = mock.Mock() |
| 98 |
app.config = Config() |
| 99 |
_process_docstring( |
| 100 |
app, |
| 101 |
'class', |
| 102 |
'SampleClass', |
| 103 |
SampleClass, |
| 104 |
mock.Mock(), |
| 105 |
lines, |
| 106 |
) |
| 107 |
|
| 108 |
expected = [ |
| 109 |
'Summary line.', |
| 110 |
'', |
| 111 |
':param arg1: arg1 description', |
| 112 |
'', |
| 113 |
] |
| 114 |
assert lines == expected |
| 115 |
|
| 116 |
|
| 117 |
class TestSetup: |
| 118 |
def test_unknown_app_type(self) -> None: |
| 119 |
setup(object()) # type: ignore[arg-type] |
| 120 |
|
| 121 |
def test_add_config_values(self) -> None: |
| 122 |
app = mock.Mock(Sphinx) |
| 123 |
setup(app) |
| 124 |
for name, _default, _rebuild, _types in Config._config_values: |
| 125 |
has_config = False |
| 126 |
for method_name, args, _kwargs in app.method_calls: |
| 127 |
if method_name == 'add_config_value' and args[0] == name: |
| 128 |
has_config = True |
| 129 |
if not has_config: |
| 130 |
pytest.fail('Config value was not added to app %s' % name) |
| 131 |
|
| 132 |
has_process_docstring = False |
| 133 |
has_skip_member = False |
| 134 |
for method_name, args, _kwargs in app.method_calls: |
| 135 |
if method_name == 'connect': |
| 136 |
if ( |
| 137 |
args[0] == 'autodoc-process-docstring' |
| 138 |
and args[1] == _process_docstring |
| 139 |
): |
| 140 |
has_process_docstring = True |
| 141 |
elif args[0] == 'autodoc-skip-member' and args[1] == _skip_member: |
| 142 |
has_skip_member = True |
| 143 |
if not has_process_docstring: |
| 144 |
pytest.fail('autodoc-process-docstring never connected') |
| 145 |
if not has_skip_member: |
| 146 |
pytest.fail('autodoc-skip-member never connected') |
| 147 |
|
| 148 |
|
| 149 |
class TestSkipMember: |
| 150 |
def assert_skip( |
| 151 |
self, |
| 152 |
what: _AutodocObjType, |
| 153 |
member: str, |
| 154 |
obj: object, |
| 155 |
expect_default_skip: bool, |
| 156 |
config_name: str, |
| 157 |
) -> None: |
| 158 |
skip = True |
| 159 |
app = mock.Mock() |
| 160 |
app.config = Config() |
| 161 |
setattr(app.config, config_name, True) |
| 162 |
if expect_default_skip: |
| 163 |
assert None is _skip_member(app, what, member, obj, skip, mock.Mock()) |
| 164 |
else: |
| 165 |
assert _skip_member(app, what, member, obj, skip, mock.Mock()) is False |
| 166 |
setattr(app.config, config_name, False) |
| 167 |
assert None is _skip_member(app, what, member, obj, skip, mock.Mock()) |
| 168 |
|
| 169 |
def test_namedtuple(self) -> None: |
| 170 |
# Since python 3.7, namedtuple._asdict() has not been documented |
| 171 |
# because there is no way to check the method is a member of the |
| 172 |
# namedtuple class. This testcase confirms only it does not |
| 173 |
# raise an error on building document |
| 174 |
# See: https://github.com/sphinx-doc/sphinx/issues/1455 |
| 175 |
self.assert_skip( |
| 176 |
'class', |
| 177 |
'_asdict', |
| 178 |
SampleNamedTuple._asdict, |
| 179 |
True, |
| 180 |
'napoleon_include_private_with_doc', |
| 181 |
) |
| 182 |
|
| 183 |
def test_class_private_doc(self) -> None: |
| 184 |
self.assert_skip( |
| 185 |
'class', |
| 186 |
'_private_doc', |
| 187 |
SampleClass._private_doc, |
| 188 |
False, |
| 189 |
'napoleon_include_private_with_doc', |
| 190 |
) |
| 191 |
|
| 192 |
def test_class_private_undoc(self) -> None: |
| 193 |
self.assert_skip( |
| 194 |
'class', |
| 195 |
'_private_undoc', |
| 196 |
SampleClass._private_undoc, |
| 197 |
True, |
| 198 |
'napoleon_include_private_with_doc', |
| 199 |
) |
| 200 |
|
| 201 |
def test_class_special_doc(self) -> None: |
| 202 |
self.assert_skip( |
| 203 |
'class', |
| 204 |
'__special_doc__', |
| 205 |
SampleClass.__special_doc__, |
| 206 |
False, |
| 207 |
'napoleon_include_special_with_doc', |
| 208 |
) |
| 209 |
|
| 210 |
def test_class_special_undoc(self) -> None: |
| 211 |
self.assert_skip( |
| 212 |
'class', |
| 213 |
'__special_undoc__', |
| 214 |
SampleClass.__special_undoc__, |
| 215 |
True, |
| 216 |
'napoleon_include_special_with_doc', |
| 217 |
) |
| 218 |
|
| 219 |
def test_class_decorated_doc(self) -> None: |
| 220 |
self.assert_skip( |
| 221 |
'class', |
| 222 |
'__decorated_func__', |
| 223 |
SampleClass.__decorated_func__, |
| 224 |
False, |
| 225 |
'napoleon_include_special_with_doc', |
| 226 |
) |
| 227 |
|
| 228 |
def test_exception_private_doc(self) -> None: |
| 229 |
self.assert_skip( |
| 230 |
'exception', |
| 231 |
'_private_doc', |
| 232 |
SampleError._private_doc, |
| 233 |
False, |
| 234 |
'napoleon_include_private_with_doc', |
| 235 |
) |
| 236 |
|
| 237 |
def test_exception_private_undoc(self) -> None: |
| 238 |
self.assert_skip( |
| 239 |
'exception', |
| 240 |
'_private_undoc', |
| 241 |
SampleError._private_undoc, |
| 242 |
True, |
| 243 |
'napoleon_include_private_with_doc', |
| 244 |
) |
| 245 |
|
| 246 |
def test_exception_special_doc(self) -> None: |
| 247 |
self.assert_skip( |
| 248 |
'exception', |
| 249 |
'__special_doc__', |
| 250 |
SampleError.__special_doc__, |
| 251 |
False, |
| 252 |
'napoleon_include_special_with_doc', |
| 253 |
) |
| 254 |
|
| 255 |
def test_exception_special_undoc(self) -> None: |
| 256 |
self.assert_skip( |
| 257 |
'exception', |
| 258 |
'__special_undoc__', |
| 259 |
SampleError.__special_undoc__, |
| 260 |
True, |
| 261 |
'napoleon_include_special_with_doc', |
| 262 |
) |
| 263 |
|
| 264 |
def test_module_private_doc(self) -> None: |
| 265 |
self.assert_skip( |
| 266 |
'module', |
| 267 |
'_private_doc', |
| 268 |
_private_doc, |
| 269 |
False, |
| 270 |
'napoleon_include_private_with_doc', |
| 271 |
) |
| 272 |
|
| 273 |
def test_module_private_undoc(self) -> None: |
| 274 |
self.assert_skip( |
| 275 |
'module', |
| 276 |
'_private_undoc', |
| 277 |
_private_undoc, |
| 278 |
True, |
| 279 |
'napoleon_include_private_with_doc', |
| 280 |
) |
| 281 |
|
| 282 |
def test_module_special_doc(self) -> None: |
| 283 |
self.assert_skip( |
| 284 |
'module', |
| 285 |
'__special_doc__', |
| 286 |
__special_doc__, |
| 287 |
False, |
| 288 |
'napoleon_include_special_with_doc', |
| 289 |
) |
| 290 |
|
| 291 |
def test_module_special_undoc(self) -> None: |
| 292 |
self.assert_skip( |
| 293 |
'module', |
| 294 |
'__special_undoc__', |
| 295 |
__special_undoc__, |
| 296 |
True, |
| 297 |
'napoleon_include_special_with_doc', |
| 298 |
) |