| 1 |
"""Test the autodoc extension. |
| 2 |
|
| 3 |
This tests mainly the Documenters; the auto directives are tested in a test |
| 4 |
source file translated by test_build. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
import sys |
| 10 |
import typing |
| 11 |
|
| 12 |
import pytest |
| 13 |
|
| 14 |
from tests.test_ext_autodoc.autodoc_util import FakeEvents, do_autodoc |
| 15 |
|
| 16 |
TYPE_CHECKING = False |
| 17 |
if TYPE_CHECKING: |
| 18 |
from typing import Any |
| 19 |
|
| 20 |
from sphinx.application import Sphinx |
| 21 |
|
| 22 |
pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path') |
| 23 |
|
| 24 |
|
| 25 |
def test_classes() -> None: |
| 26 |
actual = do_autodoc('function', 'target.classes.Foo') |
| 27 |
assert actual == [ |
| 28 |
'', |
| 29 |
'.. py:function:: Foo()', |
| 30 |
' :module: target.classes', |
| 31 |
'', |
| 32 |
] |
| 33 |
|
| 34 |
actual = do_autodoc('function', 'target.classes.Bar') |
| 35 |
assert actual == [ |
| 36 |
'', |
| 37 |
'.. py:function:: Bar(x, y)', |
| 38 |
' :module: target.classes', |
| 39 |
'', |
| 40 |
] |
| 41 |
|
| 42 |
actual = do_autodoc('function', 'target.classes.Baz') |
| 43 |
assert actual == [ |
| 44 |
'', |
| 45 |
'.. py:function:: Baz(x, y)', |
| 46 |
' :module: target.classes', |
| 47 |
'', |
| 48 |
] |
| 49 |
|
| 50 |
actual = do_autodoc('function', 'target.classes.Qux') |
| 51 |
assert actual == [ |
| 52 |
'', |
| 53 |
'.. py:function:: Qux(foo, bar)', |
| 54 |
' :module: target.classes', |
| 55 |
'', |
| 56 |
] |
| 57 |
|
| 58 |
|
| 59 |
def test_instance_variable() -> None: |
| 60 |
options = {'members': None} |
| 61 |
actual = do_autodoc('class', 'target.instance_variable.Bar', options=options) |
| 62 |
assert actual == [ |
| 63 |
'', |
| 64 |
'.. py:class:: Bar()', |
| 65 |
' :module: target.instance_variable', |
| 66 |
'', |
| 67 |
'', |
| 68 |
' .. py:attribute:: Bar.attr2', |
| 69 |
' :module: target.instance_variable', |
| 70 |
'', |
| 71 |
' docstring bar', |
| 72 |
'', |
| 73 |
'', |
| 74 |
' .. py:attribute:: Bar.attr3', |
| 75 |
' :module: target.instance_variable', |
| 76 |
'', |
| 77 |
' docstring bar', |
| 78 |
'', |
| 79 |
] |
| 80 |
|
| 81 |
|
| 82 |
def test_inherited_instance_variable() -> None: |
| 83 |
options = { |
| 84 |
'members': None, |
| 85 |
'inherited-members': None, |
| 86 |
} |
| 87 |
actual = do_autodoc('class', 'target.instance_variable.Bar', options=options) |
| 88 |
assert actual == [ |
| 89 |
'', |
| 90 |
'.. py:class:: Bar()', |
| 91 |
' :module: target.instance_variable', |
| 92 |
'', |
| 93 |
'', |
| 94 |
' .. py:attribute:: Bar.attr1', |
| 95 |
' :module: target.instance_variable', |
| 96 |
'', |
| 97 |
' docstring foo', |
| 98 |
'', |
| 99 |
'', |
| 100 |
' .. py:attribute:: Bar.attr2', |
| 101 |
' :module: target.instance_variable', |
| 102 |
'', |
| 103 |
' docstring bar', |
| 104 |
'', |
| 105 |
'', |
| 106 |
' .. py:attribute:: Bar.attr3', |
| 107 |
' :module: target.instance_variable', |
| 108 |
'', |
| 109 |
' docstring bar', |
| 110 |
'', |
| 111 |
] |
| 112 |
|
| 113 |
|
| 114 |
def test_uninitialized_attributes() -> None: |
| 115 |
options = { |
| 116 |
'members': None, |
| 117 |
'inherited-members': None, |
| 118 |
} |
| 119 |
actual = do_autodoc( |
| 120 |
'class', 'target.uninitialized_attributes.Derived', options=options |
| 121 |
) |
| 122 |
assert actual == [ |
| 123 |
'', |
| 124 |
'.. py:class:: Derived()', |
| 125 |
' :module: target.uninitialized_attributes', |
| 126 |
'', |
| 127 |
'', |
| 128 |
' .. py:attribute:: Derived.attr1', |
| 129 |
' :module: target.uninitialized_attributes', |
| 130 |
' :type: int', |
| 131 |
'', |
| 132 |
' docstring', |
| 133 |
'', |
| 134 |
'', |
| 135 |
' .. py:attribute:: Derived.attr3', |
| 136 |
' :module: target.uninitialized_attributes', |
| 137 |
' :type: int', |
| 138 |
'', |
| 139 |
' docstring', |
| 140 |
'', |
| 141 |
] |
| 142 |
|
| 143 |
|
| 144 |
def test_undocumented_uninitialized_attributes() -> None: |
| 145 |
options = { |
| 146 |
'members': None, |
| 147 |
'inherited-members': None, |
| 148 |
'undoc-members': None, |
| 149 |
} |
| 150 |
actual = do_autodoc( |
| 151 |
'class', 'target.uninitialized_attributes.Derived', options=options |
| 152 |
) |
| 153 |
assert actual == [ |
| 154 |
'', |
| 155 |
'.. py:class:: Derived()', |
| 156 |
' :module: target.uninitialized_attributes', |
| 157 |
'', |
| 158 |
'', |
| 159 |
' .. py:attribute:: Derived.attr1', |
| 160 |
' :module: target.uninitialized_attributes', |
| 161 |
' :type: int', |
| 162 |
'', |
| 163 |
' docstring', |
| 164 |
'', |
| 165 |
'', |
| 166 |
' .. py:attribute:: Derived.attr2', |
| 167 |
' :module: target.uninitialized_attributes', |
| 168 |
' :type: str', |
| 169 |
'', |
| 170 |
'', |
| 171 |
' .. py:attribute:: Derived.attr3', |
| 172 |
' :module: target.uninitialized_attributes', |
| 173 |
' :type: int', |
| 174 |
'', |
| 175 |
' docstring', |
| 176 |
'', |
| 177 |
'', |
| 178 |
' .. py:attribute:: Derived.attr4', |
| 179 |
' :module: target.uninitialized_attributes', |
| 180 |
' :type: str', |
| 181 |
'', |
| 182 |
] |
| 183 |
|
| 184 |
|
| 185 |
def test_decorators() -> None: |
| 186 |
actual = do_autodoc('class', 'target.decorator.Baz') |
| 187 |
assert actual == [ |
| 188 |
'', |
| 189 |
'.. py:class:: Baz(name=None, age=None)', |
| 190 |
' :module: target.decorator', |
| 191 |
'', |
| 192 |
] |
| 193 |
|
| 194 |
actual = do_autodoc('class', 'target.decorator.Qux') |
| 195 |
assert actual == [ |
| 196 |
'', |
| 197 |
'.. py:class:: Qux(name=None, age=None)', |
| 198 |
' :module: target.decorator', |
| 199 |
'', |
| 200 |
] |
| 201 |
|
| 202 |
actual = do_autodoc('class', 'target.decorator.Quux') |
| 203 |
assert actual == [ |
| 204 |
'', |
| 205 |
'.. py:class:: Quux(name=None, age=None)', |
| 206 |
' :module: target.decorator', |
| 207 |
'', |
| 208 |
] |
| 209 |
|
| 210 |
|
| 211 |
def test_properties() -> None: |
| 212 |
if sys.version_info[:2] >= (3, 14): |
| 213 |
type_checking_only_name = 'TypeCheckingOnlyName' |
| 214 |
else: |
| 215 |
type_checking_only_name = 'int' |
| 216 |
|
| 217 |
options = {'members': None} |
| 218 |
actual = do_autodoc('class', 'target.properties.Foo', options=options) |
| 219 |
assert actual == [ |
| 220 |
'', |
| 221 |
'.. py:class:: Foo()', |
| 222 |
' :module: target.properties', |
| 223 |
'', |
| 224 |
' docstring', |
| 225 |
'', |
| 226 |
'', |
| 227 |
' .. py:property:: Foo.prop1', |
| 228 |
' :module: target.properties', |
| 229 |
' :type: int', |
| 230 |
'', |
| 231 |
' docstring', |
| 232 |
'', |
| 233 |
'', |
| 234 |
' .. py:property:: Foo.prop1_with_type_comment', |
| 235 |
' :module: target.properties', |
| 236 |
' :type: int', |
| 237 |
'', |
| 238 |
' docstring', |
| 239 |
'', |
| 240 |
'', |
| 241 |
' .. py:property:: Foo.prop2', |
| 242 |
' :module: target.properties', |
| 243 |
' :classmethod:', |
| 244 |
' :type: int', |
| 245 |
'', |
| 246 |
' docstring', |
| 247 |
'', |
| 248 |
'', |
| 249 |
' .. py:property:: Foo.prop2_with_type_comment', |
| 250 |
' :module: target.properties', |
| 251 |
' :classmethod:', |
| 252 |
' :type: int', |
| 253 |
'', |
| 254 |
' docstring', |
| 255 |
'', |
| 256 |
'', |
| 257 |
' .. py:property:: Foo.prop3_with_undefined_anotation', |
| 258 |
' :module: target.properties', |
| 259 |
f' :type: {type_checking_only_name}', |
| 260 |
'', |
| 261 |
' docstring', |
| 262 |
'', |
| 263 |
] |
| 264 |
|
| 265 |
|
| 266 |
def test_slots_attribute() -> None: |
| 267 |
options = {'members': None} |
| 268 |
actual = do_autodoc('class', 'target.slots.Bar', options=options) |
| 269 |
assert actual == [ |
| 270 |
'', |
| 271 |
'.. py:class:: Bar()', |
| 272 |
' :module: target.slots', |
| 273 |
'', |
| 274 |
' docstring', |
| 275 |
'', |
| 276 |
'', |
| 277 |
' .. py:attribute:: Bar.attr1', |
| 278 |
' :module: target.slots', |
| 279 |
' :type: int', |
| 280 |
'', |
| 281 |
' docstring of attr1', |
| 282 |
'', |
| 283 |
'', |
| 284 |
' .. py:attribute:: Bar.attr2', |
| 285 |
' :module: target.slots', |
| 286 |
'', |
| 287 |
' docstring of instance attr2', |
| 288 |
'', |
| 289 |
] |
| 290 |
|
| 291 |
|
| 292 |
def test_show_inheritance_for_subclass_of_generic_type() -> None: |
| 293 |
options = {'show-inheritance': None} |
| 294 |
actual = do_autodoc('class', 'target.classes.Quux', options=options) |
| 295 |
assert actual == [ |
| 296 |
'', |
| 297 |
'.. py:class:: Quux(iterable=(), /)', |
| 298 |
' :module: target.classes', |
| 299 |
'', |
| 300 |
' Bases: :py:class:`~typing.List`\\ [:py:class:`int` | :py:class:`float`]', |
| 301 |
'', |
| 302 |
' A subclass of List[Union[int, float]]', |
| 303 |
'', |
| 304 |
] |
| 305 |
|
| 306 |
|
| 307 |
def test_show_inheritance_for_decendants_of_generic_type() -> None: |
| 308 |
options = {'show-inheritance': None} |
| 309 |
actual = do_autodoc('class', 'target.classes.Corge', options=options) |
| 310 |
assert actual == [ |
| 311 |
'', |
| 312 |
'.. py:class:: Corge(iterable=(), /)', |
| 313 |
' :module: target.classes', |
| 314 |
'', |
| 315 |
' Bases: :py:class:`~target.classes.Quux`', |
| 316 |
'', |
| 317 |
] |
| 318 |
|
| 319 |
|
| 320 |
def _autodoc_process_bases( |
| 321 |
app: Sphinx, name: str, obj: Any, options: Any, bases: list[type] |
| 322 |
) -> None: |
| 323 |
assert name == 'target.classes.Quux' |
| 324 |
assert obj.__module__ == 'target.classes' |
| 325 |
assert obj.__name__ == 'Quux' |
| 326 |
assert vars(options) == {} |
| 327 |
assert bases == [typing.List[typing.Union[int, float]]] # NoQA: UP006, UP007 |
| 328 |
|
| 329 |
bases.pop() |
| 330 |
bases.extend([int, str]) |
| 331 |
|
| 332 |
|
| 333 |
def test_autodoc_process_bases() -> None: |
| 334 |
events = FakeEvents() |
| 335 |
events.connect('autodoc-process-bases', _autodoc_process_bases) |
| 336 |
|
| 337 |
options = {'show-inheritance': None} |
| 338 |
actual = do_autodoc('class', 'target.classes.Quux', events=events, options=options) |
| 339 |
assert actual == [ |
| 340 |
'', |
| 341 |
'.. py:class:: Quux(iterable=(), /)', |
| 342 |
' :module: target.classes', |
| 343 |
'', |
| 344 |
' Bases: :py:class:`int`, :py:class:`str`', |
| 345 |
'', |
| 346 |
' A subclass of List[Union[int, float]]', |
| 347 |
'', |
| 348 |
] |
| 349 |
|
| 350 |
|
| 351 |
def test_class_doc_from_class() -> None: |
| 352 |
options = { |
| 353 |
'members': None, |
| 354 |
'class-doc-from': 'class', |
| 355 |
} |
| 356 |
actual = do_autodoc('class', 'target.autoclass_content.C', options=options) |
| 357 |
assert actual == [ |
| 358 |
'', |
| 359 |
'.. py:class:: C()', |
| 360 |
' :module: target.autoclass_content', |
| 361 |
'', |
| 362 |
' A class having __init__, no __new__', |
| 363 |
'', |
| 364 |
] |
| 365 |
|
| 366 |
|
| 367 |
def test_class_doc_from_init() -> None: |
| 368 |
options = { |
| 369 |
'members': None, |
| 370 |
'class-doc-from': 'init', |
| 371 |
} |
| 372 |
actual = do_autodoc('class', 'target.autoclass_content.C', options=options) |
| 373 |
assert actual == [ |
| 374 |
'', |
| 375 |
'.. py:class:: C()', |
| 376 |
' :module: target.autoclass_content', |
| 377 |
'', |
| 378 |
' __init__ docstring', |
| 379 |
'', |
| 380 |
] |
| 381 |
|
| 382 |
|
| 383 |
def test_class_doc_from_both() -> None: |
| 384 |
options = { |
| 385 |
'members': None, |
| 386 |
'class-doc-from': 'both', |
| 387 |
} |
| 388 |
actual = do_autodoc('class', 'target.autoclass_content.C', options=options) |
| 389 |
assert actual == [ |
| 390 |
'', |
| 391 |
'.. py:class:: C()', |
| 392 |
' :module: target.autoclass_content', |
| 393 |
'', |
| 394 |
' A class having __init__, no __new__', |
| 395 |
'', |
| 396 |
' __init__ docstring', |
| 397 |
'', |
| 398 |
] |
| 399 |
|
| 400 |
|
| 401 |
def test_class_alias() -> None: |
| 402 |
def autodoc_process_docstring(*args: Any) -> None: |
| 403 |
"""A handler always raises an error. |
| 404 |
This confirms this handler is never called for class aliases. |
| 405 |
""" |
| 406 |
raise RuntimeError |
| 407 |
|
| 408 |
events = FakeEvents() |
| 409 |
events.connect('autodoc-process-docstring', autodoc_process_docstring) |
| 410 |
actual = do_autodoc('class', 'target.classes.Alias', events=events) |
| 411 |
assert actual == [ |
| 412 |
'', |
| 413 |
'.. py:attribute:: Alias', |
| 414 |
' :module: target.classes', |
| 415 |
'', |
| 416 |
' alias of :py:class:`~target.classes.Foo`', |
| 417 |
] |
| 418 |
|
| 419 |
|
| 420 |
def test_class_alias_having_doccomment() -> None: |
| 421 |
actual = do_autodoc('class', 'target.classes.OtherAlias') |
| 422 |
assert actual == [ |
| 423 |
'', |
| 424 |
'.. py:attribute:: OtherAlias', |
| 425 |
' :module: target.classes', |
| 426 |
'', |
| 427 |
' docstring', |
| 428 |
'', |
| 429 |
] |
| 430 |
|
| 431 |
|
| 432 |
def test_class_alias_for_imported_object_having_doccomment() -> None: |
| 433 |
actual = do_autodoc('class', 'target.classes.IntAlias') |
| 434 |
assert actual == [ |
| 435 |
'', |
| 436 |
'.. py:attribute:: IntAlias', |
| 437 |
' :module: target.classes', |
| 438 |
'', |
| 439 |
' docstring', |
| 440 |
'', |
| 441 |
] |
| 442 |
|
| 443 |
|
| 444 |
def test_coroutine() -> None: |
| 445 |
options = {'members': None} |
| 446 |
actual = do_autodoc('class', 'target.coroutine.AsyncClass', options=options) |
| 447 |
assert actual == [ |
| 448 |
'', |
| 449 |
'.. py:class:: AsyncClass()', |
| 450 |
' :module: target.coroutine', |
| 451 |
'', |
| 452 |
'', |
| 453 |
' .. py:method:: AsyncClass.do_asyncgen()', |
| 454 |
' :module: target.coroutine', |
| 455 |
' :async:', |
| 456 |
'', |
| 457 |
' A documented async generator', |
| 458 |
'', |
| 459 |
'', |
| 460 |
' .. py:method:: AsyncClass.do_coroutine()', |
| 461 |
' :module: target.coroutine', |
| 462 |
' :async:', |
| 463 |
'', |
| 464 |
' A documented coroutine function', |
| 465 |
'', |
| 466 |
'', |
| 467 |
' .. py:method:: AsyncClass.do_coroutine2()', |
| 468 |
' :module: target.coroutine', |
| 469 |
' :async:', |
| 470 |
' :classmethod:', |
| 471 |
'', |
| 472 |
' A documented coroutine classmethod', |
| 473 |
'', |
| 474 |
'', |
| 475 |
' .. py:method:: AsyncClass.do_coroutine3()', |
| 476 |
' :module: target.coroutine', |
| 477 |
' :async:', |
| 478 |
' :staticmethod:', |
| 479 |
'', |
| 480 |
' A documented coroutine staticmethod', |
| 481 |
'', |
| 482 |
] |
| 483 |
|
| 484 |
|
| 485 |
def test_autodata_NewType_module_level() -> None: |
| 486 |
actual = do_autodoc('class', 'target.typevar.T6') |
| 487 |
assert actual == [ |
| 488 |
'', |
| 489 |
'.. py:class:: T6', |
| 490 |
' :module: target.typevar', |
| 491 |
'', |
| 492 |
' T6', |
| 493 |
'', |
| 494 |
' alias of :py:class:`~datetime.date`', |
| 495 |
'', |
| 496 |
] |
| 497 |
|
| 498 |
|
| 499 |
def test_autoattribute_NewType_class_level() -> None: |
| 500 |
actual = do_autodoc('class', 'target.typevar.Class.T6') |
| 501 |
assert actual == [ |
| 502 |
'', |
| 503 |
'.. py:class:: Class.T6', |
| 504 |
' :module: target.typevar', |
| 505 |
'', |
| 506 |
' T6', |
| 507 |
'', |
| 508 |
' alias of :py:class:`~datetime.date`', |
| 509 |
'', |
| 510 |
] |
| 511 |
|
| 512 |
|
| 513 |
def test_autodata_TypeVar_class_level() -> None: |
| 514 |
actual = do_autodoc('class', 'target.typevar.T1') |
| 515 |
assert actual == [ |
| 516 |
'', |
| 517 |
'.. py:class:: T1', |
| 518 |
' :module: target.typevar', |
| 519 |
'', |
| 520 |
' T1', |
| 521 |
'', |
| 522 |
" alias of TypeVar('T1')", |
| 523 |
'', |
| 524 |
] |
| 525 |
|
| 526 |
|
| 527 |
def test_autoattribute_TypeVar_module_level() -> None: |
| 528 |
actual = do_autodoc('class', 'target.typevar.Class.T1') |
| 529 |
assert actual == [ |
| 530 |
'', |
| 531 |
'.. py:class:: Class.T1', |
| 532 |
' :module: target.typevar', |
| 533 |
'', |
| 534 |
' T1', |
| 535 |
'', |
| 536 |
" alias of TypeVar('T1')", |
| 537 |
'', |
| 538 |
] |
| 539 |
|
| 540 |
|
| 541 |
def test_inherited_instance_variable_with_annotations() -> None: |
| 542 |
options = { |
| 543 |
'members': None, |
| 544 |
'inherited-members': None, |
| 545 |
} |
| 546 |
actual = do_autodoc( |
| 547 |
'class', 'target.inherited_annotations.NoTypeAnnotation', options=options |
| 548 |
) |
| 549 |
assert actual == [ |
| 550 |
'', |
| 551 |
'.. py:class:: NoTypeAnnotation()', |
| 552 |
' :module: target.inherited_annotations', |
| 553 |
'', |
| 554 |
'', |
| 555 |
' .. py:attribute:: NoTypeAnnotation.a', |
| 556 |
' :module: target.inherited_annotations', |
| 557 |
' :value: 1', |
| 558 |
'', |
| 559 |
' Local', |
| 560 |
'', |
| 561 |
'', |
| 562 |
' .. py:attribute:: NoTypeAnnotation.inherit_me', |
| 563 |
' :module: target.inherited_annotations', |
| 564 |
' :type: int', |
| 565 |
'', |
| 566 |
' Inherited', |
| 567 |
'', |
| 568 |
] |
| 569 |
|
| 570 |
|
| 571 |
def test_no_inherited_instance_variable_with_annotations() -> None: |
| 572 |
options = {'members': None} |
| 573 |
actual = do_autodoc( |
| 574 |
'class', 'target.inherited_annotations.NoTypeAnnotation2', options=options |
| 575 |
) |
| 576 |
assert actual == [ |
| 577 |
'', |
| 578 |
'.. py:class:: NoTypeAnnotation2()', |
| 579 |
' :module: target.inherited_annotations', |
| 580 |
'', |
| 581 |
'', |
| 582 |
' .. py:attribute:: NoTypeAnnotation2.a', |
| 583 |
' :module: target.inherited_annotations', |
| 584 |
' :value: 1', |
| 585 |
'', |
| 586 |
' Local', |
| 587 |
'', |
| 588 |
] |