| 1 |
"""Test the autosummary extension.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import sys |
| 6 |
from contextlib import chdir |
| 7 |
from io import StringIO |
| 8 |
from typing import TYPE_CHECKING |
| 9 |
from unittest.mock import Mock, patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
from docutils import nodes |
| 13 |
|
| 14 |
from sphinx import addnodes |
| 15 |
from sphinx.ext.autosummary import ( |
| 16 |
autosummary_table, |
| 17 |
autosummary_toc, |
| 18 |
extract_summary, |
| 19 |
import_by_name, |
| 20 |
mangle_signature, |
| 21 |
) |
| 22 |
from sphinx.ext.autosummary.generate import ( |
| 23 |
AutosummaryEntry, |
| 24 |
generate_autosummary_content, |
| 25 |
generate_autosummary_docs, |
| 26 |
) |
| 27 |
from sphinx.ext.autosummary.generate import main as autogen_main |
| 28 |
from sphinx.testing.util import assert_node, etree_parse |
| 29 |
|
| 30 |
from tests.utils import extract_node |
| 31 |
|
| 32 |
if TYPE_CHECKING: |
| 33 |
from xml.etree.ElementTree import Element |
| 34 |
|
| 35 |
if sys.version_info[:2] >= (3, 15): |
| 36 |
cached_dunder: tuple[str, ...] = () |
| 37 |
else: |
| 38 |
cached_dunder = ('__cached__',) |
| 39 |
|
| 40 |
|
| 41 |
html_warnfile = StringIO() |
| 42 |
|
| 43 |
|
| 44 |
defaults = { |
| 45 |
'extensions': ['sphinx.ext.autosummary'], |
| 46 |
'autosummary_generate': True, |
| 47 |
'autosummary_generate_overwrite': False, |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
@pytest.fixture(autouse=True) |
| 52 |
def _unload_target_module(): |
| 53 |
sys.modules.pop('target', None) |
| 54 |
|
| 55 |
|
| 56 |
def test_mangle_signature() -> None: |
| 57 |
TEST_SIGNATURE = """ |
| 58 |
() :: () |
| 59 |
(a, b, c, d, e) :: (a, b, c, d, e) |
| 60 |
(a, b, c=1, d=2, e=3) :: (a, b[, c, d, e]) |
| 61 |
(a, b, aaa=1, bbb=1, ccc=1, eee=1, fff=1, ggg=1, hhh=1, iii=1, jjj=1)\ |
| 62 |
:: (a, b[, aaa, bbb, ccc, ...]) |
| 63 |
(a, b, c=(), d=<foo>) :: (a, b[, c, d]) |
| 64 |
(a, b, c='foobar()', d=123) :: (a, b[, c, d]) |
| 65 |
(a, b[, c]) :: (a, b[, c]) |
| 66 |
(a, b[, cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx]) :: (a, b[, ...) |
| 67 |
(a, b='c=d, e=f, g=h', c=3) :: (a[, b, c]) |
| 68 |
(a, b="c=d, e=f, g=h", c=3) :: (a[, b, c]) |
| 69 |
(a, b='c=d, \\'e=f,\\' g=h', c=3) :: (a[, b, c]) |
| 70 |
(a, b='c=d, ', e='\\\\' g=h, c=3) :: (a[, b, e, c]) |
| 71 |
(a, b={'c=d, ': 3, '\\\\': 3}) :: (a[, b]) |
| 72 |
(a=1, b=2, c=3) :: ([a, b, c]) |
| 73 |
(a=1, b=<SomeClass: a, b, c>, c=3) :: ([a, b, c]) |
| 74 |
(a=1, b=T(a=1, b=2), c=3) :: ([a, b, c]) |
| 75 |
(a: Tuple[int, str], b: int) -> str :: (a, b) |
| 76 |
""" |
| 77 |
|
| 78 |
TEST = [ |
| 79 |
list(map(str.strip, x.split('::'))) |
| 80 |
for x in TEST_SIGNATURE.split('\n') |
| 81 |
if '::' in x |
| 82 |
] |
| 83 |
for inp, outp in TEST: |
| 84 |
res = mangle_signature(inp).strip().replace('\u00a0', ' ') |
| 85 |
assert res == outp, f"'{inp}' -> '{res}' != '{outp}'" |
| 86 |
|
| 87 |
|
| 88 |
def test_extract_summary(capsys): |
| 89 |
settings = Mock( |
| 90 |
language_code='en', |
| 91 |
id_prefix='', |
| 92 |
auto_id_prefix='', |
| 93 |
pep_reference=False, |
| 94 |
rfc_reference=False, |
| 95 |
) |
| 96 |
|
| 97 |
# normal case |
| 98 |
doc = [ |
| 99 |
'', |
| 100 |
'This is a first sentence. And second one.', |
| 101 |
'', |
| 102 |
'Second block is here', |
| 103 |
] |
| 104 |
assert extract_summary(doc, settings) == 'This is a first sentence.' |
| 105 |
|
| 106 |
# inliner case |
| 107 |
doc = [ |
| 108 |
'This sentence contains *emphasis text having dots.*,', |
| 109 |
'it does not break sentence.', |
| 110 |
] |
| 111 |
assert extract_summary(doc, settings) == ' '.join(doc) |
| 112 |
|
| 113 |
# abbreviations |
| 114 |
doc = ['Blabla, i.e. bla.'] |
| 115 |
assert extract_summary(doc, settings) == ' '.join(doc) |
| 116 |
|
| 117 |
doc = ['Blabla, (i.e. bla).'] |
| 118 |
assert extract_summary(doc, settings) == ' '.join(doc) |
| 119 |
|
| 120 |
doc = ['Blabla, e.g. bla.'] |
| 121 |
assert extract_summary(doc, settings) == ' '.join(doc) |
| 122 |
|
| 123 |
doc = ['Blabla, (e.g. bla).'] |
| 124 |
assert extract_summary(doc, settings) == ' '.join(doc) |
| 125 |
|
| 126 |
doc = ['Blabla, et al. bla.'] |
| 127 |
assert extract_summary(doc, settings) == ' '.join(doc) |
| 128 |
|
| 129 |
# literal |
| 130 |
doc = ['blah blah::'] |
| 131 |
assert extract_summary(doc, settings) == 'blah blah.' |
| 132 |
|
| 133 |
# heading |
| 134 |
doc = [ |
| 135 |
'blah blah', |
| 136 |
'=========', |
| 137 |
] |
| 138 |
assert extract_summary(doc, settings) == 'blah blah' |
| 139 |
|
| 140 |
doc = [ |
| 141 |
'=========', |
| 142 |
'blah blah', |
| 143 |
'=========', |
| 144 |
] |
| 145 |
assert extract_summary(doc, settings) == 'blah blah' |
| 146 |
|
| 147 |
# hyperlink target |
| 148 |
doc = ['Do `this <https://www.sphinx-doc.org/>`_ and that. blah blah blah.'] |
| 149 |
extracted = extract_summary(doc, settings) |
| 150 |
assert extracted == 'Do `this <https://www.sphinx-doc.org/>`_ and that.' |
| 151 |
|
| 152 |
_, err = capsys.readouterr() |
| 153 |
assert err == '' |
| 154 |
|
| 155 |
|
| 156 |
@pytest.mark.sphinx( |
| 157 |
'dummy', |
| 158 |
testroot='ext-autosummary-ext', |
| 159 |
confoverrides=defaults.copy(), |
| 160 |
copy_test_root=True, |
| 161 |
) |
| 162 |
def test_get_items_summary(make_app, app_params): |
| 163 |
import sphinx.ext.autosummary |
| 164 |
|
| 165 |
args, kwargs = app_params |
| 166 |
app = make_app(*args, **kwargs) |
| 167 |
# monkey-patch Autosummary.get_items so we can easily get access to it's |
| 168 |
# results.. |
| 169 |
orig_get_items = sphinx.ext.autosummary.Autosummary.get_items |
| 170 |
|
| 171 |
autosummary_items = {} |
| 172 |
|
| 173 |
def new_get_items(self, names, *args, **kwargs): |
| 174 |
results = orig_get_items(self, names, *args, **kwargs) |
| 175 |
for name, result in zip(names, results, strict=True): |
| 176 |
autosummary_items[name] = result # NoQA: PERF403 |
| 177 |
return results |
| 178 |
|
| 179 |
def handler(app, what, name, obj, options, lines): |
| 180 |
assert isinstance(lines, list) |
| 181 |
|
| 182 |
# ensure no docstring is processed twice: |
| 183 |
assert 'THIS HAS BEEN HANDLED' not in lines |
| 184 |
lines.append('THIS HAS BEEN HANDLED') |
| 185 |
|
| 186 |
app.connect('autodoc-process-docstring', handler) |
| 187 |
|
| 188 |
sphinx.ext.autosummary.Autosummary.get_items = new_get_items |
| 189 |
try: |
| 190 |
app.build(force_all=True) |
| 191 |
finally: |
| 192 |
sphinx.ext.autosummary.Autosummary.get_items = orig_get_items |
| 193 |
|
| 194 |
html_warnings = app._warning.getvalue() |
| 195 |
assert html_warnings == '' |
| 196 |
|
| 197 |
expected_values = { |
| 198 |
'with_sentence': 'I have a sentence which spans multiple lines.', |
| 199 |
'no_sentence': "this doesn't start with a capital.", |
| 200 |
'empty_line': 'This is the real summary', |
| 201 |
'module_attr': 'This is a module attribute', |
| 202 |
'C.class_attr': 'This is a class attribute', |
| 203 |
'C.instance_attr': 'This is an instance attribute', |
| 204 |
'C.prop_attr1': 'This is a function docstring', |
| 205 |
'C.prop_attr2': 'This is a attribute docstring', |
| 206 |
'C.C2': 'This is a nested inner class docstring', |
| 207 |
} |
| 208 |
for key, expected in expected_values.items(): |
| 209 |
assert autosummary_items[key][2] == expected, ( |
| 210 |
f'Summary for {key} was {autosummary_items[key]!r} - expected {expected!r}' |
| 211 |
) |
| 212 |
|
| 213 |
# check an item in detail |
| 214 |
assert 'func' in autosummary_items |
| 215 |
func_attrs = ( |
| 216 |
'func', |
| 217 |
'(arg_, *args, **kwargs)', |
| 218 |
'Test function take an argument ended with underscore.', |
| 219 |
'dummy_module.func', |
| 220 |
) |
| 221 |
assert autosummary_items['func'] == func_attrs |
| 222 |
|
| 223 |
|
| 224 |
def str_content(elem: Element) -> str: |
| 225 |
if elem.text is not None: |
| 226 |
return elem.text |
| 227 |
else: |
| 228 |
return ''.join(str_content(e) for e in elem) |
| 229 |
|
| 230 |
|
| 231 |
@pytest.mark.sphinx( |
| 232 |
'xml', |
| 233 |
testroot='ext-autosummary-ext', |
| 234 |
confoverrides=defaults.copy(), |
| 235 |
copy_test_root=True, |
| 236 |
) |
| 237 |
def test_escaping(app): |
| 238 |
app.build(force_all=True) |
| 239 |
|
| 240 |
docpage = app.builder.outdir / 'underscore_module_.xml' |
| 241 |
assert docpage.exists() |
| 242 |
|
| 243 |
title = etree_parse(docpage).find('section/title') |
| 244 |
assert str_content(title) == 'underscore_module_' |
| 245 |
|
| 246 |
|
| 247 |
@pytest.mark.sphinx('html', testroot='ext-autosummary', copy_test_root=True) |
| 248 |
def test_autosummary_generate_content_for_module(app): |
| 249 |
import autosummary_dummy_module # type: ignore[import-not-found] |
| 250 |
|
| 251 |
template = Mock() |
| 252 |
|
| 253 |
generate_autosummary_content( |
| 254 |
'autosummary_dummy_module', |
| 255 |
autosummary_dummy_module, |
| 256 |
None, |
| 257 |
template, |
| 258 |
None, |
| 259 |
False, |
| 260 |
False, |
| 261 |
{}, |
| 262 |
config=app.config, |
| 263 |
events=app.events, |
| 264 |
) |
| 265 |
assert template.render.call_args[0][0] == 'module' |
| 266 |
|
| 267 |
context = template.render.call_args[0][1] |
| 268 |
assert context['members'] == [ |
| 269 |
'CONSTANT1', |
| 270 |
'CONSTANT2', |
| 271 |
'Exc', |
| 272 |
'Foo', |
| 273 |
'_Baz', |
| 274 |
'_Exc', |
| 275 |
'__all__', |
| 276 |
'__builtins__', |
| 277 |
*cached_dunder, |
| 278 |
'__doc__', |
| 279 |
'__file__', |
| 280 |
'__name__', |
| 281 |
'__package__', |
| 282 |
'_quux', |
| 283 |
'bar', |
| 284 |
'non_imported_member', |
| 285 |
'quuz', |
| 286 |
'qux', |
| 287 |
] |
| 288 |
assert context['functions'] == ['bar'] |
| 289 |
assert context['all_functions'] == ['_quux', 'bar'] |
| 290 |
assert context['classes'] == ['Foo'] |
| 291 |
assert context['all_classes'] == ['Foo', '_Baz'] |
| 292 |
assert context['exceptions'] == ['Exc'] |
| 293 |
assert context['all_exceptions'] == ['Exc', '_Exc'] |
| 294 |
assert context['attributes'] == ['CONSTANT1', 'qux', 'quuz', 'non_imported_member'] |
| 295 |
assert context['all_attributes'] == [ |
| 296 |
'CONSTANT1', |
| 297 |
'qux', |
| 298 |
'quuz', |
| 299 |
'non_imported_member', |
| 300 |
] |
| 301 |
assert context['fullname'] == 'autosummary_dummy_module' |
| 302 |
assert context['module'] == 'autosummary_dummy_module' |
| 303 |
assert context['objname'] == '' |
| 304 |
assert context['name'] == '' |
| 305 |
assert context['objtype'] == 'module' |
| 306 |
|
| 307 |
|
| 308 |
@pytest.mark.sphinx('html', testroot='ext-autosummary', copy_test_root=True) |
| 309 |
def test_autosummary_generate_content_for_module___all__(app): |
| 310 |
import autosummary_dummy_module # ty: ignore[unresolved-import] |
| 311 |
|
| 312 |
template = Mock() |
| 313 |
app.config.autosummary_ignore_module_all = False |
| 314 |
|
| 315 |
generate_autosummary_content( |
| 316 |
'autosummary_dummy_module', |
| 317 |
autosummary_dummy_module, |
| 318 |
None, |
| 319 |
template, |
| 320 |
None, |
| 321 |
False, |
| 322 |
False, |
| 323 |
{}, |
| 324 |
config=app.config, |
| 325 |
events=app.events, |
| 326 |
) |
| 327 |
assert template.render.call_args[0][0] == 'module' |
| 328 |
|
| 329 |
context = template.render.call_args[0][1] |
| 330 |
assert context['members'] == [ |
| 331 |
'CONSTANT1', |
| 332 |
'Exc', |
| 333 |
'Foo', |
| 334 |
'_Baz', |
| 335 |
'bar', |
| 336 |
'qux', |
| 337 |
'path', |
| 338 |
] |
| 339 |
assert context['functions'] == ['bar'] |
| 340 |
assert context['all_functions'] == ['bar'] |
| 341 |
assert context['classes'] == ['Foo'] |
| 342 |
assert context['all_classes'] == ['Foo', '_Baz'] |
| 343 |
assert context['exceptions'] == ['Exc'] |
| 344 |
assert context['all_exceptions'] == ['Exc'] |
| 345 |
assert context['attributes'] == ['CONSTANT1', 'qux'] |
| 346 |
assert context['all_attributes'] == ['CONSTANT1', 'qux'] |
| 347 |
assert context['fullname'] == 'autosummary_dummy_module' |
| 348 |
assert context['module'] == 'autosummary_dummy_module' |
| 349 |
assert context['objname'] == '' |
| 350 |
assert context['name'] == '' |
| 351 |
assert context['objtype'] == 'module' |
| 352 |
|
| 353 |
|
| 354 |
@pytest.mark.sphinx('html', testroot='ext-autosummary', copy_test_root=True) |
| 355 |
def test_autosummary_generate_content_for_module_skipped(app): |
| 356 |
import autosummary_dummy_module # ty: ignore[unresolved-import] |
| 357 |
|
| 358 |
template = Mock() |
| 359 |
|
| 360 |
def skip_member(app, what, name, obj, skip, options): |
| 361 |
if name in {'Foo', 'bar', 'Exc'}: |
| 362 |
return True |
| 363 |
return None |
| 364 |
|
| 365 |
app.connect('autodoc-skip-member', skip_member) |
| 366 |
generate_autosummary_content( |
| 367 |
'autosummary_dummy_module', |
| 368 |
autosummary_dummy_module, |
| 369 |
None, |
| 370 |
template, |
| 371 |
None, |
| 372 |
False, |
| 373 |
False, |
| 374 |
{}, |
| 375 |
config=app.config, |
| 376 |
events=app.events, |
| 377 |
) |
| 378 |
context = template.render.call_args[0][1] |
| 379 |
assert context['members'] == [ |
| 380 |
'CONSTANT1', |
| 381 |
'CONSTANT2', |
| 382 |
'_Baz', |
| 383 |
'_Exc', |
| 384 |
'__all__', |
| 385 |
'__builtins__', |
| 386 |
*cached_dunder, |
| 387 |
'__doc__', |
| 388 |
'__file__', |
| 389 |
'__name__', |
| 390 |
'__package__', |
| 391 |
'_quux', |
| 392 |
'non_imported_member', |
| 393 |
'quuz', |
| 394 |
'qux', |
| 395 |
] |
| 396 |
assert context['functions'] == [] |
| 397 |
assert context['classes'] == [] |
| 398 |
assert context['exceptions'] == [] |
| 399 |
|
| 400 |
|
| 401 |
@pytest.mark.sphinx('html', testroot='ext-autosummary', copy_test_root=True) |
| 402 |
def test_autosummary_generate_content_for_module_imported_members(app): |
| 403 |
import autosummary_dummy_module # ty: ignore[unresolved-import] |
| 404 |
|
| 405 |
template = Mock() |
| 406 |
|
| 407 |
generate_autosummary_content( |
| 408 |
'autosummary_dummy_module', |
| 409 |
autosummary_dummy_module, |
| 410 |
None, |
| 411 |
template, |
| 412 |
None, |
| 413 |
True, |
| 414 |
False, |
| 415 |
{}, |
| 416 |
config=app.config, |
| 417 |
events=app.events, |
| 418 |
) |
| 419 |
assert template.render.call_args[0][0] == 'module' |
| 420 |
|
| 421 |
context = template.render.call_args[0][1] |
| 422 |
assert context['members'] == [ |
| 423 |
'CONSTANT1', |
| 424 |
'CONSTANT2', |
| 425 |
'Class', |
| 426 |
'Exc', |
| 427 |
'Foo', |
| 428 |
'Union', |
| 429 |
'_Baz', |
| 430 |
'_Exc', |
| 431 |
'__all__', |
| 432 |
'__builtins__', |
| 433 |
*cached_dunder, |
| 434 |
'__doc__', |
| 435 |
'__file__', |
| 436 |
'__loader__', |
| 437 |
'__name__', |
| 438 |
'__package__', |
| 439 |
'__spec__', |
| 440 |
'_quux', |
| 441 |
'bar', |
| 442 |
'considered_as_imported', |
| 443 |
'non_imported_member', |
| 444 |
'path', |
| 445 |
'quuz', |
| 446 |
'qux', |
| 447 |
] |
| 448 |
assert context['functions'] == ['bar'] |
| 449 |
assert context['all_functions'] == ['_quux', 'bar'] |
| 450 |
if sys.version_info >= (3, 14, 0, 'alpha', 5): |
| 451 |
assert context['classes'] == ['Class', 'Foo', 'Union'] |
| 452 |
assert context['all_classes'] == ['Class', 'Foo', 'Union', '_Baz'] |
| 453 |
else: |
| 454 |
assert context['classes'] == ['Class', 'Foo'] |
| 455 |
assert context['all_classes'] == ['Class', 'Foo', '_Baz'] |
| 456 |
assert context['exceptions'] == ['Exc'] |
| 457 |
assert context['all_exceptions'] == ['Exc', '_Exc'] |
| 458 |
assert context['attributes'] == ['CONSTANT1', 'qux', 'quuz', 'non_imported_member'] |
| 459 |
assert context['all_attributes'] == [ |
| 460 |
'CONSTANT1', |
| 461 |
'qux', |
| 462 |
'quuz', |
| 463 |
'non_imported_member', |
| 464 |
] |
| 465 |
assert context['fullname'] == 'autosummary_dummy_module' |
| 466 |
assert context['module'] == 'autosummary_dummy_module' |
| 467 |
assert context['objname'] == '' |
| 468 |
assert context['name'] == '' |
| 469 |
assert context['objtype'] == 'module' |
| 470 |
|
| 471 |
|
| 472 |
@pytest.mark.sphinx('html', testroot='ext-autosummary', copy_test_root=True) |
| 473 |
def test_autosummary_generate_content_for_module_imported_members_inherited_module(app): |
| 474 |
import autosummary_dummy_inherited_module # type: ignore[import-not-found] |
| 475 |
|
| 476 |
template = Mock() |
| 477 |
|
| 478 |
generate_autosummary_content( |
| 479 |
'autosummary_dummy_inherited_module', |
| 480 |
autosummary_dummy_inherited_module, |
| 481 |
None, |
| 482 |
template, |
| 483 |
None, |
| 484 |
True, |
| 485 |
False, |
| 486 |
{}, |
| 487 |
config=app.config, |
| 488 |
events=app.events, |
| 489 |
) |
| 490 |
assert template.render.call_args[0][0] == 'module' |
| 491 |
|
| 492 |
context = template.render.call_args[0][1] |
| 493 |
assert context['members'] == [ |
| 494 |
'Foo', |
| 495 |
'InheritedAttrClass', |
| 496 |
'__all__', |
| 497 |
'__builtins__', |
| 498 |
*cached_dunder, |
| 499 |
'__doc__', |
| 500 |
'__file__', |
| 501 |
'__loader__', |
| 502 |
'__name__', |
| 503 |
'__package__', |
| 504 |
'__spec__', |
| 505 |
] |
| 506 |
assert context['functions'] == [] |
| 507 |
assert context['classes'] == ['Foo', 'InheritedAttrClass'] |
| 508 |
assert context['exceptions'] == [] |
| 509 |
assert context['all_exceptions'] == [] |
| 510 |
assert context['attributes'] == [] |
| 511 |
assert context['all_attributes'] == [] |
| 512 |
assert context['fullname'] == 'autosummary_dummy_inherited_module' |
| 513 |
assert context['module'] == 'autosummary_dummy_inherited_module' |
| 514 |
assert context['objname'] == '' |
| 515 |
assert context['name'] == '' |
| 516 |
assert context['objtype'] == 'module' |
| 517 |
|
| 518 |
|
| 519 |
@pytest.mark.sphinx('dummy', testroot='ext-autosummary', copy_test_root=True) |
| 520 |
def test_autosummary_generate(app): |
| 521 |
app.build(force_all=True) |
| 522 |
|
| 523 |
doctree = app.env.get_doctree('index') |
| 524 |
assert_node( |
| 525 |
doctree, |
| 526 |
( |
| 527 |
nodes.paragraph, |
| 528 |
nodes.paragraph, |
| 529 |
addnodes.tabular_col_spec, |
| 530 |
autosummary_table, |
| 531 |
[autosummary_toc, addnodes.toctree], |
| 532 |
), |
| 533 |
) |
| 534 |
assert_node( |
| 535 |
doctree[3], |
| 536 |
[ |
| 537 |
autosummary_table, |
| 538 |
nodes.table, |
| 539 |
nodes.tgroup, |
| 540 |
( |
| 541 |
nodes.colspec, |
| 542 |
nodes.colspec, |
| 543 |
[ |
| 544 |
nodes.tbody, |
| 545 |
( |
| 546 |
nodes.row, |
| 547 |
nodes.row, |
| 548 |
nodes.row, |
| 549 |
nodes.row, |
| 550 |
nodes.row, |
| 551 |
nodes.row, |
| 552 |
nodes.row, |
| 553 |
nodes.row, |
| 554 |
), |
| 555 |
], |
| 556 |
), |
| 557 |
], |
| 558 |
) |
| 559 |
assert_node(extract_node(doctree, 4, 0), addnodes.toctree, caption='An autosummary') |
| 560 |
|
| 561 |
assert len(extract_node(doctree, 3, 0, 0, 2)) == 8 |
| 562 |
assert extract_node(doctree, 3, 0, 0, 2, 0).astext() == ( |
| 563 |
'autosummary_dummy_module\n\n' |
| 564 |
) |
| 565 |
assert extract_node(doctree, 3, 0, 0, 2, 1).astext() == ( |
| 566 |
'autosummary_dummy_module.Foo()\n\n' |
| 567 |
) |
| 568 |
assert extract_node(doctree, 3, 0, 0, 2, 2).astext() == ( |
| 569 |
'autosummary_dummy_module.Foo.Bar()\n\n' |
| 570 |
) |
| 571 |
assert extract_node(doctree, 3, 0, 0, 2, 3).astext() == ( |
| 572 |
'autosummary_dummy_module.Foo.value\n\ndocstring' |
| 573 |
) |
| 574 |
assert extract_node(doctree, 3, 0, 0, 2, 4).astext() == ( |
| 575 |
'autosummary_dummy_module.bar(x[, y])\n\n' |
| 576 |
) |
| 577 |
assert extract_node(doctree, 3, 0, 0, 2, 5).astext() == ( |
| 578 |
'autosummary_dummy_module.qux\n\na module-level attribute' |
| 579 |
) |
| 580 |
assert extract_node(doctree, 3, 0, 0, 2, 6).astext() == ( |
| 581 |
'autosummary_dummy_inherited_module.InheritedAttrClass()\n\n' |
| 582 |
) |
| 583 |
assert extract_node(doctree, 3, 0, 0, 2, 7).astext() == ( |
| 584 |
'autosummary_dummy_inherited_module.InheritedAttrClass.subclassattr\n\nother docstring' |
| 585 |
) |
| 586 |
|
| 587 |
path = app.srcdir / 'generated' / 'autosummary_dummy_module.rst' |
| 588 |
module = path.read_text(encoding='utf8') |
| 589 |
|
| 590 |
assert ' .. autosummary::\n \n Foo\n \n' in module |
| 591 |
assert ( |
| 592 |
' .. autosummary::\n' |
| 593 |
' \n' |
| 594 |
' CONSTANT1\n' |
| 595 |
' qux\n' |
| 596 |
' quuz\n' |
| 597 |
' non_imported_member\n' |
| 598 |
' \n' |
| 599 |
) in module |
| 600 |
|
| 601 |
path = app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.rst' |
| 602 |
Foo = path.read_text(encoding='utf8') |
| 603 |
assert '.. automethod:: __init__' in Foo |
| 604 |
assert ( |
| 605 |
' .. autosummary::\n \n ~Foo.__init__\n ~Foo.bar\n \n' |
| 606 |
) in Foo |
| 607 |
assert ( |
| 608 |
' .. autosummary::\n' |
| 609 |
' \n' |
| 610 |
' ~Foo.CONSTANT3\n' |
| 611 |
' ~Foo.CONSTANT4\n' |
| 612 |
' ~Foo.baz\n' |
| 613 |
' ~Foo.value\n' |
| 614 |
' \n' |
| 615 |
) in Foo |
| 616 |
|
| 617 |
path = app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.Bar.rst' |
| 618 |
FooBar = path.read_text(encoding='utf8') |
| 619 |
assert ( |
| 620 |
'.. currentmodule:: autosummary_dummy_module\n\n.. autoclass:: Foo.Bar\n' |
| 621 |
) in FooBar |
| 622 |
|
| 623 |
path = app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.value.rst' |
| 624 |
Foo_value = path.read_text(encoding='utf8') |
| 625 |
assert ( |
| 626 |
'.. currentmodule:: autosummary_dummy_module\n\n.. autoattribute:: Foo.value' |
| 627 |
) in Foo_value |
| 628 |
|
| 629 |
path = app.srcdir / 'generated' / 'autosummary_dummy_module.qux.rst' |
| 630 |
qux = path.read_text(encoding='utf8') |
| 631 |
assert '.. currentmodule:: autosummary_dummy_module\n\n.. autodata:: qux' in qux |
| 632 |
|
| 633 |
path = ( |
| 634 |
app.srcdir |
| 635 |
/ 'generated' |
| 636 |
/ 'autosummary_dummy_inherited_module.InheritedAttrClass.rst' |
| 637 |
) |
| 638 |
InheritedAttrClass = path.read_text(encoding='utf8') |
| 639 |
print(InheritedAttrClass) |
| 640 |
assert '.. automethod:: __init__' in Foo |
| 641 |
assert ( |
| 642 |
' .. autosummary::\n' |
| 643 |
' \n' |
| 644 |
' ~InheritedAttrClass.__init__\n' |
| 645 |
' ~InheritedAttrClass.bar\n' |
| 646 |
' \n' |
| 647 |
) in InheritedAttrClass |
| 648 |
assert ( |
| 649 |
' .. autosummary::\n' |
| 650 |
' \n' |
| 651 |
' ~InheritedAttrClass.CONSTANT3\n' |
| 652 |
' ~InheritedAttrClass.CONSTANT4\n' |
| 653 |
' ~InheritedAttrClass.baz\n' |
| 654 |
' ~InheritedAttrClass.subclassattr\n' |
| 655 |
' ~InheritedAttrClass.value\n' |
| 656 |
' \n' |
| 657 |
) in InheritedAttrClass |
| 658 |
|
| 659 |
path = ( |
| 660 |
app.srcdir |
| 661 |
/ 'generated' |
| 662 |
/ 'autosummary_dummy_inherited_module.InheritedAttrClass.subclassattr.rst' |
| 663 |
) |
| 664 |
InheritedAttrClass_subclassattr = path.read_text(encoding='utf8') |
| 665 |
assert ( |
| 666 |
'.. currentmodule:: autosummary_dummy_inherited_module\n' |
| 667 |
'\n' |
| 668 |
'.. autoattribute:: InheritedAttrClass.subclassattr' |
| 669 |
) in InheritedAttrClass_subclassattr |
| 670 |
|
| 671 |
|
| 672 |
@pytest.mark.sphinx( |
| 673 |
'dummy', |
| 674 |
testroot='ext-autosummary', |
| 675 |
confoverrides={'autosummary_generate_overwrite': False}, |
| 676 |
copy_test_root=True, |
| 677 |
) |
| 678 |
def test_autosummary_generate_overwrite1(app_params, make_app): |
| 679 |
args, kwargs = app_params |
| 680 |
srcdir = kwargs.get('srcdir') |
| 681 |
|
| 682 |
(srcdir / 'generated').mkdir(parents=True, exist_ok=True) |
| 683 |
(srcdir / 'generated' / 'autosummary_dummy_module.rst').write_text('', encoding='utf8') # fmt: skip |
| 684 |
|
| 685 |
app = make_app(*args, **kwargs) |
| 686 |
path = srcdir / 'generated' / 'autosummary_dummy_module.rst' |
| 687 |
content = path.read_text(encoding='utf8') |
| 688 |
assert content == '' |
| 689 |
assert 'autosummary_dummy_module.rst' not in app._warning.getvalue() |
| 690 |
|
| 691 |
|
| 692 |
@pytest.mark.sphinx( |
| 693 |
'dummy', |
| 694 |
testroot='ext-autosummary', |
| 695 |
confoverrides={'autosummary_generate_overwrite': True}, |
| 696 |
copy_test_root=True, |
| 697 |
) |
| 698 |
def test_autosummary_generate_overwrite2(app_params, make_app): |
| 699 |
args, kwargs = app_params |
| 700 |
srcdir = kwargs.get('srcdir') |
| 701 |
|
| 702 |
(srcdir / 'generated').mkdir(parents=True, exist_ok=True) |
| 703 |
(srcdir / 'generated' / 'autosummary_dummy_module.rst').write_text('', encoding='utf8') # fmt: skip |
| 704 |
|
| 705 |
app = make_app(*args, **kwargs) |
| 706 |
path = srcdir / 'generated' / 'autosummary_dummy_module.rst' |
| 707 |
content = path.read_text(encoding='utf8') |
| 708 |
assert content != '' |
| 709 |
assert 'autosummary_dummy_module.rst' not in app._warning.getvalue() |
| 710 |
|
| 711 |
|
| 712 |
@pytest.mark.sphinx('dummy', testroot='ext-autosummary-recursive', copy_test_root=True) |
| 713 |
@pytest.mark.usefixtures('rollback_sysmodules') |
| 714 |
def test_autosummary_recursive(app): |
| 715 |
sys.modules.pop('package', None) # unload target module to clear the module cache |
| 716 |
|
| 717 |
app.build() |
| 718 |
|
| 719 |
# autosummary having :recursive: option |
| 720 |
assert (app.srcdir / 'generated' / 'package.rst').exists() |
| 721 |
assert (app.srcdir / 'generated' / 'package.module.rst').exists() |
| 722 |
assert not (app.srcdir / 'generated' / 'package.module_importfail.rst').exists() |
| 723 |
assert (app.srcdir / 'generated' / 'package.package.rst').exists() |
| 724 |
assert (app.srcdir / 'generated' / 'package.package.module.rst').exists() |
| 725 |
|
| 726 |
# autosummary not having :recursive: option |
| 727 |
assert (app.srcdir / 'generated' / 'package2.rst').exists() |
| 728 |
assert not (app.srcdir / 'generated' / 'package2.module.rst').exists() |
| 729 |
|
| 730 |
# Check content of recursively generated stub-files |
| 731 |
content = (app.srcdir / 'generated' / 'package.rst').read_text(encoding='utf8') |
| 732 |
assert 'module' in content |
| 733 |
assert 'package' in content |
| 734 |
assert 'module_importfail' in content |
| 735 |
# we no longer generate fully-qualified module names. |
| 736 |
assert 'package.module' not in content |
| 737 |
assert 'package.package' not in content |
| 738 |
assert 'package.module_importfail' not in content |
| 739 |
|
| 740 |
path = app.srcdir / 'generated' / 'package.package.rst' |
| 741 |
content = path.read_text(encoding='utf8') |
| 742 |
assert 'module' in content |
| 743 |
assert 'package.package.module' not in content |
| 744 |
|
| 745 |
warnings = app.warning.getvalue() |
| 746 |
assert 'Summarised items should not include the current module.' not in warnings |
| 747 |
|
| 748 |
|
| 749 |
@pytest.mark.sphinx( |
| 750 |
'dummy', |
| 751 |
testroot='ext-autosummary-recursive', |
| 752 |
srcdir='test_autosummary_recursive_skips_mocked_modules', |
| 753 |
confoverrides={'autosummary_mock_imports': ['package.package']}, |
| 754 |
) |
| 755 |
@pytest.mark.usefixtures('rollback_sysmodules') |
| 756 |
def test_autosummary_recursive_skips_mocked_modules(app): |
| 757 |
sys.modules.pop('package', None) # unload target module to clear the module cache |
| 758 |
app.build() |
| 759 |
|
| 760 |
assert (app.srcdir / 'generated' / 'package.rst').exists() |
| 761 |
assert (app.srcdir / 'generated' / 'package.module.rst').exists() |
| 762 |
assert not (app.srcdir / 'generated' / 'package.package.rst').exists() |
| 763 |
assert not (app.srcdir / 'generated' / 'package.package.module.rst').exists() |
| 764 |
|
| 765 |
|
| 766 |
@pytest.mark.sphinx( |
| 767 |
'dummy', |
| 768 |
testroot='ext-autosummary-filename-map', |
| 769 |
copy_test_root=True, |
| 770 |
) |
| 771 |
def test_autosummary_filename_map(app): |
| 772 |
app.build() |
| 773 |
|
| 774 |
assert (app.srcdir / 'generated' / 'module_mangled.rst').exists() |
| 775 |
assert not (app.srcdir / 'generated' / 'autosummary_dummy_module.rst').exists() |
| 776 |
assert (app.srcdir / 'generated' / 'bar.rst').exists() |
| 777 |
assert not (app.srcdir / 'generated' / 'autosummary_dummy_module.bar.rst').exists() |
| 778 |
assert (app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.rst').exists() |
| 779 |
|
| 780 |
html_warnings = app._warning.getvalue() |
| 781 |
assert html_warnings == '' |
| 782 |
|
| 783 |
|
| 784 |
@pytest.mark.sphinx( |
| 785 |
'latex', |
| 786 |
testroot='ext-autosummary-ext', |
| 787 |
confoverrides=defaults.copy(), |
| 788 |
copy_test_root=True, |
| 789 |
) |
| 790 |
def test_autosummary_latex_table_colspec(app): |
| 791 |
app.build(force_all=True) |
| 792 |
result = (app.outdir / 'projectnamenotset.tex').read_text(encoding='utf8') |
| 793 |
print(app.status.getvalue()) |
| 794 |
print(app.warning.getvalue()) |
| 795 |
assert r'\begin{longtable}{\X{1}{2}\X{1}{2}}' in result |
| 796 |
assert r'p{0.5\linewidth}' not in result |
| 797 |
|
| 798 |
|
| 799 |
def test_import_by_name() -> None: |
| 800 |
import sphinx |
| 801 |
import sphinx.ext.autosummary |
| 802 |
|
| 803 |
prefixed_name, obj, parent, modname = import_by_name('sphinx') |
| 804 |
assert prefixed_name == 'sphinx' |
| 805 |
assert obj is sphinx |
| 806 |
assert parent is None |
| 807 |
assert modname == 'sphinx' |
| 808 |
|
| 809 |
prefixed_name, obj, parent, modname = import_by_name( |
| 810 |
'sphinx.ext.autosummary.__name__' |
| 811 |
) |
| 812 |
assert prefixed_name == 'sphinx.ext.autosummary.__name__' |
| 813 |
assert obj is sphinx.ext.autosummary.__name__ |
| 814 |
assert parent is sphinx.ext.autosummary |
| 815 |
assert modname == 'sphinx.ext.autosummary' |
| 816 |
|
| 817 |
prefixed_name, obj, parent, modname = import_by_name( |
| 818 |
'sphinx.ext.autosummary.Autosummary.get_items' |
| 819 |
) |
| 820 |
assert prefixed_name == 'sphinx.ext.autosummary.Autosummary.get_items' |
| 821 |
assert obj == sphinx.ext.autosummary.Autosummary.get_items |
| 822 |
assert parent is sphinx.ext.autosummary.Autosummary |
| 823 |
assert modname == 'sphinx.ext.autosummary' |
| 824 |
|
| 825 |
|
| 826 |
@pytest.mark.sphinx( |
| 827 |
'dummy', |
| 828 |
testroot='ext-autosummary-mock_imports', |
| 829 |
copy_test_root=True, |
| 830 |
) |
| 831 |
def test_autosummary_mock_imports(app): |
| 832 |
try: |
| 833 |
app.build() |
| 834 |
assert app.warning.getvalue() == '' |
| 835 |
|
| 836 |
# generated/foo is generated successfully |
| 837 |
assert app.env.get_doctree('generated/foo') |
| 838 |
finally: |
| 839 |
sys.modules.pop('foo', None) # unload foo module |
| 840 |
|
| 841 |
|
| 842 |
@pytest.mark.sphinx( |
| 843 |
'dummy', |
| 844 |
testroot='ext-autosummary-imported_members', |
| 845 |
copy_test_root=True, |
| 846 |
) |
| 847 |
def test_autosummary_imported_members(app): |
| 848 |
try: |
| 849 |
app.build() |
| 850 |
# generated/foo is generated successfully |
| 851 |
assert app.env.get_doctree('generated/autosummary_dummy_package') |
| 852 |
|
| 853 |
path = app.srcdir / 'generated' / 'autosummary_dummy_package.rst' |
| 854 |
module = path.read_text(encoding='utf8') |
| 855 |
assert ' .. autosummary::\n \n Bar\n ' in module |
| 856 |
assert ' .. autosummary::\n \n foo\n ' in module |
| 857 |
finally: |
| 858 |
sys.modules.pop('autosummary_dummy_package', None) |
| 859 |
|
| 860 |
|
| 861 |
@pytest.mark.sphinx( |
| 862 |
'dummy', |
| 863 |
testroot='ext-autosummary-module_all', |
| 864 |
copy_test_root=True, |
| 865 |
) |
| 866 |
def test_autosummary_module_all(app): |
| 867 |
try: |
| 868 |
app.build() |
| 869 |
# generated/foo is generated successfully |
| 870 |
assert app.env.get_doctree('generated/autosummary_dummy_package_all') |
| 871 |
path = app.srcdir / 'generated' / 'autosummary_dummy_package_all.rst' |
| 872 |
module = path.read_text(encoding='utf8') |
| 873 |
assert ' .. autosummary::\n \n PublicBar\n \n' in module |
| 874 |
assert ( |
| 875 |
' .. autosummary::\n \n public_foo\n public_baz\n \n' |
| 876 |
) in module |
| 877 |
assert ( |
| 878 |
'.. autosummary::\n :toctree:\n :recursive:\n\n extra_dummy_module\n' |
| 879 |
) in module |
| 880 |
finally: |
| 881 |
sys.modules.pop('autosummary_dummy_package_all', None) |
| 882 |
|
| 883 |
|
| 884 |
@pytest.mark.sphinx( |
| 885 |
'dummy', |
| 886 |
testroot='ext-autosummary-module_empty_all', |
| 887 |
copy_test_root=True, |
| 888 |
) |
| 889 |
def test_autosummary_module_empty_all(app): |
| 890 |
try: |
| 891 |
app.build() |
| 892 |
# generated/foo is generated successfully |
| 893 |
assert app.env.get_doctree('generated/autosummary_dummy_package_empty_all') |
| 894 |
path = app.srcdir / 'generated' / 'autosummary_dummy_package_empty_all.rst' |
| 895 |
module = path.read_text(encoding='utf8') |
| 896 |
assert '.. automodule:: autosummary_dummy_package_empty_all' in module |
| 897 |
# for __all__ = (), the output should not contain any variables |
| 898 |
assert '__all__' not in module |
| 899 |
assert '__builtins__' not in module |
| 900 |
assert '__cached__' not in module |
| 901 |
assert '__doc__' not in module |
| 902 |
assert '__file__' not in module |
| 903 |
assert '__loader__' not in module |
| 904 |
assert '__name__' not in module |
| 905 |
assert '__package__' not in module |
| 906 |
assert '__path__' not in module |
| 907 |
assert '__spec__' not in module |
| 908 |
finally: |
| 909 |
sys.modules.pop('autosummary_dummy_package_all', None) |
| 910 |
|
| 911 |
|
| 912 |
@pytest.mark.sphinx( |
| 913 |
'html', |
| 914 |
testroot='ext-autodoc', |
| 915 |
confoverrides={'extensions': ['sphinx.ext.autosummary']}, |
| 916 |
copy_test_root=True, |
| 917 |
) |
| 918 |
def test_generate_autosummary_docs_property(app): |
| 919 |
with patch('sphinx.ext.autosummary.generate.find_autosummary_in_files') as mock: |
| 920 |
mock.return_value = [ |
| 921 |
AutosummaryEntry('target.methods.Base.prop', 'prop', None, False) |
| 922 |
] |
| 923 |
generate_autosummary_docs([], output_dir=app.srcdir, app=app) |
| 924 |
|
| 925 |
content = (app.srcdir / 'target.methods.Base.prop.rst').read_text(encoding='utf8') |
| 926 |
assert content == ( |
| 927 |
'target.methods.Base.prop\n' |
| 928 |
'========================\n' |
| 929 |
'\n' |
| 930 |
'.. currentmodule:: target.methods\n' |
| 931 |
'\n' |
| 932 |
'.. autoproperty:: Base.prop' |
| 933 |
) |
| 934 |
|
| 935 |
|
| 936 |
@pytest.mark.sphinx( |
| 937 |
'html', |
| 938 |
testroot='ext-autosummary-skip-member', |
| 939 |
copy_test_root=True, |
| 940 |
) |
| 941 |
def test_autosummary_skip_member(app): |
| 942 |
app.build() |
| 943 |
|
| 944 |
content = (app.srcdir / 'generate' / 'target.Foo.rst').read_text(encoding='utf8') |
| 945 |
assert 'Foo.skipmeth' not in content |
| 946 |
assert 'Foo._privatemeth' in content |
| 947 |
|
| 948 |
|
| 949 |
@pytest.mark.sphinx('html', testroot='ext-autosummary-template', copy_test_root=True) |
| 950 |
def test_autosummary_template(app): |
| 951 |
app.build() |
| 952 |
|
| 953 |
content = (app.srcdir / 'generate' / 'target.Foo.rst').read_text(encoding='utf8') |
| 954 |
assert 'EMPTY' in content |
| 955 |
|
| 956 |
|
| 957 |
@pytest.mark.sphinx( |
| 958 |
'dummy', |
| 959 |
testroot='ext-autosummary', |
| 960 |
confoverrides={'autosummary_generate': []}, |
| 961 |
) |
| 962 |
def test_empty_autosummary_generate(app): |
| 963 |
app.build() |
| 964 |
assert ( |
| 965 |
'WARNING: autosummary: failed to import autosummary_importfail' |
| 966 |
) in app.warning.getvalue() |
| 967 |
|
| 968 |
|
| 969 |
@pytest.mark.sphinx( |
| 970 |
'dummy', |
| 971 |
testroot='ext-autosummary', |
| 972 |
confoverrides={'autosummary_generate': ['unknown']}, |
| 973 |
) |
| 974 |
def test_invalid_autosummary_generate(app): |
| 975 |
assert ( |
| 976 |
'WARNING: autosummary_generate: file not found: unknown.rst' |
| 977 |
) in app.warning.getvalue() |
| 978 |
|
| 979 |
|
| 980 |
def test_autogen(rootdir, tmp_path): |
| 981 |
with chdir(rootdir / 'test-templating'): |
| 982 |
args = ['-o', str(tmp_path), '-t', '.', 'autosummary_templating.txt'] |
| 983 |
autogen_main(args) |
| 984 |
assert (tmp_path / 'sphinx.application.TemplateBridge.rst').exists() |
| 985 |
|
| 986 |
|
| 987 |
def test_autogen_remove_old(rootdir, tmp_path): |
| 988 |
"""Test the ``--remove-old`` option.""" |
| 989 |
tmp_path.joinpath('other.rst').write_text('old content', encoding='utf-8') |
| 990 |
with chdir(rootdir / 'test-templating'): |
| 991 |
args = ['-o', str(tmp_path), '-t', '.', 'autosummary_templating.txt'] |
| 992 |
autogen_main(args) |
| 993 |
assert set(tmp_path.iterdir()) == { |
| 994 |
tmp_path / 'sphinx.application.TemplateBridge.rst', |
| 995 |
tmp_path / 'other.rst', |
| 996 |
} |
| 997 |
autogen_main([*args, '--remove-old']) |
| 998 |
assert set(tmp_path.iterdir()) == { |
| 999 |
tmp_path / 'sphinx.application.TemplateBridge.rst' |
| 1000 |
} |