| 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 itertools |
| 10 |
import logging |
| 11 |
import pathlib |
| 12 |
import sys |
| 13 |
from typing import TYPE_CHECKING |
| 14 |
from warnings import catch_warnings |
| 15 |
|
| 16 |
import pytest |
| 17 |
|
| 18 |
from sphinx.environment import _CurrentDocument |
| 19 |
from sphinx.ext.autodoc._directive_options import ( |
| 20 |
_AutoDocumenterOptions, |
| 21 |
inherited_members_option, |
| 22 |
) |
| 23 |
from sphinx.ext.autodoc._dynamic._docstrings import _get_docstring_lines |
| 24 |
from sphinx.ext.autodoc._generate import _auto_document_object |
| 25 |
from sphinx.ext.autodoc._legacy_class_based._documenters import Documenter |
| 26 |
from sphinx.ext.autodoc._property_types import _ItemProperties |
| 27 |
from sphinx.ext.autodoc._sentinels import ALL |
| 28 |
from sphinx.ext.autodoc._shared import _AutodocAttrGetter, _AutodocConfig |
| 29 |
from sphinx.util.inspect import safe_getattr |
| 30 |
|
| 31 |
from tests.test_ext_autodoc.autodoc_util import FakeEvents, do_autodoc |
| 32 |
|
| 33 |
try: |
| 34 |
# Enable pyximport to test cython module |
| 35 |
import pyximport |
| 36 |
|
| 37 |
pyximport.install() |
| 38 |
except ImportError: |
| 39 |
pyximport = None |
| 40 |
|
| 41 |
if TYPE_CHECKING: |
| 42 |
from typing import Any |
| 43 |
|
| 44 |
from sphinx.ext.autodoc._property_types import _AutodocObjType |
| 45 |
from sphinx.ext.autodoc._shared import _AttrGetter |
| 46 |
|
| 47 |
pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path') |
| 48 |
|
| 49 |
processed_signatures: list[tuple[str, str]] = [] |
| 50 |
|
| 51 |
|
| 52 |
def get_docstring_lines(obj_type, obj): |
| 53 |
config = _AutodocConfig() |
| 54 |
|
| 55 |
parent = object # dummy |
| 56 |
props = _ItemProperties( |
| 57 |
obj_type=obj_type, |
| 58 |
module_name='', |
| 59 |
parts=(obj.__name__,), |
| 60 |
docstring_lines=(), |
| 61 |
_obj=obj, |
| 62 |
_obj___module__=getattr(obj, '__module__', None), |
| 63 |
) |
| 64 |
ds = _get_docstring_lines( |
| 65 |
props, |
| 66 |
class_doc_from=config.autoclass_content, |
| 67 |
get_attr=safe_getattr, |
| 68 |
inherit_docstrings=config.autodoc_inherit_docstrings, |
| 69 |
parent=parent, |
| 70 |
tab_width=8, |
| 71 |
) |
| 72 |
# for testing purposes, concat them and strip the empty line at the end |
| 73 |
res = list(itertools.chain.from_iterable(ds or ())) |
| 74 |
if res: |
| 75 |
res.pop() |
| 76 |
return tuple(res) |
| 77 |
|
| 78 |
|
| 79 |
def test_get_docstring_lines(): |
| 80 |
# objects without docstring |
| 81 |
def f(): |
| 82 |
pass |
| 83 |
|
| 84 |
assert get_docstring_lines('function', f) == () |
| 85 |
|
| 86 |
# standard function, diverse docstring styles... |
| 87 |
def f(): |
| 88 |
"""Docstring""" |
| 89 |
|
| 90 |
assert get_docstring_lines('function', f) == ('Docstring',) |
| 91 |
|
| 92 |
def f(): |
| 93 |
""" |
| 94 |
Docstring |
| 95 |
""" # NoQA: D212 |
| 96 |
|
| 97 |
assert get_docstring_lines('function', f) == ('Docstring',) |
| 98 |
|
| 99 |
# first line vs. other lines indentation |
| 100 |
def f(): |
| 101 |
"""First line |
| 102 |
|
| 103 |
Other |
| 104 |
lines |
| 105 |
""" |
| 106 |
|
| 107 |
assert get_docstring_lines('function', f) == ( |
| 108 |
'First line', |
| 109 |
'', |
| 110 |
'Other', |
| 111 |
' lines', |
| 112 |
) |
| 113 |
|
| 114 |
# charset guessing (this module is encoded in utf-8) |
| 115 |
def f(): |
| 116 |
"""Döcstring""" |
| 117 |
|
| 118 |
assert get_docstring_lines('function', f) == ('Döcstring',) |
| 119 |
|
| 120 |
# verify that method docstrings get extracted in both normal case |
| 121 |
# and in case of bound method posing as a function |
| 122 |
class J: |
| 123 |
def foo(self): |
| 124 |
"""Method docstring""" |
| 125 |
|
| 126 |
expected = ('Method docstring',) |
| 127 |
assert get_docstring_lines('method', J.foo) == expected |
| 128 |
assert get_docstring_lines('function', J().foo) == expected |
| 129 |
|
| 130 |
|
| 131 |
class _MyDocumenter(Documenter): |
| 132 |
objtype = 'integer' |
| 133 |
directivetype = 'integer' |
| 134 |
priority = 100 |
| 135 |
|
| 136 |
@classmethod |
| 137 |
def can_document_member(cls, member, membername, isattr, parent): |
| 138 |
return isinstance(member, int) |
| 139 |
|
| 140 |
def document_members(self, all_members=False): |
| 141 |
return |
| 142 |
|
| 143 |
|
| 144 |
def test_new_documenter(): |
| 145 |
config = _AutodocConfig() |
| 146 |
# app.add_autodocumenter(_MyDocumenter) |
| 147 |
|
| 148 |
options = {'members': 'integer'} |
| 149 |
# TODO: Fix! Perhaps add a way to signal module/class-level? |
| 150 |
actual = do_autodoc('module', 'target', config=config, options=options) |
| 151 |
return |
| 152 |
assert actual == [ |
| 153 |
'', |
| 154 |
'.. py:module:: target', |
| 155 |
'', |
| 156 |
'', |
| 157 |
'.. py:integer:: integer', |
| 158 |
' :module: target', |
| 159 |
'', |
| 160 |
' documentation for the integer', |
| 161 |
'', |
| 162 |
] |
| 163 |
|
| 164 |
|
| 165 |
getattr_spy = [] |
| 166 |
|
| 167 |
|
| 168 |
def test_attrgetter_using(): |
| 169 |
attrs = [] |
| 170 |
|
| 171 |
def _special_getattr(obj, attr_name, *defargs): |
| 172 |
if attr_name in attrs: |
| 173 |
getattr_spy.append((obj, attr_name)) |
| 174 |
return None |
| 175 |
return getattr(obj, attr_name, *defargs) |
| 176 |
|
| 177 |
# See Sphinx.add_autodoc_attrgetter() |
| 178 |
autodoc_attrgetters = {type: _special_getattr} |
| 179 |
get_attr = _AutodocAttrGetter(autodoc_attrgetters) |
| 180 |
options = _AutoDocumenterOptions(members=ALL) |
| 181 |
|
| 182 |
options.inherited_members = inherited_members_option(False) |
| 183 |
attrs[:] = ['meth'] |
| 184 |
with catch_warnings(record=True): |
| 185 |
_assert_getter_works( |
| 186 |
'class', |
| 187 |
'target.Class', |
| 188 |
*attrs, |
| 189 |
get_attr=get_attr, |
| 190 |
options=options, |
| 191 |
) |
| 192 |
|
| 193 |
options.inherited_members = inherited_members_option(True) |
| 194 |
attrs[:] = ['inheritedmeth'] |
| 195 |
with catch_warnings(record=True): |
| 196 |
_assert_getter_works( |
| 197 |
'class', |
| 198 |
'target.inheritance.Derived', |
| 199 |
*attrs, |
| 200 |
get_attr=get_attr, |
| 201 |
options=options, |
| 202 |
) |
| 203 |
|
| 204 |
|
| 205 |
def _assert_getter_works( |
| 206 |
objtype: _AutodocObjType, |
| 207 |
name: str, |
| 208 |
*attrs: str, |
| 209 |
get_attr: _AttrGetter, |
| 210 |
options: _AutoDocumenterOptions, |
| 211 |
) -> None: |
| 212 |
getattr_spy.clear() |
| 213 |
|
| 214 |
config = _AutodocConfig() |
| 215 |
current_document = _CurrentDocument() |
| 216 |
events = FakeEvents() |
| 217 |
|
| 218 |
_auto_document_object( |
| 219 |
config=config, |
| 220 |
current_document=current_document, |
| 221 |
events=events, |
| 222 |
get_attr=get_attr, |
| 223 |
more_content=None, |
| 224 |
name=name, |
| 225 |
obj_type=objtype, |
| 226 |
options=options, |
| 227 |
record_dependencies=set(), |
| 228 |
ref_context={}, |
| 229 |
reread_always=set(), |
| 230 |
) |
| 231 |
|
| 232 |
hooked_members = {s[1] for s in getattr_spy} |
| 233 |
documented_members = {s[1] for s in processed_signatures} |
| 234 |
for attr in attrs: |
| 235 |
fullname = f'{name}.{attr}' |
| 236 |
assert attr in hooked_members |
| 237 |
assert fullname not in documented_members, f'{fullname!r} not intercepted' |
| 238 |
|
| 239 |
|
| 240 |
def test_py_module(caplog: pytest.LogCaptureFixture) -> None: |
| 241 |
# work around sphinx.util.logging.setup() |
| 242 |
logger = logging.getLogger('sphinx') |
| 243 |
logger.handlers[:] = [caplog.handler] |
| 244 |
caplog.set_level(logging.WARNING) |
| 245 |
|
| 246 |
# without py:module |
| 247 |
actual = do_autodoc('method', 'Class.meth', expect_import_error=True) |
| 248 |
assert actual == [] |
| 249 |
assert len(set(caplog.messages)) == 1 |
| 250 |
assert ( |
| 251 |
"don't know which module to import for autodocumenting 'Class.meth'" |
| 252 |
) in caplog.messages[0] |
| 253 |
caplog.clear() |
| 254 |
|
| 255 |
# with py:module |
| 256 |
ref_context: dict[str, Any] = {'py:module': 'target'} |
| 257 |
actual = do_autodoc('method', 'Class.meth', ref_context=ref_context) |
| 258 |
assert actual == [ |
| 259 |
'', |
| 260 |
'.. py:method:: Class.meth()', |
| 261 |
' :module: target', |
| 262 |
'', |
| 263 |
' Function.', |
| 264 |
'', |
| 265 |
] |
| 266 |
assert len(caplog.records) == 0 |
| 267 |
|
| 268 |
|
| 269 |
def test_autodoc_decorator() -> None: |
| 270 |
actual = do_autodoc('decorator', 'target.decorator.deco1') |
| 271 |
assert actual == [ |
| 272 |
'', |
| 273 |
'.. py:decorator:: deco1', |
| 274 |
' :module: target.decorator', |
| 275 |
'', |
| 276 |
' docstring for deco1', |
| 277 |
'', |
| 278 |
] |
| 279 |
|
| 280 |
actual = do_autodoc('decorator', 'target.decorator.deco2') |
| 281 |
assert actual == [ |
| 282 |
'', |
| 283 |
'.. py:decorator:: deco2(condition, message)', |
| 284 |
' :module: target.decorator', |
| 285 |
'', |
| 286 |
' docstring for deco2', |
| 287 |
'', |
| 288 |
] |
| 289 |
|
| 290 |
|
| 291 |
def test_autodoc_exception() -> None: |
| 292 |
actual = do_autodoc('exception', 'target.CustomEx') |
| 293 |
assert actual == [ |
| 294 |
'', |
| 295 |
'.. py:exception:: CustomEx', |
| 296 |
' :module: target', |
| 297 |
'', |
| 298 |
' My custom exception.', |
| 299 |
'', |
| 300 |
] |
| 301 |
|
| 302 |
|
| 303 |
def test_autodoc_warnings(caplog: pytest.LogCaptureFixture) -> None: |
| 304 |
# work around sphinx.util.logging.setup() |
| 305 |
logger = logging.getLogger('sphinx') |
| 306 |
logger.handlers[:] = [caplog.handler] |
| 307 |
caplog.set_level(logging.WARNING) |
| 308 |
|
| 309 |
current_document = _CurrentDocument(docname='dummy') |
| 310 |
|
| 311 |
# can't import module |
| 312 |
caplog.clear() |
| 313 |
do_autodoc( |
| 314 |
'module', 'unknown', current_document=current_document, expect_import_error=True |
| 315 |
) |
| 316 |
assert len(set(caplog.messages)) == 1 |
| 317 |
assert "failed to import 'unknown'" in caplog.messages[0] |
| 318 |
|
| 319 |
# missing function |
| 320 |
caplog.clear() |
| 321 |
do_autodoc( |
| 322 |
'function', |
| 323 |
'unknown', |
| 324 |
current_document=current_document, |
| 325 |
expect_import_error=True, |
| 326 |
) |
| 327 |
assert len(set(caplog.messages)) == 1 |
| 328 |
assert "import for autodocumenting 'unknown'" in caplog.messages[0] |
| 329 |
|
| 330 |
caplog.clear() |
| 331 |
do_autodoc( |
| 332 |
'function', |
| 333 |
'target.unknown', |
| 334 |
current_document=current_document, |
| 335 |
expect_import_error=True, |
| 336 |
) |
| 337 |
assert len(set(caplog.messages)) == 1 |
| 338 |
assert "failed to import 'unknown' from module 'target'" in caplog.messages[0] |
| 339 |
|
| 340 |
# missing method |
| 341 |
caplog.clear() |
| 342 |
do_autodoc( |
| 343 |
'method', |
| 344 |
'target.Class.unknown', |
| 345 |
current_document=current_document, |
| 346 |
expect_import_error=True, |
| 347 |
) |
| 348 |
assert len(set(caplog.messages)) == 1 |
| 349 |
assert "failed to import 'Class.unknown' from module 'target'" in caplog.messages[0] |
| 350 |
|
| 351 |
|
| 352 |
def test_autodoc_attributes() -> None: |
| 353 |
options = { |
| 354 |
'synopsis': 'Synopsis', |
| 355 |
'platform': 'Platform', |
| 356 |
'deprecated': None, |
| 357 |
} |
| 358 |
actual = do_autodoc('module', 'target', options=options) |
| 359 |
assert actual == [ |
| 360 |
'', |
| 361 |
'.. py:module:: target', |
| 362 |
' :synopsis: Synopsis', |
| 363 |
' :platform: Platform', |
| 364 |
' :deprecated:', |
| 365 |
'', |
| 366 |
] |
| 367 |
|
| 368 |
|
| 369 |
def test_autodoc_members() -> None: |
| 370 |
options: dict[str, Any] |
| 371 |
|
| 372 |
# default (no-members) |
| 373 |
actual = do_autodoc('class', 'target.inheritance.Base') |
| 374 |
assert [line for line in actual if '::' in line] == [ |
| 375 |
'.. py:class:: Base()', |
| 376 |
] |
| 377 |
|
| 378 |
# default ALL-members |
| 379 |
options = {'members': None} |
| 380 |
actual = do_autodoc('class', 'target.inheritance.Base', options=options) |
| 381 |
assert [line for line in actual if '::' in line] == [ |
| 382 |
'.. py:class:: Base()', |
| 383 |
' .. py:attribute:: Base.inheritedattr', |
| 384 |
' .. py:method:: Base.inheritedclassmeth()', |
| 385 |
' .. py:method:: Base.inheritedmeth()', |
| 386 |
' .. py:method:: Base.inheritedstaticmeth(cls)', |
| 387 |
] |
| 388 |
|
| 389 |
# default specific-members |
| 390 |
options = {'members': 'inheritedmeth,inheritedstaticmeth'} |
| 391 |
actual = do_autodoc('class', 'target.inheritance.Base', options=options) |
| 392 |
assert [line for line in actual if '::' in line] == [ |
| 393 |
'.. py:class:: Base()', |
| 394 |
' .. py:method:: Base.inheritedmeth()', |
| 395 |
' .. py:method:: Base.inheritedstaticmeth(cls)', |
| 396 |
] |
| 397 |
|
| 398 |
# ALL-members override autodoc_default_options |
| 399 |
options = {'members': None} |
| 400 |
config = _AutodocConfig(autodoc_default_options={'members': 'inheritedstaticmeth'}) |
| 401 |
actual = do_autodoc( |
| 402 |
'class', 'target.inheritance.Base', config=config, options=options |
| 403 |
) |
| 404 |
assert [line for line in actual if '::' in line] == [ |
| 405 |
'.. py:class:: Base()', |
| 406 |
' .. py:attribute:: Base.inheritedattr', |
| 407 |
' .. py:method:: Base.inheritedclassmeth()', |
| 408 |
' .. py:method:: Base.inheritedmeth()', |
| 409 |
' .. py:method:: Base.inheritedstaticmeth(cls)', |
| 410 |
] |
| 411 |
|
| 412 |
# members override autodoc_default_options |
| 413 |
options = {'members': 'inheritedmeth'} |
| 414 |
actual = do_autodoc( |
| 415 |
'class', 'target.inheritance.Base', config=config, options=options |
| 416 |
) |
| 417 |
assert [line for line in actual if '::' in line] == [ |
| 418 |
'.. py:class:: Base()', |
| 419 |
' .. py:method:: Base.inheritedmeth()', |
| 420 |
] |
| 421 |
|
| 422 |
# members extends autodoc_default_options |
| 423 |
options = {'members': '+inheritedmeth'} |
| 424 |
actual = do_autodoc( |
| 425 |
'class', 'target.inheritance.Base', config=config, options=options |
| 426 |
) |
| 427 |
assert [line for line in actual if '::' in line] == [ |
| 428 |
'.. py:class:: Base()', |
| 429 |
' .. py:method:: Base.inheritedmeth()', |
| 430 |
' .. py:method:: Base.inheritedstaticmeth(cls)', |
| 431 |
] |
| 432 |
|
| 433 |
|
| 434 |
def test_autodoc_exclude_members() -> None: |
| 435 |
options = { |
| 436 |
'members': None, |
| 437 |
'exclude-members': 'inheritedmeth,inheritedstaticmeth', |
| 438 |
} |
| 439 |
actual = do_autodoc('class', 'target.inheritance.Base', options=options) |
| 440 |
assert [line for line in actual if '::' in line] == [ |
| 441 |
'.. py:class:: Base()', |
| 442 |
' .. py:attribute:: Base.inheritedattr', |
| 443 |
' .. py:method:: Base.inheritedclassmeth()', |
| 444 |
] |
| 445 |
|
| 446 |
# members vs exclude-members |
| 447 |
options = { |
| 448 |
'members': 'inheritedmeth', |
| 449 |
'exclude-members': 'inheritedmeth', |
| 450 |
} |
| 451 |
actual = do_autodoc('class', 'target.inheritance.Base', options=options) |
| 452 |
assert [line for line in actual if '::' in line] == [ |
| 453 |
'.. py:class:: Base()', |
| 454 |
] |
| 455 |
|
| 456 |
# + has no effect when autodoc_default_options are not present |
| 457 |
options = { |
| 458 |
'members': None, |
| 459 |
'exclude-members': '+inheritedmeth,inheritedstaticmeth', |
| 460 |
} |
| 461 |
actual = do_autodoc('class', 'target.inheritance.Base', options=options) |
| 462 |
assert [line for line in actual if '::' in line] == [ |
| 463 |
'.. py:class:: Base()', |
| 464 |
' .. py:attribute:: Base.inheritedattr', |
| 465 |
' .. py:method:: Base.inheritedclassmeth()', |
| 466 |
] |
| 467 |
|
| 468 |
# exclude-members overrides autodoc_default_options |
| 469 |
options = { |
| 470 |
'members': None, |
| 471 |
'exclude-members': 'inheritedmeth', |
| 472 |
} |
| 473 |
config = _AutodocConfig( |
| 474 |
autodoc_default_options={'exclude-members': 'inheritedstaticmeth'} |
| 475 |
) |
| 476 |
actual = do_autodoc( |
| 477 |
'class', 'target.inheritance.Base', config=config, options=options |
| 478 |
) |
| 479 |
assert [line for line in actual if '::' in line] == [ |
| 480 |
'.. py:class:: Base()', |
| 481 |
' .. py:attribute:: Base.inheritedattr', |
| 482 |
' .. py:method:: Base.inheritedclassmeth()', |
| 483 |
' .. py:method:: Base.inheritedstaticmeth(cls)', |
| 484 |
] |
| 485 |
|
| 486 |
# exclude-members extends autodoc_default_options |
| 487 |
options = { |
| 488 |
'members': None, |
| 489 |
'exclude-members': '+inheritedmeth', |
| 490 |
} |
| 491 |
config = _AutodocConfig( |
| 492 |
autodoc_default_options={'exclude-members': 'inheritedstaticmeth'} |
| 493 |
) |
| 494 |
actual = do_autodoc( |
| 495 |
'class', 'target.inheritance.Base', config=config, options=options |
| 496 |
) |
| 497 |
assert [line for line in actual if '::' in line] == [ |
| 498 |
'.. py:class:: Base()', |
| 499 |
' .. py:attribute:: Base.inheritedattr', |
| 500 |
' .. py:method:: Base.inheritedclassmeth()', |
| 501 |
] |
| 502 |
|
| 503 |
# no exclude-members causes use autodoc_default_options |
| 504 |
options = {'members': None} |
| 505 |
config = _AutodocConfig( |
| 506 |
autodoc_default_options={'exclude-members': 'inheritedstaticmeth,inheritedmeth'} |
| 507 |
) |
| 508 |
actual = do_autodoc( |
| 509 |
'class', 'target.inheritance.Base', config=config, options=options |
| 510 |
) |
| 511 |
assert [line for line in actual if '::' in line] == [ |
| 512 |
'.. py:class:: Base()', |
| 513 |
' .. py:attribute:: Base.inheritedattr', |
| 514 |
' .. py:method:: Base.inheritedclassmeth()', |
| 515 |
] |
| 516 |
|
| 517 |
# empty exclude-members cancels autodoc_default_options |
| 518 |
options = { |
| 519 |
'members': None, |
| 520 |
'exclude-members': None, |
| 521 |
} |
| 522 |
config = _AutodocConfig( |
| 523 |
autodoc_default_options={'exclude-members': 'inheritedstaticmeth,inheritedmeth'} |
| 524 |
) |
| 525 |
actual = do_autodoc( |
| 526 |
'class', 'target.inheritance.Base', config=config, options=options |
| 527 |
) |
| 528 |
assert [line for line in actual if '::' in line] == [ |
| 529 |
'.. py:class:: Base()', |
| 530 |
' .. py:attribute:: Base.inheritedattr', |
| 531 |
' .. py:method:: Base.inheritedclassmeth()', |
| 532 |
' .. py:method:: Base.inheritedmeth()', |
| 533 |
' .. py:method:: Base.inheritedstaticmeth(cls)', |
| 534 |
] |
| 535 |
|
| 536 |
|
| 537 |
def test_autodoc_undoc_members() -> None: |
| 538 |
options = { |
| 539 |
'members': None, |
| 540 |
'undoc-members': None, |
| 541 |
} |
| 542 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 543 |
assert [line for line in actual if '::' in line] == [ |
| 544 |
'.. py:class:: Class(arg)', |
| 545 |
' .. py:method:: Class.a_staticmeth()', |
| 546 |
' .. py:attribute:: Class.attr', |
| 547 |
' .. py:method:: Class.b_staticmeth()', |
| 548 |
' .. py:attribute:: Class.docattr', |
| 549 |
' .. py:method:: Class.excludemeth()', |
| 550 |
' .. py:attribute:: Class.inst_attr_comment', |
| 551 |
' .. py:attribute:: Class.inst_attr_inline', |
| 552 |
' .. py:attribute:: Class.inst_attr_string', |
| 553 |
' .. py:attribute:: Class.mdocattr', |
| 554 |
' .. py:method:: Class.meth()', |
| 555 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 556 |
' .. py:method:: Class.roger(a, *, b=2, c=3, d=4, e=5, f=6)', |
| 557 |
' .. py:attribute:: Class.skipattr', |
| 558 |
' .. py:method:: Class.skipmeth()', |
| 559 |
' .. py:attribute:: Class.udocattr', |
| 560 |
' .. py:method:: Class.undocmeth()', |
| 561 |
] |
| 562 |
|
| 563 |
# use autodoc_default_options |
| 564 |
options = {'members': None} |
| 565 |
config = _AutodocConfig(autodoc_default_options={'undoc-members': True}) |
| 566 |
actual = do_autodoc('class', 'target.Class', config=config, options=options) |
| 567 |
assert [line for line in actual if '::' in line] == [ |
| 568 |
'.. py:class:: Class(arg)', |
| 569 |
' .. py:method:: Class.a_staticmeth()', |
| 570 |
' .. py:attribute:: Class.attr', |
| 571 |
' .. py:method:: Class.b_staticmeth()', |
| 572 |
' .. py:attribute:: Class.docattr', |
| 573 |
' .. py:method:: Class.excludemeth()', |
| 574 |
' .. py:attribute:: Class.inst_attr_comment', |
| 575 |
' .. py:attribute:: Class.inst_attr_inline', |
| 576 |
' .. py:attribute:: Class.inst_attr_string', |
| 577 |
' .. py:attribute:: Class.mdocattr', |
| 578 |
' .. py:method:: Class.meth()', |
| 579 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 580 |
' .. py:method:: Class.roger(a, *, b=2, c=3, d=4, e=5, f=6)', |
| 581 |
' .. py:attribute:: Class.skipattr', |
| 582 |
' .. py:method:: Class.skipmeth()', |
| 583 |
' .. py:attribute:: Class.udocattr', |
| 584 |
' .. py:method:: Class.undocmeth()', |
| 585 |
] |
| 586 |
|
| 587 |
# options negation work check |
| 588 |
options = { |
| 589 |
'members': None, |
| 590 |
'no-undoc-members': None, |
| 591 |
} |
| 592 |
actual = do_autodoc('class', 'target.Class', config=config, options=options) |
| 593 |
assert [line for line in actual if '::' in line] == [ |
| 594 |
'.. py:class:: Class(arg)', |
| 595 |
' .. py:attribute:: Class.attr', |
| 596 |
' .. py:attribute:: Class.docattr', |
| 597 |
' .. py:method:: Class.excludemeth()', |
| 598 |
' .. py:attribute:: Class.inst_attr_comment', |
| 599 |
' .. py:attribute:: Class.inst_attr_inline', |
| 600 |
' .. py:attribute:: Class.inst_attr_string', |
| 601 |
' .. py:attribute:: Class.mdocattr', |
| 602 |
' .. py:method:: Class.meth()', |
| 603 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 604 |
' .. py:method:: Class.skipmeth()', |
| 605 |
' .. py:attribute:: Class.udocattr', |
| 606 |
] |
| 607 |
|
| 608 |
|
| 609 |
def test_autodoc_undoc_members_for_metadata_only() -> None: |
| 610 |
# metadata only member is not displayed |
| 611 |
options = {'members': None} |
| 612 |
actual = do_autodoc('module', 'target.metadata', options=options) |
| 613 |
assert actual == [ |
| 614 |
'', |
| 615 |
'.. py:module:: target.metadata', |
| 616 |
'', |
| 617 |
] |
| 618 |
|
| 619 |
# metadata only member is displayed when undoc-member given |
| 620 |
options = { |
| 621 |
'members': None, |
| 622 |
'undoc-members': None, |
| 623 |
} |
| 624 |
actual = do_autodoc('module', 'target.metadata', options=options) |
| 625 |
assert actual == [ |
| 626 |
'', |
| 627 |
'.. py:module:: target.metadata', |
| 628 |
'', |
| 629 |
'', |
| 630 |
'.. py:function:: foo()', |
| 631 |
' :module: target.metadata', |
| 632 |
'', |
| 633 |
' :meta metadata-only-docstring:', |
| 634 |
'', |
| 635 |
] |
| 636 |
|
| 637 |
|
| 638 |
def test_autodoc_inherited_members() -> None: |
| 639 |
options = { |
| 640 |
'members': None, |
| 641 |
'inherited-members': None, |
| 642 |
} |
| 643 |
actual = do_autodoc('class', 'target.inheritance.Derived', options=options) |
| 644 |
assert [line for line in actual if 'method::' in line] == [ |
| 645 |
' .. py:method:: Derived.another_inheritedmeth()', |
| 646 |
' .. py:method:: Derived.inheritedclassmeth()', |
| 647 |
' .. py:method:: Derived.inheritedmeth()', |
| 648 |
' .. py:method:: Derived.inheritedstaticmeth(cls)', |
| 649 |
] |
| 650 |
|
| 651 |
|
| 652 |
def test_autodoc_inherited_members_Base() -> None: |
| 653 |
options = { |
| 654 |
'members': None, |
| 655 |
'inherited-members': 'Base', |
| 656 |
'special-members': None, |
| 657 |
} |
| 658 |
|
| 659 |
# check methods for object class are shown |
| 660 |
actual = do_autodoc('class', 'target.inheritance.Derived', options=options) |
| 661 |
assert ' .. py:method:: Derived.inheritedmeth()' in actual |
| 662 |
assert ' .. py:method:: Derived.inheritedclassmeth' not in actual |
| 663 |
|
| 664 |
|
| 665 |
def test_autodoc_inherited_members_None() -> None: |
| 666 |
options = { |
| 667 |
'members': None, |
| 668 |
'inherited-members': 'None', |
| 669 |
'special-members': None, |
| 670 |
} |
| 671 |
|
| 672 |
# check methods for object class are shown |
| 673 |
actual = do_autodoc('class', 'target.inheritance.Derived', options=options) |
| 674 |
assert ' .. py:method:: Derived.__init__()' in actual |
| 675 |
assert ' .. py:method:: Derived.__str__()' in actual |
| 676 |
|
| 677 |
|
| 678 |
def test_autodoc_imported_members() -> None: |
| 679 |
options = { |
| 680 |
'members': None, |
| 681 |
'imported-members': None, |
| 682 |
'ignore-module-all': None, |
| 683 |
} |
| 684 |
actual = do_autodoc('module', 'target', options=options) |
| 685 |
assert ( |
| 686 |
'.. py:function:: function_to_be_imported(app: ~sphinx.application.Sphinx | None) -> str' |
| 687 |
) in actual |
| 688 |
|
| 689 |
|
| 690 |
def test_autodoc_special_members() -> None: |
| 691 |
# specific special methods |
| 692 |
options = { |
| 693 |
'undoc-members': None, |
| 694 |
'special-members': '__init__,__special1__', |
| 695 |
} |
| 696 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 697 |
assert [line for line in actual if '::' in line] == [ |
| 698 |
'.. py:class:: Class(arg)', |
| 699 |
' .. py:method:: Class.__init__(arg)', |
| 700 |
' .. py:method:: Class.__special1__()', |
| 701 |
] |
| 702 |
|
| 703 |
# combination with specific members |
| 704 |
options = { |
| 705 |
'members': 'attr,docattr', |
| 706 |
'undoc-members': None, |
| 707 |
'special-members': '__init__,__special1__', |
| 708 |
} |
| 709 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 710 |
assert [line for line in actual if '::' in line] == [ |
| 711 |
'.. py:class:: Class(arg)', |
| 712 |
' .. py:method:: Class.__init__(arg)', |
| 713 |
' .. py:method:: Class.__special1__()', |
| 714 |
' .. py:attribute:: Class.attr', |
| 715 |
' .. py:attribute:: Class.docattr', |
| 716 |
] |
| 717 |
|
| 718 |
# all special methods |
| 719 |
options = { |
| 720 |
'members': None, |
| 721 |
'undoc-members': None, |
| 722 |
'special-members': None, |
| 723 |
} |
| 724 |
if sys.version_info >= (3, 13, 0, 'alpha', 5): |
| 725 |
options['exclude-members'] = '__static_attributes__,__firstlineno__' |
| 726 |
if sys.version_info >= (3, 14, 0, 'alpha', 7): |
| 727 |
ann_attrs: tuple[str, ...] = ( |
| 728 |
' .. py:attribute:: Class.__annotate_func__', |
| 729 |
' .. py:attribute:: Class.__annotations_cache__', |
| 730 |
) |
| 731 |
else: |
| 732 |
ann_attrs = (' .. py:attribute:: Class.__annotations__',) |
| 733 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 734 |
assert [line for line in actual if '::' in line] == [ |
| 735 |
'.. py:class:: Class(arg)', |
| 736 |
*ann_attrs, |
| 737 |
' .. py:attribute:: Class.__dict__', |
| 738 |
' .. py:method:: Class.__init__(arg)', |
| 739 |
' .. py:attribute:: Class.__module__', |
| 740 |
' .. py:method:: Class.__special1__()', |
| 741 |
' .. py:method:: Class.__special2__()', |
| 742 |
' .. py:attribute:: Class.__weakref__', |
| 743 |
' .. py:method:: Class.a_staticmeth()', |
| 744 |
' .. py:attribute:: Class.attr', |
| 745 |
' .. py:method:: Class.b_staticmeth()', |
| 746 |
' .. py:attribute:: Class.docattr', |
| 747 |
' .. py:method:: Class.excludemeth()', |
| 748 |
' .. py:attribute:: Class.inst_attr_comment', |
| 749 |
' .. py:attribute:: Class.inst_attr_inline', |
| 750 |
' .. py:attribute:: Class.inst_attr_string', |
| 751 |
' .. py:attribute:: Class.mdocattr', |
| 752 |
' .. py:method:: Class.meth()', |
| 753 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 754 |
' .. py:method:: Class.roger(a, *, b=2, c=3, d=4, e=5, f=6)', |
| 755 |
' .. py:attribute:: Class.skipattr', |
| 756 |
' .. py:method:: Class.skipmeth()', |
| 757 |
' .. py:attribute:: Class.udocattr', |
| 758 |
' .. py:method:: Class.undocmeth()', |
| 759 |
] |
| 760 |
|
| 761 |
# specific special methods from autodoc_default_options |
| 762 |
options = {'undoc-members': None} |
| 763 |
config = _AutodocConfig(autodoc_default_options={'special-members': '__special2__'}) |
| 764 |
actual = do_autodoc('class', 'target.Class', config=config, options=options) |
| 765 |
assert [line for line in actual if '::' in line] == [ |
| 766 |
'.. py:class:: Class(arg)', |
| 767 |
' .. py:method:: Class.__special2__()', |
| 768 |
] |
| 769 |
|
| 770 |
# specific special methods option with autodoc_default_options |
| 771 |
options = { |
| 772 |
'undoc-members': None, |
| 773 |
'special-members': '__init__,__special1__', |
| 774 |
} |
| 775 |
actual = do_autodoc('class', 'target.Class', config=config, options=options) |
| 776 |
assert [line for line in actual if '::' in line] == [ |
| 777 |
'.. py:class:: Class(arg)', |
| 778 |
' .. py:method:: Class.__init__(arg)', |
| 779 |
' .. py:method:: Class.__special1__()', |
| 780 |
] |
| 781 |
|
| 782 |
# specific special methods merge with autodoc_default_options |
| 783 |
options = { |
| 784 |
'undoc-members': None, |
| 785 |
'special-members': '+__init__,__special1__', |
| 786 |
} |
| 787 |
actual = do_autodoc('class', 'target.Class', config=config, options=options) |
| 788 |
assert [line for line in actual if '::' in line] == [ |
| 789 |
'.. py:class:: Class(arg)', |
| 790 |
' .. py:method:: Class.__init__(arg)', |
| 791 |
' .. py:method:: Class.__special1__()', |
| 792 |
' .. py:method:: Class.__special2__()', |
| 793 |
] |
| 794 |
|
| 795 |
|
| 796 |
def test_autodoc_ignore_module_all() -> None: |
| 797 |
# default (no-ignore-module-all) |
| 798 |
options = {'members': None} |
| 799 |
actual = do_autodoc('module', 'target', options=options) |
| 800 |
assert [line for line in actual if 'class::' in line] == [ |
| 801 |
'.. py:class:: Class(arg)', |
| 802 |
] |
| 803 |
|
| 804 |
# ignore-module-all |
| 805 |
options = { |
| 806 |
'members': None, |
| 807 |
'ignore-module-all': None, |
| 808 |
} |
| 809 |
actual = do_autodoc('module', 'target', options=options) |
| 810 |
assert [line for line in actual if 'class::' in line] == [ |
| 811 |
'.. py:class:: Class(arg)', |
| 812 |
'.. py:class:: CustomDict', |
| 813 |
'.. py:class:: InnerChild()', |
| 814 |
'.. py:class:: InstAttCls()', |
| 815 |
'.. py:class:: Outer()', |
| 816 |
' .. py:class:: Outer.Inner()', |
| 817 |
'.. py:class:: StrRepr', |
| 818 |
] |
| 819 |
|
| 820 |
|
| 821 |
def test_autodoc_noindex() -> None: |
| 822 |
options = {'no-index': None} |
| 823 |
actual = do_autodoc('module', 'target', options=options) |
| 824 |
assert actual == [ |
| 825 |
'', |
| 826 |
'.. py:module:: target', |
| 827 |
' :no-index:', |
| 828 |
'', |
| 829 |
] |
| 830 |
|
| 831 |
# TODO: :no-index: should be propagated to children of target item. |
| 832 |
|
| 833 |
actual = do_autodoc('class', 'target.inheritance.Base', options=options) |
| 834 |
assert actual == [ |
| 835 |
'', |
| 836 |
'.. py:class:: Base()', |
| 837 |
' :no-index:', |
| 838 |
' :module: target.inheritance', |
| 839 |
'', |
| 840 |
] |
| 841 |
|
| 842 |
|
| 843 |
def test_autodoc_subclass_of_builtin_class() -> None: |
| 844 |
options = {'members': None} |
| 845 |
actual = do_autodoc('class', 'target.CustomDict', options=options) |
| 846 |
assert actual == [ |
| 847 |
'', |
| 848 |
'.. py:class:: CustomDict', |
| 849 |
' :module: target', |
| 850 |
'', |
| 851 |
' Docstring.', |
| 852 |
'', |
| 853 |
] |
| 854 |
|
| 855 |
|
| 856 |
def test_autodoc_inner_class() -> None: |
| 857 |
options = {'members': None} |
| 858 |
actual = do_autodoc('class', 'target.Outer', options=options) |
| 859 |
assert actual == [ |
| 860 |
'', |
| 861 |
'.. py:class:: Outer()', |
| 862 |
' :module: target', |
| 863 |
'', |
| 864 |
' Foo', |
| 865 |
'', |
| 866 |
'', |
| 867 |
' .. py:class:: Outer.Inner()', |
| 868 |
' :module: target', |
| 869 |
'', |
| 870 |
' Foo', |
| 871 |
'', |
| 872 |
'', |
| 873 |
' .. py:method:: Outer.Inner.meth()', |
| 874 |
' :module: target', |
| 875 |
'', |
| 876 |
' Foo', |
| 877 |
'', |
| 878 |
'', |
| 879 |
' .. py:attribute:: Outer.factory', |
| 880 |
' :module: target', |
| 881 |
'', |
| 882 |
' alias of :py:class:`dict`', |
| 883 |
] |
| 884 |
|
| 885 |
actual = do_autodoc('class', 'target.Outer.Inner', options=options) |
| 886 |
assert actual == [ |
| 887 |
'', |
| 888 |
'.. py:class:: Inner()', |
| 889 |
' :module: target.Outer', |
| 890 |
'', |
| 891 |
' Foo', |
| 892 |
'', |
| 893 |
'', |
| 894 |
' .. py:method:: Inner.meth()', |
| 895 |
' :module: target.Outer', |
| 896 |
'', |
| 897 |
' Foo', |
| 898 |
'', |
| 899 |
] |
| 900 |
|
| 901 |
options['show-inheritance'] = None |
| 902 |
actual = do_autodoc('class', 'target.InnerChild', options=options) |
| 903 |
assert actual == [ |
| 904 |
'', |
| 905 |
'.. py:class:: InnerChild()', |
| 906 |
' :module: target', |
| 907 |
'', |
| 908 |
' Bases: :py:class:`~target.Outer.Inner`', |
| 909 |
'', |
| 910 |
' InnerChild docstring', |
| 911 |
'', |
| 912 |
] |
| 913 |
|
| 914 |
|
| 915 |
def test_autodoc_classmethod() -> None: |
| 916 |
actual = do_autodoc('method', 'target.inheritance.Base.inheritedclassmeth') |
| 917 |
assert actual == [ |
| 918 |
'', |
| 919 |
'.. py:method:: Base.inheritedclassmeth()', |
| 920 |
' :module: target.inheritance', |
| 921 |
' :classmethod:', |
| 922 |
'', |
| 923 |
' Inherited class method.', |
| 924 |
'', |
| 925 |
] |
| 926 |
|
| 927 |
|
| 928 |
def test_autodoc_staticmethod() -> None: |
| 929 |
actual = do_autodoc('method', 'target.inheritance.Base.inheritedstaticmeth') |
| 930 |
assert actual == [ |
| 931 |
'', |
| 932 |
'.. py:method:: Base.inheritedstaticmeth(cls)', |
| 933 |
' :module: target.inheritance', |
| 934 |
' :staticmethod:', |
| 935 |
'', |
| 936 |
' Inherited static method.', |
| 937 |
'', |
| 938 |
] |
| 939 |
|
| 940 |
|
| 941 |
def test_autodoc_descriptor() -> None: |
| 942 |
options = { |
| 943 |
'members': None, |
| 944 |
'undoc-members': None, |
| 945 |
} |
| 946 |
actual = do_autodoc('class', 'target.descriptor.Class', options=options) |
| 947 |
assert actual == [ |
| 948 |
'', |
| 949 |
'.. py:class:: Class()', |
| 950 |
' :module: target.descriptor', |
| 951 |
'', |
| 952 |
'', |
| 953 |
' .. py:attribute:: Class.descr', |
| 954 |
' :module: target.descriptor', |
| 955 |
'', |
| 956 |
' Descriptor instance docstring.', |
| 957 |
'', |
| 958 |
'', |
| 959 |
' .. py:property:: Class.prop', |
| 960 |
' :module: target.descriptor', |
| 961 |
'', |
| 962 |
' Property.', |
| 963 |
'', |
| 964 |
] |
| 965 |
|
| 966 |
|
| 967 |
def test_autodoc_cached_property() -> None: |
| 968 |
options = { |
| 969 |
'members': None, |
| 970 |
'undoc-members': None, |
| 971 |
} |
| 972 |
actual = do_autodoc('class', 'target.cached_property.Foo', options=options) |
| 973 |
assert actual == [ |
| 974 |
'', |
| 975 |
'.. py:class:: Foo()', |
| 976 |
' :module: target.cached_property', |
| 977 |
'', |
| 978 |
'', |
| 979 |
' .. py:property:: Foo.prop', |
| 980 |
' :module: target.cached_property', |
| 981 |
' :type: int', |
| 982 |
'', |
| 983 |
'', |
| 984 |
' .. py:property:: Foo.prop_with_type_comment', |
| 985 |
' :module: target.cached_property', |
| 986 |
' :type: int', |
| 987 |
'', |
| 988 |
] |
| 989 |
|
| 990 |
|
| 991 |
def test_autodoc_member_order() -> None: |
| 992 |
# case member-order='bysource' |
| 993 |
options = { |
| 994 |
'members': None, |
| 995 |
'member-order': 'bysource', |
| 996 |
'undoc-members': None, |
| 997 |
'private-members': None, |
| 998 |
} |
| 999 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 1000 |
assert [line for line in actual if '::' in line] == [ |
| 1001 |
'.. py:class:: Class(arg)', |
| 1002 |
' .. py:method:: Class.meth()', |
| 1003 |
' .. py:method:: Class.undocmeth()', |
| 1004 |
' .. py:method:: Class.skipmeth()', |
| 1005 |
' .. py:method:: Class.excludemeth()', |
| 1006 |
' .. py:attribute:: Class.skipattr', |
| 1007 |
' .. py:attribute:: Class.attr', |
| 1008 |
' .. py:attribute:: Class.docattr', |
| 1009 |
' .. py:attribute:: Class.udocattr', |
| 1010 |
' .. py:attribute:: Class.mdocattr', |
| 1011 |
' .. py:method:: Class.roger(a, *, b=2, c=3, d=4, e=5, f=6)', |
| 1012 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 1013 |
' .. py:method:: Class.b_staticmeth()', |
| 1014 |
' .. py:method:: Class.a_staticmeth()', |
| 1015 |
' .. py:attribute:: Class.inst_attr_inline', |
| 1016 |
' .. py:attribute:: Class.inst_attr_comment', |
| 1017 |
' .. py:attribute:: Class.inst_attr_string', |
| 1018 |
' .. py:attribute:: Class._private_inst_attr', |
| 1019 |
] |
| 1020 |
|
| 1021 |
# case member-order='groupwise' |
| 1022 |
options = { |
| 1023 |
'members': None, |
| 1024 |
'member-order': 'groupwise', |
| 1025 |
'undoc-members': None, |
| 1026 |
'private-members': None, |
| 1027 |
} |
| 1028 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 1029 |
assert [line for line in actual if '::' in line] == [ |
| 1030 |
'.. py:class:: Class(arg)', |
| 1031 |
# class methods |
| 1032 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 1033 |
' .. py:method:: Class.roger(a, *, b=2, c=3, d=4, e=5, f=6)', |
| 1034 |
# static methods |
| 1035 |
' .. py:method:: Class.a_staticmeth()', |
| 1036 |
' .. py:method:: Class.b_staticmeth()', |
| 1037 |
# regular methods |
| 1038 |
' .. py:method:: Class.excludemeth()', |
| 1039 |
' .. py:method:: Class.meth()', |
| 1040 |
' .. py:method:: Class.skipmeth()', |
| 1041 |
' .. py:method:: Class.undocmeth()', |
| 1042 |
' .. py:attribute:: Class._private_inst_attr', |
| 1043 |
' .. py:attribute:: Class.attr', |
| 1044 |
' .. py:attribute:: Class.docattr', |
| 1045 |
' .. py:attribute:: Class.inst_attr_comment', |
| 1046 |
' .. py:attribute:: Class.inst_attr_inline', |
| 1047 |
' .. py:attribute:: Class.inst_attr_string', |
| 1048 |
' .. py:attribute:: Class.mdocattr', |
| 1049 |
' .. py:attribute:: Class.skipattr', |
| 1050 |
' .. py:attribute:: Class.udocattr', |
| 1051 |
] |
| 1052 |
|
| 1053 |
# case member-order=None |
| 1054 |
options = { |
| 1055 |
'members': None, |
| 1056 |
'undoc-members': None, |
| 1057 |
'private-members': None, |
| 1058 |
} |
| 1059 |
actual = do_autodoc('class', 'target.Class', options=options) |
| 1060 |
assert [line for line in actual if '::' in line] == [ |
| 1061 |
'.. py:class:: Class(arg)', |
| 1062 |
' .. py:attribute:: Class._private_inst_attr', |
| 1063 |
' .. py:method:: Class.a_staticmeth()', |
| 1064 |
' .. py:attribute:: Class.attr', |
| 1065 |
' .. py:method:: Class.b_staticmeth()', |
| 1066 |
' .. py:attribute:: Class.docattr', |
| 1067 |
' .. py:method:: Class.excludemeth()', |
| 1068 |
' .. py:attribute:: Class.inst_attr_comment', |
| 1069 |
' .. py:attribute:: Class.inst_attr_inline', |
| 1070 |
' .. py:attribute:: Class.inst_attr_string', |
| 1071 |
' .. py:attribute:: Class.mdocattr', |
| 1072 |
' .. py:method:: Class.meth()', |
| 1073 |
' .. py:method:: Class.moore(a, e, f) -> happiness', |
| 1074 |
' .. py:method:: Class.roger(a, *, b=2, c=3, d=4, e=5, f=6)', |
| 1075 |
' .. py:attribute:: Class.skipattr', |
| 1076 |
' .. py:method:: Class.skipmeth()', |
| 1077 |
' .. py:attribute:: Class.udocattr', |
| 1078 |
' .. py:method:: Class.undocmeth()', |
| 1079 |
] |
| 1080 |
|
| 1081 |
|
| 1082 |
def test_autodoc_module_member_order() -> None: |
| 1083 |
# case member-order='bysource' |
| 1084 |
options = { |
| 1085 |
'members': 'foo, Bar, baz, qux, Quux, foobar', |
| 1086 |
'member-order': 'bysource', |
| 1087 |
'undoc-members': None, |
| 1088 |
} |
| 1089 |
actual = do_autodoc('module', 'target.sort_by_all', options=options) |
| 1090 |
assert [line for line in actual if '::' in line] == [ |
| 1091 |
'.. py:module:: target.sort_by_all', |
| 1092 |
'.. py:function:: baz()', |
| 1093 |
'.. py:function:: foo()', |
| 1094 |
'.. py:class:: Bar()', |
| 1095 |
'.. py:class:: Quux()', |
| 1096 |
'.. py:function:: foobar()', |
| 1097 |
'.. py:function:: qux()', |
| 1098 |
] |
| 1099 |
|
| 1100 |
# case member-order='bysource' and ignore-module-all |
| 1101 |
options = { |
| 1102 |
'members': 'foo, Bar, baz, qux, Quux, foobar', |
| 1103 |
'member-order': 'bysource', |
| 1104 |
'undoc-members': None, |
| 1105 |
'ignore-module-all': None, |
| 1106 |
} |
| 1107 |
actual = do_autodoc('module', 'target.sort_by_all', options=options) |
| 1108 |
assert [line for line in actual if '::' in line] == [ |
| 1109 |
'.. py:module:: target.sort_by_all', |
| 1110 |
'.. py:function:: foo()', |
| 1111 |
'.. py:class:: Bar()', |
| 1112 |
'.. py:function:: baz()', |
| 1113 |
'.. py:function:: qux()', |
| 1114 |
'.. py:class:: Quux()', |
| 1115 |
'.. py:function:: foobar()', |
| 1116 |
] |
| 1117 |
|
| 1118 |
|
| 1119 |
def test_autodoc_module_scope() -> None: |
| 1120 |
current_document = _CurrentDocument(docname='index') |
| 1121 |
current_document.autodoc_module = 'target' |
| 1122 |
actual = do_autodoc( |
| 1123 |
'attribute', 'Class.mdocattr', current_document=current_document |
| 1124 |
) |
| 1125 |
assert actual == [ |
| 1126 |
'', |
| 1127 |
'.. py:attribute:: Class.mdocattr', |
| 1128 |
' :module: target', |
| 1129 |
' :value: <_io.StringIO object>', |
| 1130 |
'', |
| 1131 |
' should be documented as well - sĂĽĂź', |
| 1132 |
'', |
| 1133 |
] |
| 1134 |
|
| 1135 |
|
| 1136 |
def test_autodoc_class_scope() -> None: |
| 1137 |
current_document = _CurrentDocument(docname='index') |
| 1138 |
current_document.autodoc_module = 'target' |
| 1139 |
current_document.autodoc_class = 'Class' |
| 1140 |
actual = do_autodoc('attribute', 'mdocattr', current_document=current_document) |
| 1141 |
assert actual == [ |
| 1142 |
'', |
| 1143 |
'.. py:attribute:: Class.mdocattr', |
| 1144 |
' :module: target', |
| 1145 |
' :value: <_io.StringIO object>', |
| 1146 |
'', |
| 1147 |
' should be documented as well - sĂĽĂź', |
| 1148 |
'', |
| 1149 |
] |
| 1150 |
|
| 1151 |
|
| 1152 |
def test_class_attributes() -> None: |
| 1153 |
options = { |
| 1154 |
'members': None, |
| 1155 |
'undoc-members': None, |
| 1156 |
} |
| 1157 |
actual = do_autodoc('class', 'target.AttCls', options=options) |
| 1158 |
assert actual == [ |
| 1159 |
'', |
| 1160 |
'.. py:class:: AttCls()', |
| 1161 |
' :module: target', |
| 1162 |
'', |
| 1163 |
'', |
| 1164 |
' .. py:attribute:: AttCls.a1', |
| 1165 |
' :module: target', |
| 1166 |
' :value: hello world', |
| 1167 |
'', |
| 1168 |
'', |
| 1169 |
' .. py:attribute:: AttCls.a2', |
| 1170 |
' :module: target', |
| 1171 |
' :value: None', |
| 1172 |
'', |
| 1173 |
] |
| 1174 |
|
| 1175 |
|
| 1176 |
def test_autoclass_instance_attributes() -> None: |
| 1177 |
options: dict[str, Any] |
| 1178 |
options = {'members': None} |
| 1179 |
actual = do_autodoc('class', 'target.InstAttCls', options=options) |
| 1180 |
assert actual == [ |
| 1181 |
'', |
| 1182 |
'.. py:class:: InstAttCls()', |
| 1183 |
' :module: target', |
| 1184 |
'', |
| 1185 |
' Class with documented class and instance attributes.', |
| 1186 |
'', |
| 1187 |
'', |
| 1188 |
' .. py:attribute:: InstAttCls.ca1', |
| 1189 |
' :module: target', |
| 1190 |
" :value: 'a'", |
| 1191 |
'', |
| 1192 |
' Doc comment for class attribute InstAttCls.ca1.', |
| 1193 |
' It can have multiple lines.', |
| 1194 |
'', |
| 1195 |
'', |
| 1196 |
' .. py:attribute:: InstAttCls.ca2', |
| 1197 |
' :module: target', |
| 1198 |
" :value: 'b'", |
| 1199 |
'', |
| 1200 |
' Doc comment for InstAttCls.ca2. One line only.', |
| 1201 |
'', |
| 1202 |
'', |
| 1203 |
' .. py:attribute:: InstAttCls.ca3', |
| 1204 |
' :module: target', |
| 1205 |
" :value: 'c'", |
| 1206 |
'', |
| 1207 |
' Docstring for class attribute InstAttCls.ca3.', |
| 1208 |
'', |
| 1209 |
'', |
| 1210 |
' .. py:attribute:: InstAttCls.ia1', |
| 1211 |
' :module: target', |
| 1212 |
'', |
| 1213 |
' Doc comment for instance attribute InstAttCls.ia1', |
| 1214 |
'', |
| 1215 |
'', |
| 1216 |
' .. py:attribute:: InstAttCls.ia2', |
| 1217 |
' :module: target', |
| 1218 |
'', |
| 1219 |
' Docstring for instance attribute InstAttCls.ia2.', |
| 1220 |
'', |
| 1221 |
] |
| 1222 |
|
| 1223 |
# pick up arbitrary attributes |
| 1224 |
options = {'members': 'ca1,ia1'} |
| 1225 |
actual = do_autodoc('class', 'target.InstAttCls', options=options) |
| 1226 |
assert actual == [ |
| 1227 |
'', |
| 1228 |
'.. py:class:: InstAttCls()', |
| 1229 |
' :module: target', |
| 1230 |
'', |
| 1231 |
' Class with documented class and instance attributes.', |
| 1232 |
'', |
| 1233 |
'', |
| 1234 |
' .. py:attribute:: InstAttCls.ca1', |
| 1235 |
' :module: target', |
| 1236 |
" :value: 'a'", |
| 1237 |
'', |
| 1238 |
' Doc comment for class attribute InstAttCls.ca1.', |
| 1239 |
' It can have multiple lines.', |
| 1240 |
'', |
| 1241 |
'', |
| 1242 |
' .. py:attribute:: InstAttCls.ia1', |
| 1243 |
' :module: target', |
| 1244 |
'', |
| 1245 |
' Doc comment for instance attribute InstAttCls.ia1', |
| 1246 |
'', |
| 1247 |
] |
| 1248 |
|
| 1249 |
|
| 1250 |
def test_autoattribute_instance_attributes() -> None: |
| 1251 |
actual = do_autodoc('attribute', 'target.InstAttCls.ia1') |
| 1252 |
assert actual == [ |
| 1253 |
'', |
| 1254 |
'.. py:attribute:: InstAttCls.ia1', |
| 1255 |
' :module: target', |
| 1256 |
'', |
| 1257 |
' Doc comment for instance attribute InstAttCls.ia1', |
| 1258 |
'', |
| 1259 |
] |
| 1260 |
|
| 1261 |
|
| 1262 |
def test_slots() -> None: |
| 1263 |
options = { |
| 1264 |
'members': None, |
| 1265 |
'undoc-members': None, |
| 1266 |
} |
| 1267 |
actual = do_autodoc('module', 'target.slots', options=options) |
| 1268 |
assert actual == [ |
| 1269 |
'', |
| 1270 |
'.. py:module:: target.slots', |
| 1271 |
'', |
| 1272 |
'', |
| 1273 |
'.. py:class:: Bar()', |
| 1274 |
' :module: target.slots', |
| 1275 |
'', |
| 1276 |
' docstring', |
| 1277 |
'', |
| 1278 |
'', |
| 1279 |
' .. py:attribute:: Bar.attr1', |
| 1280 |
' :module: target.slots', |
| 1281 |
' :type: int', |
| 1282 |
'', |
| 1283 |
' docstring of attr1', |
| 1284 |
'', |
| 1285 |
'', |
| 1286 |
' .. py:attribute:: Bar.attr2', |
| 1287 |
' :module: target.slots', |
| 1288 |
'', |
| 1289 |
' docstring of instance attr2', |
| 1290 |
'', |
| 1291 |
'', |
| 1292 |
' .. py:attribute:: Bar.attr3', |
| 1293 |
' :module: target.slots', |
| 1294 |
'', |
| 1295 |
'', |
| 1296 |
'.. py:class:: Baz()', |
| 1297 |
' :module: target.slots', |
| 1298 |
'', |
| 1299 |
' docstring', |
| 1300 |
'', |
| 1301 |
'', |
| 1302 |
' .. py:attribute:: Baz.attr', |
| 1303 |
' :module: target.slots', |
| 1304 |
'', |
| 1305 |
'', |
| 1306 |
'.. py:class:: Foo()', |
| 1307 |
' :module: target.slots', |
| 1308 |
'', |
| 1309 |
' docstring', |
| 1310 |
'', |
| 1311 |
'', |
| 1312 |
' .. py:attribute:: Foo.attr', |
| 1313 |
' :module: target.slots', |
| 1314 |
'', |
| 1315 |
] |
| 1316 |
|
| 1317 |
|
| 1318 |
class _EnumFormatter: |
| 1319 |
def __init__(self, name: str, *, module: str = 'target.enums') -> None: |
| 1320 |
self.name = name |
| 1321 |
self.module = module |
| 1322 |
|
| 1323 |
@property |
| 1324 |
def target(self) -> str: |
| 1325 |
"""The autodoc target class.""" |
| 1326 |
return f'{self.module}.{self.name}' |
| 1327 |
|
| 1328 |
def subtarget(self, name: str) -> str: |
| 1329 |
"""The autodoc sub-target (an attribute, method, etc).""" |
| 1330 |
return f'{self.target}.{name}' |
| 1331 |
|
| 1332 |
def _node( |
| 1333 |
self, |
| 1334 |
role: str, |
| 1335 |
name: str, |
| 1336 |
doc: str, |
| 1337 |
*, |
| 1338 |
args: str, |
| 1339 |
indent: int, |
| 1340 |
**options: Any, |
| 1341 |
) -> list[str]: |
| 1342 |
prefix = indent * ' ' |
| 1343 |
tab = ' ' * 3 |
| 1344 |
|
| 1345 |
def rst_option(name: str, value: Any) -> str: |
| 1346 |
value = '' if value == 1 else value # note True == 1. |
| 1347 |
return f'{prefix}{tab}:{name}: {value!s}'.rstrip() |
| 1348 |
|
| 1349 |
lines = [ |
| 1350 |
'', |
| 1351 |
f'{prefix}.. py:{role}:: {name}{args}', |
| 1352 |
f'{prefix}{tab}:module: {self.module}', |
| 1353 |
*itertools.starmap(rst_option, options.items()), |
| 1354 |
] |
| 1355 |
if doc: |
| 1356 |
lines.extend(['', f'{prefix}{tab}{doc}']) |
| 1357 |
lines.append('') |
| 1358 |
return lines |
| 1359 |
|
| 1360 |
def entry( |
| 1361 |
self, |
| 1362 |
entry_name: str, |
| 1363 |
doc: str = '', |
| 1364 |
*, |
| 1365 |
role: str, |
| 1366 |
args: str = '', |
| 1367 |
indent: int = 3, |
| 1368 |
**rst_options: Any, |
| 1369 |
) -> list[str]: |
| 1370 |
"""Get the RST lines for a named attribute, method, etc.""" |
| 1371 |
qualname = f'{self.name}.{entry_name}' |
| 1372 |
return self._node(role, qualname, doc, args=args, indent=indent, **rst_options) |
| 1373 |
|
| 1374 |
def preamble_lookup( |
| 1375 |
self, doc: str, *, indent: int = 0, **options: Any |
| 1376 |
) -> list[str]: |
| 1377 |
assert doc, ( |
| 1378 |
f'enumeration class {self.target!r} should have an explicit docstring' |
| 1379 |
) |
| 1380 |
|
| 1381 |
args = self._preamble_args(functional_constructor=False) |
| 1382 |
return self._preamble(doc=doc, args=args, indent=indent, **options) |
| 1383 |
|
| 1384 |
def preamble_constructor( |
| 1385 |
self, doc: str, *, indent: int = 0, **options: Any |
| 1386 |
) -> list[str]: |
| 1387 |
assert doc, ( |
| 1388 |
f'enumeration class {self.target!r} should have an explicit docstring' |
| 1389 |
) |
| 1390 |
|
| 1391 |
args = self._preamble_args(functional_constructor=True) |
| 1392 |
return self._preamble(doc=doc, args=args, indent=indent, **options) |
| 1393 |
|
| 1394 |
def _preamble( |
| 1395 |
self, *, doc: str, args: str, indent: int = 0, **options: Any |
| 1396 |
) -> list[str]: |
| 1397 |
"""Generate the preamble of the class being documented.""" |
| 1398 |
return self._node('class', self.name, doc, args=args, indent=indent, **options) |
| 1399 |
|
| 1400 |
@staticmethod |
| 1401 |
def _preamble_args(functional_constructor: bool = False) -> str: |
| 1402 |
"""EnumType.__call__() is a dual-purpose method: |
| 1403 |
|
| 1404 |
* Look an enum member (valid only if the enum has members) |
| 1405 |
* Create a new enum class (functional API) |
| 1406 |
""" |
| 1407 |
if sys.version_info[:2] >= (3, 14): |
| 1408 |
if functional_constructor: |
| 1409 |
return ( |
| 1410 |
'(new_class_name, /, names, *, module=None, ' |
| 1411 |
'qualname=None, type=None, start=1, boundary=None)' |
| 1412 |
) |
| 1413 |
else: |
| 1414 |
return '(*values)' |
| 1415 |
if sys.version_info[:2] >= (3, 13) or sys.version_info[:3] >= (3, 12, 3): |
| 1416 |
if functional_constructor: |
| 1417 |
return ( |
| 1418 |
'(new_class_name, /, names, *, module=None, ' |
| 1419 |
'qualname=None, type=None, start=1, boundary=None)' |
| 1420 |
) |
| 1421 |
else: |
| 1422 |
return '(*values)' |
| 1423 |
return ( |
| 1424 |
'(value, names=None, *values, module=None, ' |
| 1425 |
'qualname=None, type=None, start=1, boundary=None)' |
| 1426 |
) |
| 1427 |
|
| 1428 |
def method( |
| 1429 |
self, |
| 1430 |
name: str, |
| 1431 |
doc: str, |
| 1432 |
*flags: str, |
| 1433 |
args: str = '()', |
| 1434 |
indent: int = 3, |
| 1435 |
) -> list[str]: |
| 1436 |
rst_options = dict.fromkeys(flags, '') |
| 1437 |
return self.entry( |
| 1438 |
name, doc, role='method', args=args, indent=indent, **rst_options |
| 1439 |
) |
| 1440 |
|
| 1441 |
def member(self, name: str, value: Any, doc: str, *, indent: int = 3) -> list[str]: |
| 1442 |
rst_options = {'value': repr(value)} |
| 1443 |
return self.entry(name, doc, role='attribute', indent=indent, **rst_options) |
| 1444 |
|
| 1445 |
|
| 1446 |
@pytest.fixture |
| 1447 |
def autodoc_enum_options() -> dict[str, object]: |
| 1448 |
"""Default autodoc options to use when testing enum's documentation.""" |
| 1449 |
return {'members': None, 'undoc-members': None} |
| 1450 |
|
| 1451 |
|
| 1452 |
def test_enum_class(autodoc_enum_options): |
| 1453 |
fmt = _EnumFormatter('EnumCls') |
| 1454 |
options = autodoc_enum_options | {'private-members': None} |
| 1455 |
|
| 1456 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1457 |
assert actual == [ |
| 1458 |
*fmt.preamble_lookup('this is enum class'), |
| 1459 |
*fmt.method( |
| 1460 |
'say_goodbye', 'a classmethod says good-bye to you.', 'classmethod' |
| 1461 |
), |
| 1462 |
*fmt.method('say_hello', 'a method says hello to you.'), |
| 1463 |
*fmt.member('val1', 12, 'doc for val1'), |
| 1464 |
*fmt.member('val2', 23, 'doc for val2'), |
| 1465 |
*fmt.member('val3', 34, 'doc for val3'), |
| 1466 |
*fmt.member('val4', 34, ''), # val4 is alias of val3 |
| 1467 |
] |
| 1468 |
|
| 1469 |
# Inherited members exclude the native Enum API (in particular |
| 1470 |
# the 'name' and 'value' properties), unless they were explicitly |
| 1471 |
# redefined by the user in one of the bases. |
| 1472 |
actual = do_autodoc( |
| 1473 |
'class', fmt.target, options=options | {'inherited-members': None} |
| 1474 |
) |
| 1475 |
assert actual == [ |
| 1476 |
*fmt.preamble_lookup('this is enum class'), |
| 1477 |
*fmt.method( |
| 1478 |
'say_goodbye', 'a classmethod says good-bye to you.', 'classmethod' |
| 1479 |
), |
| 1480 |
*fmt.method('say_hello', 'a method says hello to you.'), |
| 1481 |
*fmt.member('val1', 12, 'doc for val1'), |
| 1482 |
*fmt.member('val2', 23, 'doc for val2'), |
| 1483 |
*fmt.member('val3', 34, 'doc for val3'), |
| 1484 |
*fmt.member('val4', 34, ''), # val4 is alias of val3 |
| 1485 |
] |
| 1486 |
|
| 1487 |
# checks for an attribute of EnumCls |
| 1488 |
actual = do_autodoc('attribute', fmt.subtarget('val1')) |
| 1489 |
assert actual == fmt.member('val1', 12, 'doc for val1', indent=0) |
| 1490 |
|
| 1491 |
|
| 1492 |
def test_enum_class_with_data_type(autodoc_enum_options): |
| 1493 |
fmt = _EnumFormatter('EnumClassWithDataType') |
| 1494 |
|
| 1495 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1496 |
assert actual == [ |
| 1497 |
*fmt.preamble_lookup('this is enum class'), |
| 1498 |
*fmt.method('say_goodbye', 'docstring', 'classmethod'), |
| 1499 |
*fmt.method('say_hello', 'docstring'), |
| 1500 |
*fmt.member('x', 'x', ''), |
| 1501 |
] |
| 1502 |
|
| 1503 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1504 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1505 |
assert actual == [ |
| 1506 |
*fmt.preamble_lookup('this is enum class'), |
| 1507 |
*fmt.entry('dtype', 'docstring', role='property'), |
| 1508 |
*fmt.method('isupper', 'inherited'), |
| 1509 |
*fmt.method('say_goodbye', 'docstring', 'classmethod'), |
| 1510 |
*fmt.method('say_hello', 'docstring'), |
| 1511 |
*fmt.member('x', 'x', ''), |
| 1512 |
] |
| 1513 |
|
| 1514 |
|
| 1515 |
def test_enum_class_with_mixin_type(autodoc_enum_options): |
| 1516 |
fmt = _EnumFormatter('EnumClassWithMixinType') |
| 1517 |
|
| 1518 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1519 |
assert actual == [ |
| 1520 |
*fmt.preamble_lookup('this is enum class'), |
| 1521 |
*fmt.method('say_goodbye', 'docstring', 'classmethod'), |
| 1522 |
*fmt.method('say_hello', 'docstring'), |
| 1523 |
*fmt.member('x', 'X', ''), |
| 1524 |
] |
| 1525 |
|
| 1526 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1527 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1528 |
assert actual == [ |
| 1529 |
*fmt.preamble_lookup('this is enum class'), |
| 1530 |
*fmt.method('say_goodbye', 'docstring', 'classmethod'), |
| 1531 |
*fmt.method('say_hello', 'docstring'), |
| 1532 |
*fmt.entry('value', 'uppercased', role='property'), |
| 1533 |
*fmt.member('x', 'X', ''), |
| 1534 |
] |
| 1535 |
|
| 1536 |
|
| 1537 |
def test_enum_class_with_mixin_type_and_inheritence(autodoc_enum_options): |
| 1538 |
fmt = _EnumFormatter('EnumClassWithMixinTypeInherit') |
| 1539 |
|
| 1540 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1541 |
assert actual == [ |
| 1542 |
*fmt.preamble_lookup('this is enum class'), |
| 1543 |
*fmt.member('x', 'X', ''), |
| 1544 |
] |
| 1545 |
|
| 1546 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1547 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1548 |
assert actual == [ |
| 1549 |
*fmt.preamble_lookup('this is enum class'), |
| 1550 |
*fmt.method('say_goodbye', 'inherited', 'classmethod'), |
| 1551 |
*fmt.method('say_hello', 'inherited'), |
| 1552 |
*fmt.entry('value', 'uppercased', role='property'), |
| 1553 |
*fmt.member('x', 'X', ''), |
| 1554 |
] |
| 1555 |
|
| 1556 |
|
| 1557 |
def test_enum_class_with_mixin_enum_type(autodoc_enum_options): |
| 1558 |
fmt = _EnumFormatter('EnumClassWithMixinEnumType') |
| 1559 |
|
| 1560 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1561 |
assert actual == [ |
| 1562 |
*fmt.preamble_lookup('this is enum class'), |
| 1563 |
# override() is overridden at the class level so it should be rendered |
| 1564 |
*fmt.method('override', 'overridden'), |
| 1565 |
# say_goodbye() and say_hello() are not rendered since they are inherited |
| 1566 |
*fmt.member('x', 'x', ''), |
| 1567 |
] |
| 1568 |
|
| 1569 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1570 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1571 |
assert actual == [ |
| 1572 |
*fmt.preamble_lookup('this is enum class'), |
| 1573 |
*fmt.method('override', 'overridden'), |
| 1574 |
*fmt.method('say_goodbye', 'inherited', 'classmethod'), |
| 1575 |
*fmt.method('say_hello', 'inherited'), |
| 1576 |
*fmt.member('x', 'x', ''), |
| 1577 |
] |
| 1578 |
|
| 1579 |
|
| 1580 |
def test_enum_class_with_mixin_and_data_type(autodoc_enum_options): |
| 1581 |
fmt = _EnumFormatter('EnumClassWithMixinAndDataType') |
| 1582 |
|
| 1583 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1584 |
assert actual == [ |
| 1585 |
*fmt.preamble_lookup('this is enum class'), |
| 1586 |
*fmt.method('isupper', 'overridden'), |
| 1587 |
*fmt.method('say_goodbye', 'overridden', 'classmethod'), |
| 1588 |
*fmt.method('say_hello', 'overridden'), |
| 1589 |
*fmt.member('x', 'X', ''), |
| 1590 |
] |
| 1591 |
|
| 1592 |
# add the special member __str__ (but not the inherited members) |
| 1593 |
options = autodoc_enum_options | {'special-members': '__str__'} |
| 1594 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1595 |
assert actual == [ |
| 1596 |
*fmt.preamble_lookup('this is enum class'), |
| 1597 |
*fmt.method('__str__', 'overridden'), |
| 1598 |
*fmt.method('isupper', 'overridden'), |
| 1599 |
*fmt.method('say_goodbye', 'overridden', 'classmethod'), |
| 1600 |
*fmt.method('say_hello', 'overridden'), |
| 1601 |
*fmt.member('x', 'X', ''), |
| 1602 |
] |
| 1603 |
|
| 1604 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1605 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1606 |
assert actual == [ |
| 1607 |
*fmt.preamble_lookup('this is enum class'), |
| 1608 |
*fmt.entry('dtype', 'docstring', role='property'), |
| 1609 |
*fmt.method('isupper', 'overridden'), |
| 1610 |
*fmt.method('say_goodbye', 'overridden', 'classmethod'), |
| 1611 |
*fmt.method('say_hello', 'overridden'), |
| 1612 |
*fmt.entry('value', 'uppercased', role='property'), |
| 1613 |
*fmt.member('x', 'X', ''), |
| 1614 |
] |
| 1615 |
|
| 1616 |
|
| 1617 |
def test_enum_with_parent_enum(autodoc_enum_options): |
| 1618 |
fmt = _EnumFormatter('EnumClassWithParentEnum') |
| 1619 |
|
| 1620 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1621 |
assert actual == [ |
| 1622 |
*fmt.preamble_lookup('this is enum class'), |
| 1623 |
*fmt.method('isupper', 'overridden'), |
| 1624 |
*fmt.member('x', 'X', ''), |
| 1625 |
] |
| 1626 |
|
| 1627 |
# add the special member __str__ (but not the inherited members) |
| 1628 |
options = autodoc_enum_options | {'special-members': '__str__'} |
| 1629 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1630 |
assert actual == [ |
| 1631 |
*fmt.preamble_lookup('this is enum class'), |
| 1632 |
*fmt.method('__str__', 'overridden'), |
| 1633 |
*fmt.method('isupper', 'overridden'), |
| 1634 |
*fmt.member('x', 'X', ''), |
| 1635 |
] |
| 1636 |
|
| 1637 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1638 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1639 |
assert actual == [ |
| 1640 |
*fmt.preamble_lookup('this is enum class'), |
| 1641 |
*fmt.entry('dtype', 'docstring', role='property'), |
| 1642 |
*fmt.method('isupper', 'overridden'), |
| 1643 |
*fmt.method('override', 'inherited'), |
| 1644 |
*fmt.method('say_goodbye', 'inherited', 'classmethod'), |
| 1645 |
*fmt.method('say_hello', 'inherited'), |
| 1646 |
*fmt.entry('value', 'uppercased', role='property'), |
| 1647 |
*fmt.member('x', 'X', ''), |
| 1648 |
] |
| 1649 |
|
| 1650 |
|
| 1651 |
def test_enum_sunder_method(autodoc_enum_options): |
| 1652 |
PRIVATE = {'private-members': None} # sunder methods are recognized as private |
| 1653 |
|
| 1654 |
fmt = _EnumFormatter('EnumSunderMissingInNonEnumMixin') |
| 1655 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1656 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1657 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options | PRIVATE) |
| 1658 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1659 |
|
| 1660 |
fmt = _EnumFormatter('EnumSunderMissingInEnumMixin') |
| 1661 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1662 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1663 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options | PRIVATE) |
| 1664 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1665 |
|
| 1666 |
fmt = _EnumFormatter('EnumSunderMissingInDataType') |
| 1667 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1668 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1669 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options | PRIVATE) |
| 1670 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1671 |
|
| 1672 |
fmt = _EnumFormatter('EnumSunderMissingInClass') |
| 1673 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1674 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1675 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options | PRIVATE) |
| 1676 |
assert actual == [ |
| 1677 |
*fmt.preamble_constructor('this is enum class'), |
| 1678 |
*fmt.method('_missing_', 'docstring', 'classmethod', args='(value)'), |
| 1679 |
] |
| 1680 |
|
| 1681 |
|
| 1682 |
def test_enum_inherited_sunder_method(autodoc_enum_options): |
| 1683 |
options = autodoc_enum_options | { |
| 1684 |
'private-members': None, |
| 1685 |
'inherited-members': None, |
| 1686 |
} |
| 1687 |
|
| 1688 |
fmt = _EnumFormatter('EnumSunderMissingInNonEnumMixin') |
| 1689 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1690 |
assert actual == [ |
| 1691 |
*fmt.preamble_constructor('this is enum class'), |
| 1692 |
*fmt.method('_missing_', 'inherited', 'classmethod', args='(value)'), |
| 1693 |
] |
| 1694 |
|
| 1695 |
fmt = _EnumFormatter('EnumSunderMissingInEnumMixin') |
| 1696 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1697 |
assert actual == [ |
| 1698 |
*fmt.preamble_constructor('this is enum class'), |
| 1699 |
*fmt.method('_missing_', 'inherited', 'classmethod', args='(value)'), |
| 1700 |
] |
| 1701 |
|
| 1702 |
fmt = _EnumFormatter('EnumSunderMissingInDataType') |
| 1703 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1704 |
assert actual == [ |
| 1705 |
*fmt.preamble_constructor('this is enum class'), |
| 1706 |
*fmt.method('_missing_', 'inherited', 'classmethod', args='(value)'), |
| 1707 |
*fmt.entry('dtype', 'docstring', role='property'), |
| 1708 |
*fmt.method('isupper', 'inherited'), |
| 1709 |
] |
| 1710 |
|
| 1711 |
fmt = _EnumFormatter('EnumSunderMissingInClass') |
| 1712 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1713 |
assert actual == [ |
| 1714 |
*fmt.preamble_constructor('this is enum class'), |
| 1715 |
*fmt.method('_missing_', 'docstring', 'classmethod', args='(value)'), |
| 1716 |
] |
| 1717 |
|
| 1718 |
|
| 1719 |
def test_enum_custom_name_property(autodoc_enum_options): |
| 1720 |
fmt = _EnumFormatter('EnumNamePropertyInNonEnumMixin') |
| 1721 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1722 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1723 |
|
| 1724 |
fmt = _EnumFormatter('EnumNamePropertyInEnumMixin') |
| 1725 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1726 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1727 |
|
| 1728 |
fmt = _EnumFormatter('EnumNamePropertyInDataType') |
| 1729 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1730 |
assert actual == [*fmt.preamble_constructor('this is enum class')] |
| 1731 |
|
| 1732 |
fmt = _EnumFormatter('EnumNamePropertyInClass') |
| 1733 |
actual = do_autodoc('class', fmt.target, options=autodoc_enum_options) |
| 1734 |
assert actual == [ |
| 1735 |
*fmt.preamble_constructor('this is enum class'), |
| 1736 |
*fmt.entry('name', 'docstring', role='property'), |
| 1737 |
] |
| 1738 |
|
| 1739 |
|
| 1740 |
def test_enum_inherited_custom_name_property(autodoc_enum_options): |
| 1741 |
options = autodoc_enum_options | {'inherited-members': None} |
| 1742 |
|
| 1743 |
fmt = _EnumFormatter('EnumNamePropertyInNonEnumMixin') |
| 1744 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1745 |
assert actual == [ |
| 1746 |
*fmt.preamble_constructor('this is enum class'), |
| 1747 |
*fmt.entry('name', 'inherited', role='property'), |
| 1748 |
] |
| 1749 |
|
| 1750 |
fmt = _EnumFormatter('EnumNamePropertyInEnumMixin') |
| 1751 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1752 |
assert actual == [ |
| 1753 |
*fmt.preamble_constructor('this is enum class'), |
| 1754 |
*fmt.entry('name', 'inherited', role='property'), |
| 1755 |
] |
| 1756 |
|
| 1757 |
fmt = _EnumFormatter('EnumNamePropertyInDataType') |
| 1758 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1759 |
assert actual == [ |
| 1760 |
*fmt.preamble_constructor('this is enum class'), |
| 1761 |
*fmt.entry('dtype', 'docstring', role='property'), |
| 1762 |
*fmt.method('isupper', 'inherited'), |
| 1763 |
*fmt.entry('name', 'inherited', role='property'), |
| 1764 |
] |
| 1765 |
|
| 1766 |
fmt = _EnumFormatter('EnumNamePropertyInClass') |
| 1767 |
actual = do_autodoc('class', fmt.target, options=options) |
| 1768 |
assert actual == [ |
| 1769 |
*fmt.preamble_constructor('this is enum class'), |
| 1770 |
*fmt.entry('name', 'docstring', role='property'), |
| 1771 |
] |
| 1772 |
|
| 1773 |
|
| 1774 |
def test_descriptor_class() -> None: |
| 1775 |
options = {'members': 'CustomDataDescriptor,CustomDataDescriptor2'} |
| 1776 |
actual = do_autodoc('module', 'target.descriptor', options=options) |
| 1777 |
assert actual == [ |
| 1778 |
'', |
| 1779 |
'.. py:module:: target.descriptor', |
| 1780 |
'', |
| 1781 |
'', |
| 1782 |
'.. py:class:: CustomDataDescriptor(doc)', |
| 1783 |
' :module: target.descriptor', |
| 1784 |
'', |
| 1785 |
' Descriptor class docstring.', |
| 1786 |
'', |
| 1787 |
'', |
| 1788 |
' .. py:method:: CustomDataDescriptor.meth()', |
| 1789 |
' :module: target.descriptor', |
| 1790 |
'', |
| 1791 |
' Function.', |
| 1792 |
'', |
| 1793 |
'', |
| 1794 |
'.. py:class:: CustomDataDescriptor2(doc)', |
| 1795 |
' :module: target.descriptor', |
| 1796 |
'', |
| 1797 |
' Descriptor class with custom metaclass docstring.', |
| 1798 |
'', |
| 1799 |
] |
| 1800 |
|
| 1801 |
|
| 1802 |
def test_automethod_for_builtin() -> None: |
| 1803 |
actual = do_autodoc('method', 'builtins.int.__add__') |
| 1804 |
assert actual == [ |
| 1805 |
'', |
| 1806 |
'.. py:method:: int.__add__(value, /)', |
| 1807 |
' :module: builtins', |
| 1808 |
'', |
| 1809 |
' Return self+value.', |
| 1810 |
'', |
| 1811 |
] |
| 1812 |
|
| 1813 |
|
| 1814 |
def test_automethod_for_decorated() -> None: |
| 1815 |
actual = do_autodoc('method', 'target.decorator.Bar.meth') |
| 1816 |
assert actual == [ |
| 1817 |
'', |
| 1818 |
'.. py:method:: Bar.meth(name=None, age=None)', |
| 1819 |
' :module: target.decorator', |
| 1820 |
'', |
| 1821 |
] |
| 1822 |
|
| 1823 |
|
| 1824 |
def test_abstractmethods() -> None: |
| 1825 |
options = { |
| 1826 |
'members': None, |
| 1827 |
'undoc-members': None, |
| 1828 |
} |
| 1829 |
actual = do_autodoc('module', 'target.abstractmethods', options=options) |
| 1830 |
assert actual == [ |
| 1831 |
'', |
| 1832 |
'.. py:module:: target.abstractmethods', |
| 1833 |
'', |
| 1834 |
'', |
| 1835 |
'.. py:class:: Base()', |
| 1836 |
' :module: target.abstractmethods', |
| 1837 |
'', |
| 1838 |
'', |
| 1839 |
' .. py:method:: Base.abstractmeth()', |
| 1840 |
' :module: target.abstractmethods', |
| 1841 |
' :abstractmethod:', |
| 1842 |
'', |
| 1843 |
'', |
| 1844 |
' .. py:method:: Base.classmeth()', |
| 1845 |
' :module: target.abstractmethods', |
| 1846 |
' :abstractmethod:', |
| 1847 |
' :classmethod:', |
| 1848 |
'', |
| 1849 |
'', |
| 1850 |
' .. py:method:: Base.coroutinemeth()', |
| 1851 |
' :module: target.abstractmethods', |
| 1852 |
' :abstractmethod:', |
| 1853 |
' :async:', |
| 1854 |
'', |
| 1855 |
'', |
| 1856 |
' .. py:method:: Base.meth()', |
| 1857 |
' :module: target.abstractmethods', |
| 1858 |
'', |
| 1859 |
'', |
| 1860 |
' .. py:property:: Base.prop', |
| 1861 |
' :module: target.abstractmethods', |
| 1862 |
' :abstractmethod:', |
| 1863 |
'', |
| 1864 |
'', |
| 1865 |
' .. py:method:: Base.staticmeth()', |
| 1866 |
' :module: target.abstractmethods', |
| 1867 |
' :abstractmethod:', |
| 1868 |
' :staticmethod:', |
| 1869 |
'', |
| 1870 |
] |
| 1871 |
|
| 1872 |
|
| 1873 |
def test_partialfunction() -> None: |
| 1874 |
options = {'members': None} |
| 1875 |
actual = do_autodoc('module', 'target.partialfunction', options=options) |
| 1876 |
assert actual == [ |
| 1877 |
'', |
| 1878 |
'.. py:module:: target.partialfunction', |
| 1879 |
'', |
| 1880 |
'', |
| 1881 |
'.. py:function:: func1(a, b, c)', |
| 1882 |
' :module: target.partialfunction', |
| 1883 |
'', |
| 1884 |
' docstring of func1', |
| 1885 |
'', |
| 1886 |
'', |
| 1887 |
'.. py:function:: func2(b, c)', |
| 1888 |
' :module: target.partialfunction', |
| 1889 |
'', |
| 1890 |
' docstring of func1', |
| 1891 |
'', |
| 1892 |
'', |
| 1893 |
'.. py:function:: func3(c)', |
| 1894 |
' :module: target.partialfunction', |
| 1895 |
'', |
| 1896 |
' docstring of func3', |
| 1897 |
'', |
| 1898 |
'', |
| 1899 |
'.. py:function:: func4()', |
| 1900 |
' :module: target.partialfunction', |
| 1901 |
'', |
| 1902 |
' docstring of func3', |
| 1903 |
'', |
| 1904 |
] |
| 1905 |
|
| 1906 |
|
| 1907 |
def test_imported_partialfunction_should_not_shown_without_imported_members() -> None: |
| 1908 |
options = {'members': None} |
| 1909 |
actual = do_autodoc('module', 'target.imported_members', options=options) |
| 1910 |
assert actual == [ |
| 1911 |
'', |
| 1912 |
'.. py:module:: target.imported_members', |
| 1913 |
'', |
| 1914 |
] |
| 1915 |
|
| 1916 |
|
| 1917 |
def test_bound_method() -> None: |
| 1918 |
options = {'members': None} |
| 1919 |
actual = do_autodoc('module', 'target.bound_method', options=options) |
| 1920 |
assert actual == [ |
| 1921 |
'', |
| 1922 |
'.. py:module:: target.bound_method', |
| 1923 |
'', |
| 1924 |
'', |
| 1925 |
'.. py:function:: bound_method()', |
| 1926 |
' :module: target.bound_method', |
| 1927 |
'', |
| 1928 |
' Method docstring', |
| 1929 |
'', |
| 1930 |
] |
| 1931 |
|
| 1932 |
|
| 1933 |
def test_partialmethod() -> None: |
| 1934 |
expected = [ |
| 1935 |
'', |
| 1936 |
'.. py:class:: Cell()', |
| 1937 |
' :module: target.partialmethod', |
| 1938 |
'', |
| 1939 |
' An example for partialmethod.', |
| 1940 |
'', |
| 1941 |
' refs: https://docs.python.org/3/library/functools.html#functools.partialmethod', |
| 1942 |
'', |
| 1943 |
'', |
| 1944 |
' .. py:method:: Cell.set_alive()', |
| 1945 |
' :module: target.partialmethod', |
| 1946 |
'', |
| 1947 |
' Make a cell alive.', |
| 1948 |
'', |
| 1949 |
'', |
| 1950 |
' .. py:method:: Cell.set_state(state)', |
| 1951 |
' :module: target.partialmethod', |
| 1952 |
'', |
| 1953 |
' Update state of cell to *state*.', |
| 1954 |
'', |
| 1955 |
] |
| 1956 |
|
| 1957 |
options = {'members': None} |
| 1958 |
actual = do_autodoc('class', 'target.partialmethod.Cell', options=options) |
| 1959 |
assert actual == expected |
| 1960 |
|
| 1961 |
|
| 1962 |
def test_partialmethod_undoc_members() -> None: |
| 1963 |
expected = [ |
| 1964 |
'', |
| 1965 |
'.. py:class:: Cell()', |
| 1966 |
' :module: target.partialmethod', |
| 1967 |
'', |
| 1968 |
' An example for partialmethod.', |
| 1969 |
'', |
| 1970 |
' refs: https://docs.python.org/3/library/functools.html#functools.partialmethod', |
| 1971 |
'', |
| 1972 |
'', |
| 1973 |
' .. py:method:: Cell.set_alive()', |
| 1974 |
' :module: target.partialmethod', |
| 1975 |
'', |
| 1976 |
' Make a cell alive.', |
| 1977 |
'', |
| 1978 |
'', |
| 1979 |
' .. py:method:: Cell.set_dead()', |
| 1980 |
' :module: target.partialmethod', |
| 1981 |
'', |
| 1982 |
'', |
| 1983 |
' .. py:method:: Cell.set_state(state)', |
| 1984 |
' :module: target.partialmethod', |
| 1985 |
'', |
| 1986 |
' Update state of cell to *state*.', |
| 1987 |
'', |
| 1988 |
] |
| 1989 |
|
| 1990 |
options = { |
| 1991 |
'members': None, |
| 1992 |
'undoc-members': None, |
| 1993 |
} |
| 1994 |
actual = do_autodoc('class', 'target.partialmethod.Cell', options=options) |
| 1995 |
assert actual == expected |
| 1996 |
|
| 1997 |
|
| 1998 |
def test_autodoc_typed_instance_variables() -> None: |
| 1999 |
options = { |
| 2000 |
'members': None, |
| 2001 |
'undoc-members': None, |
| 2002 |
} |
| 2003 |
# First compute autodoc of a `Derived` member to verify that it |
| 2004 |
# doesn't result in inherited members in |
| 2005 |
# `Derived.__annotations__`. |
| 2006 |
# https://github.com/sphinx-doc/sphinx/issues/13934 |
| 2007 |
do_autodoc('attribute', 'target.typed_vars.Derived.attr2') |
| 2008 |
actual = do_autodoc('module', 'target.typed_vars', options=options) |
| 2009 |
assert actual == [ |
| 2010 |
'', |
| 2011 |
'.. py:module:: target.typed_vars', |
| 2012 |
'', |
| 2013 |
'', |
| 2014 |
'.. py:attribute:: Alias', |
| 2015 |
' :module: target.typed_vars', |
| 2016 |
'', |
| 2017 |
' alias of :py:class:`~target.typed_vars.Derived`', |
| 2018 |
'', |
| 2019 |
'.. py:class:: Class()', |
| 2020 |
' :module: target.typed_vars', |
| 2021 |
'', |
| 2022 |
'', |
| 2023 |
' .. py:attribute:: Class.attr1', |
| 2024 |
' :module: target.typed_vars', |
| 2025 |
' :type: int', |
| 2026 |
' :value: 0', |
| 2027 |
'', |
| 2028 |
'', |
| 2029 |
' .. py:attribute:: Class.attr2', |
| 2030 |
' :module: target.typed_vars', |
| 2031 |
' :type: int', |
| 2032 |
'', |
| 2033 |
'', |
| 2034 |
' .. py:attribute:: Class.attr3', |
| 2035 |
' :module: target.typed_vars', |
| 2036 |
' :type: int', |
| 2037 |
' :value: 0', |
| 2038 |
'', |
| 2039 |
'', |
| 2040 |
' .. py:attribute:: Class.attr4', |
| 2041 |
' :module: target.typed_vars', |
| 2042 |
' :type: int', |
| 2043 |
'', |
| 2044 |
' attr4', |
| 2045 |
'', |
| 2046 |
'', |
| 2047 |
' .. py:attribute:: Class.attr5', |
| 2048 |
' :module: target.typed_vars', |
| 2049 |
' :type: int', |
| 2050 |
'', |
| 2051 |
' attr5', |
| 2052 |
'', |
| 2053 |
'', |
| 2054 |
' .. py:attribute:: Class.attr6', |
| 2055 |
' :module: target.typed_vars', |
| 2056 |
' :type: int', |
| 2057 |
'', |
| 2058 |
' attr6', |
| 2059 |
'', |
| 2060 |
'', |
| 2061 |
' .. py:attribute:: Class.descr4', |
| 2062 |
' :module: target.typed_vars', |
| 2063 |
' :type: int', |
| 2064 |
'', |
| 2065 |
' This is descr4', |
| 2066 |
'', |
| 2067 |
'', |
| 2068 |
'.. py:class:: Derived()', |
| 2069 |
' :module: target.typed_vars', |
| 2070 |
'', |
| 2071 |
'', |
| 2072 |
' .. py:attribute:: Derived.attr7', |
| 2073 |
' :module: target.typed_vars', |
| 2074 |
' :type: int', |
| 2075 |
'', |
| 2076 |
'', |
| 2077 |
'.. py:data:: attr1', |
| 2078 |
' :module: target.typed_vars', |
| 2079 |
' :type: str', |
| 2080 |
" :value: ''", |
| 2081 |
'', |
| 2082 |
' attr1', |
| 2083 |
'', |
| 2084 |
'', |
| 2085 |
'.. py:data:: attr2', |
| 2086 |
' :module: target.typed_vars', |
| 2087 |
' :type: str', |
| 2088 |
'', |
| 2089 |
' attr2', |
| 2090 |
'', |
| 2091 |
'', |
| 2092 |
'.. py:data:: attr3', |
| 2093 |
' :module: target.typed_vars', |
| 2094 |
' :type: str', |
| 2095 |
" :value: ''", |
| 2096 |
'', |
| 2097 |
' attr3', |
| 2098 |
'', |
| 2099 |
] |
| 2100 |
|
| 2101 |
|
| 2102 |
def test_autodoc_typed_inherited_instance_variables() -> None: |
| 2103 |
options = { |
| 2104 |
'members': None, |
| 2105 |
'undoc-members': None, |
| 2106 |
'inherited-members': None, |
| 2107 |
} |
| 2108 |
actual = do_autodoc('class', 'target.typed_vars.Derived', options=options) |
| 2109 |
assert actual == [ |
| 2110 |
'', |
| 2111 |
'.. py:class:: Derived()', |
| 2112 |
' :module: target.typed_vars', |
| 2113 |
'', |
| 2114 |
'', |
| 2115 |
' .. py:attribute:: Derived.attr1', |
| 2116 |
' :module: target.typed_vars', |
| 2117 |
' :type: int', |
| 2118 |
' :value: 0', |
| 2119 |
'', |
| 2120 |
'', |
| 2121 |
' .. py:attribute:: Derived.attr2', |
| 2122 |
' :module: target.typed_vars', |
| 2123 |
' :type: int', |
| 2124 |
'', |
| 2125 |
'', |
| 2126 |
' .. py:attribute:: Derived.attr3', |
| 2127 |
' :module: target.typed_vars', |
| 2128 |
' :type: int', |
| 2129 |
' :value: 0', |
| 2130 |
'', |
| 2131 |
'', |
| 2132 |
' .. py:attribute:: Derived.attr4', |
| 2133 |
' :module: target.typed_vars', |
| 2134 |
' :type: int', |
| 2135 |
'', |
| 2136 |
' attr4', |
| 2137 |
'', |
| 2138 |
'', |
| 2139 |
' .. py:attribute:: Derived.attr5', |
| 2140 |
' :module: target.typed_vars', |
| 2141 |
' :type: int', |
| 2142 |
'', |
| 2143 |
' attr5', |
| 2144 |
'', |
| 2145 |
'', |
| 2146 |
' .. py:attribute:: Derived.attr6', |
| 2147 |
' :module: target.typed_vars', |
| 2148 |
' :type: int', |
| 2149 |
'', |
| 2150 |
' attr6', |
| 2151 |
'', |
| 2152 |
'', |
| 2153 |
' .. py:attribute:: Derived.attr7', |
| 2154 |
' :module: target.typed_vars', |
| 2155 |
' :type: int', |
| 2156 |
'', |
| 2157 |
'', |
| 2158 |
' .. py:attribute:: Derived.descr4', |
| 2159 |
' :module: target.typed_vars', |
| 2160 |
' :type: int', |
| 2161 |
'', |
| 2162 |
] |
| 2163 |
|
| 2164 |
|
| 2165 |
def test_autodoc_GenericAlias() -> None: |
| 2166 |
options = { |
| 2167 |
'members': None, |
| 2168 |
'undoc-members': None, |
| 2169 |
} |
| 2170 |
actual = do_autodoc('module', 'target.genericalias', options=options) |
| 2171 |
assert actual == [ |
| 2172 |
'', |
| 2173 |
'.. py:module:: target.genericalias', |
| 2174 |
'', |
| 2175 |
'', |
| 2176 |
'.. py:class:: Class()', |
| 2177 |
' :module: target.genericalias', |
| 2178 |
'', |
| 2179 |
'', |
| 2180 |
' .. py:attribute:: Class.T', |
| 2181 |
' :module: target.genericalias', |
| 2182 |
'', |
| 2183 |
' A list of int', |
| 2184 |
'', |
| 2185 |
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]', |
| 2186 |
'', |
| 2187 |
'', |
| 2188 |
'.. py:data:: L', |
| 2189 |
' :module: target.genericalias', |
| 2190 |
'', |
| 2191 |
' A list of Class', |
| 2192 |
'', |
| 2193 |
' alias of :py:class:`~typing.List`\\ ' |
| 2194 |
'[:py:class:`~target.genericalias.Class`]', |
| 2195 |
'', |
| 2196 |
'', |
| 2197 |
'.. py:data:: T', |
| 2198 |
' :module: target.genericalias', |
| 2199 |
'', |
| 2200 |
' A list of int', |
| 2201 |
'', |
| 2202 |
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]', |
| 2203 |
'', |
| 2204 |
] |
| 2205 |
|
| 2206 |
|
| 2207 |
def test_autodoc_pep695_type_alias() -> None: |
| 2208 |
config = _AutodocConfig( |
| 2209 |
autodoc_type_aliases={ |
| 2210 |
'buffer_like': 'buffer_like', |
| 2211 |
'pathlike': 'pathlike', |
| 2212 |
'Handler': 'Handler', |
| 2213 |
} |
| 2214 |
) |
| 2215 |
options = { |
| 2216 |
'members': None, |
| 2217 |
'undoc-members': None, |
| 2218 |
} |
| 2219 |
actual = do_autodoc('module', 'target.pep695', config=config, options=options) |
| 2220 |
assert actual == [ |
| 2221 |
'', |
| 2222 |
'.. py:module:: target.pep695', |
| 2223 |
'', |
| 2224 |
'', |
| 2225 |
'.. py:class:: Bar', |
| 2226 |
' :module: target.pep695', |
| 2227 |
'', |
| 2228 |
' This is newtype of Pep695Alias.', |
| 2229 |
'', |
| 2230 |
' alias of :py:type:`~target.pep695.Pep695Alias`', |
| 2231 |
'', |
| 2232 |
'', |
| 2233 |
'.. py:class:: Foo()', |
| 2234 |
' :module: target.pep695', |
| 2235 |
'', |
| 2236 |
' This is class Foo.', |
| 2237 |
'', |
| 2238 |
'', |
| 2239 |
'.. py:data:: Handler', |
| 2240 |
' :module: target.pep695', |
| 2241 |
'', |
| 2242 |
' A generic type alias', |
| 2243 |
'', |
| 2244 |
' alias of :py:class:`type`\\ [:py:class:`Exception`]', |
| 2245 |
'', |
| 2246 |
'', |
| 2247 |
'.. py:type:: HandlerTypeAliasType', |
| 2248 |
' :module: target.pep695', |
| 2249 |
' :canonical: type[Exception]', |
| 2250 |
'', |
| 2251 |
' This is an explicitly constructed generic alias typing.TypeAlias.', |
| 2252 |
'', |
| 2253 |
'', |
| 2254 |
'.. py:type:: Pep695Alias', |
| 2255 |
' :module: target.pep695', |
| 2256 |
' :canonical: ~target.pep695.Foo', |
| 2257 |
'', |
| 2258 |
' This is PEP695 type alias.', |
| 2259 |
'', |
| 2260 |
'', |
| 2261 |
'.. py:type:: Pep695AliasC', |
| 2262 |
' :module: target.pep695', |
| 2263 |
' :canonical: dict[str, ~target.pep695.Foo]', |
| 2264 |
'', |
| 2265 |
' This is PEP695 complex type alias with doc comment.', |
| 2266 |
'', |
| 2267 |
'', |
| 2268 |
'.. py:type:: Pep695AliasOfAlias', |
| 2269 |
' :module: target.pep695', |
| 2270 |
' :canonical: ~target.pep695.Pep695AliasC', |
| 2271 |
'', |
| 2272 |
' This is PEP695 type alias of PEP695 alias.', |
| 2273 |
'', |
| 2274 |
'', |
| 2275 |
# Undocumented alias should not inherit any documentation |
| 2276 |
'.. py:type:: Pep695AliasUndocumented', |
| 2277 |
' :module: target.pep695', |
| 2278 |
' :canonical: ~target.pep695.Foo', |
| 2279 |
'', |
| 2280 |
'', |
| 2281 |
'.. py:type:: Pep695AliasUnion', |
| 2282 |
' :module: target.pep695', |
| 2283 |
' :canonical: str | int', |
| 2284 |
'', |
| 2285 |
' This is PEP695 type alias for union.', |
| 2286 |
'', |
| 2287 |
'', |
| 2288 |
'.. py:type:: TypeAliasTypeExplicit', |
| 2289 |
' :module: target.pep695', |
| 2290 |
' :canonical: ~target.pep695.Foo', |
| 2291 |
'', |
| 2292 |
' This is an explicitly constructed typing.TypeAlias.', |
| 2293 |
'', |
| 2294 |
'', |
| 2295 |
'.. py:type:: TypeAliasTypeExtension', |
| 2296 |
' :module: target.pep695', |
| 2297 |
' :canonical: ~target.pep695.Foo', |
| 2298 |
'', |
| 2299 |
' This is an explicitly constructed typing_extensions.TypeAlias.', |
| 2300 |
'', |
| 2301 |
'', |
| 2302 |
'.. py:function:: buffer_len(data: buffer_like) -> int', |
| 2303 |
' :module: target.pep695', |
| 2304 |
'', |
| 2305 |
' Return length of a buffer-like object.', |
| 2306 |
'', |
| 2307 |
' Tests Union type alias cross-reference resolution.', |
| 2308 |
'', |
| 2309 |
'', |
| 2310 |
'.. py:data:: buffer_like', |
| 2311 |
' :module: target.pep695', |
| 2312 |
' :value: bytes | bytearray | memoryview', |
| 2313 |
'', |
| 2314 |
' Some buffer-like object', |
| 2315 |
'', |
| 2316 |
'', |
| 2317 |
'.. py:data:: pathlike', |
| 2318 |
' :module: target.pep695', |
| 2319 |
f' :value: str | {pathlib.Path.__module__}.Path', |
| 2320 |
'', |
| 2321 |
' Any type of path', |
| 2322 |
'', |
| 2323 |
'', |
| 2324 |
'.. py:function:: process_error(handler: Handler, other: ~target.pep695.HandlerTypeAliasType) -> str', |
| 2325 |
' :module: target.pep695', |
| 2326 |
'', |
| 2327 |
' Process an error with a custom handler type.', |
| 2328 |
'', |
| 2329 |
' Tests generic type alias cross-reference resolution.', |
| 2330 |
'', |
| 2331 |
'', |
| 2332 |
'.. py:function:: read_file(path: pathlike) -> bytes', |
| 2333 |
' :module: target.pep695', |
| 2334 |
'', |
| 2335 |
' Read a file and return its contents.', |
| 2336 |
'', |
| 2337 |
' Tests Union type alias cross-reference resolution.', |
| 2338 |
'', |
| 2339 |
'', |
| 2340 |
'.. py:function:: ret_pep695(a: ~target.pep695.Pep695Alias) -> ~target.pep695.Pep695Alias', |
| 2341 |
' :module: target.pep695', |
| 2342 |
'', |
| 2343 |
' This fn accepts and returns PEP695 alias.', |
| 2344 |
'', |
| 2345 |
] |
| 2346 |
|
| 2347 |
|
| 2348 |
def test_autodoc_TypeVar() -> None: |
| 2349 |
options = { |
| 2350 |
'members': None, |
| 2351 |
'undoc-members': None, |
| 2352 |
} |
| 2353 |
actual = do_autodoc('module', 'target.typevar', options=options) |
| 2354 |
assert actual == [ |
| 2355 |
'', |
| 2356 |
'.. py:module:: target.typevar', |
| 2357 |
'', |
| 2358 |
'', |
| 2359 |
'.. py:class:: Class()', |
| 2360 |
' :module: target.typevar', |
| 2361 |
'', |
| 2362 |
'', |
| 2363 |
' .. py:class:: Class.T1', |
| 2364 |
' :module: target.typevar', |
| 2365 |
'', |
| 2366 |
' T1', |
| 2367 |
'', |
| 2368 |
" alias of TypeVar('T1')", |
| 2369 |
'', |
| 2370 |
'', |
| 2371 |
' .. py:class:: Class.T6', |
| 2372 |
' :module: target.typevar', |
| 2373 |
'', |
| 2374 |
' T6', |
| 2375 |
'', |
| 2376 |
' alias of :py:class:`~datetime.date`', |
| 2377 |
'', |
| 2378 |
'', |
| 2379 |
'.. py:class:: T1', |
| 2380 |
' :module: target.typevar', |
| 2381 |
'', |
| 2382 |
' T1', |
| 2383 |
'', |
| 2384 |
" alias of TypeVar('T1')", |
| 2385 |
'', |
| 2386 |
'', |
| 2387 |
'.. py:class:: T3', |
| 2388 |
' :module: target.typevar', |
| 2389 |
'', |
| 2390 |
' T3', |
| 2391 |
'', |
| 2392 |
" alias of TypeVar('T3', int, str)", |
| 2393 |
'', |
| 2394 |
'', |
| 2395 |
'.. py:class:: T4', |
| 2396 |
' :module: target.typevar', |
| 2397 |
'', |
| 2398 |
' T4', |
| 2399 |
'', |
| 2400 |
" alias of TypeVar('T4', covariant=True)", |
| 2401 |
'', |
| 2402 |
'', |
| 2403 |
'.. py:class:: T5', |
| 2404 |
' :module: target.typevar', |
| 2405 |
'', |
| 2406 |
' T5', |
| 2407 |
'', |
| 2408 |
" alias of TypeVar('T5', contravariant=True)", |
| 2409 |
'', |
| 2410 |
'', |
| 2411 |
'.. py:class:: T6', |
| 2412 |
' :module: target.typevar', |
| 2413 |
'', |
| 2414 |
' T6', |
| 2415 |
'', |
| 2416 |
' alias of :py:class:`~datetime.date`', |
| 2417 |
'', |
| 2418 |
'', |
| 2419 |
'.. py:class:: T7', |
| 2420 |
' :module: target.typevar', |
| 2421 |
'', |
| 2422 |
' T7', |
| 2423 |
'', |
| 2424 |
" alias of TypeVar('T7', bound=\\ :py:class:`int`)", |
| 2425 |
'', |
| 2426 |
] |
| 2427 |
|
| 2428 |
|
| 2429 |
def test_autodoc_Annotated() -> None: |
| 2430 |
options = { |
| 2431 |
'members': None, |
| 2432 |
'member-order': 'bysource', |
| 2433 |
} |
| 2434 |
actual = do_autodoc('module', 'target.annotated', options=options) |
| 2435 |
assert actual == [ |
| 2436 |
'', |
| 2437 |
'.. py:module:: target.annotated', |
| 2438 |
'', |
| 2439 |
'', |
| 2440 |
'.. py:class:: FuncValidator(func: function)', |
| 2441 |
' :module: target.annotated', |
| 2442 |
'', |
| 2443 |
'', |
| 2444 |
'.. py:class:: MaxLen(max_length: int, whitelisted_words: list[str])', |
| 2445 |
' :module: target.annotated', |
| 2446 |
'', |
| 2447 |
'', |
| 2448 |
'.. py:data:: ValidatedString', |
| 2449 |
' :module: target.annotated', |
| 2450 |
'', |
| 2451 |
' Type alias for a validated string.', |
| 2452 |
'', |
| 2453 |
' alias of :py:class:`~typing.Annotated`\\ [:py:class:`str`, ' |
| 2454 |
':py:class:`~target.annotated.FuncValidator`\\ (func=\\ :py:class:`~target.annotated.validate`)]', |
| 2455 |
'', |
| 2456 |
'', |
| 2457 |
".. py:function:: hello(name: ~typing.Annotated[str, 'attribute']) -> None", |
| 2458 |
' :module: target.annotated', |
| 2459 |
'', |
| 2460 |
' docstring', |
| 2461 |
'', |
| 2462 |
'', |
| 2463 |
'.. py:class:: AnnotatedAttributes()', |
| 2464 |
' :module: target.annotated', |
| 2465 |
'', |
| 2466 |
' docstring', |
| 2467 |
'', |
| 2468 |
'', |
| 2469 |
' .. py:attribute:: AnnotatedAttributes.name', |
| 2470 |
' :module: target.annotated', |
| 2471 |
" :type: ~typing.Annotated[str, 'attribute']", |
| 2472 |
'', |
| 2473 |
' Docstring about the ``name`` attribute.', |
| 2474 |
'', |
| 2475 |
'', |
| 2476 |
' .. py:attribute:: AnnotatedAttributes.max_len', |
| 2477 |
' :module: target.annotated', |
| 2478 |
" :type: list[~typing.Annotated[str, ~target.annotated.MaxLen(max_length=10, whitelisted_words=['word_one', 'word_two'])]]", |
| 2479 |
'', |
| 2480 |
' Docstring about the ``max_len`` attribute.', |
| 2481 |
'', |
| 2482 |
'', |
| 2483 |
' .. py:attribute:: AnnotatedAttributes.validated', |
| 2484 |
' :module: target.annotated', |
| 2485 |
' :type: ~typing.Annotated[str, ~target.annotated.FuncValidator(func=~target.annotated.validate)]', |
| 2486 |
'', |
| 2487 |
' Docstring about the ``validated`` attribute.', |
| 2488 |
'', |
| 2489 |
] |
| 2490 |
|
| 2491 |
|
| 2492 |
def test_autodoc_TYPE_CHECKING() -> None: |
| 2493 |
options = { |
| 2494 |
'members': None, |
| 2495 |
'undoc-members': None, |
| 2496 |
} |
| 2497 |
actual = do_autodoc('module', 'target.TYPE_CHECKING', options=options) |
| 2498 |
assert actual == [ |
| 2499 |
'', |
| 2500 |
'.. py:module:: target.TYPE_CHECKING', |
| 2501 |
'', |
| 2502 |
'', |
| 2503 |
'.. py:class:: Foo()', |
| 2504 |
' :module: target.TYPE_CHECKING', |
| 2505 |
'', |
| 2506 |
'', |
| 2507 |
' .. py:attribute:: Foo.attr1', |
| 2508 |
' :module: target.TYPE_CHECKING', |
| 2509 |
' :type: ~io.StringIO', |
| 2510 |
'', |
| 2511 |
'', |
| 2512 |
'.. py:function:: spam(ham: ~collections.abc.Iterable[str]) -> tuple[~gettext.NullTranslations, bool]', |
| 2513 |
' :module: target.TYPE_CHECKING', |
| 2514 |
'', |
| 2515 |
] |
| 2516 |
|
| 2517 |
|
| 2518 |
def test_autodoc_TYPE_CHECKING_circular_import() -> None: |
| 2519 |
options = { |
| 2520 |
'members': None, |
| 2521 |
'undoc-members': None, |
| 2522 |
} |
| 2523 |
actual = do_autodoc('module', 'circular_import', options=options) |
| 2524 |
assert actual == [ |
| 2525 |
'', |
| 2526 |
'.. py:module:: circular_import', |
| 2527 |
'', |
| 2528 |
] |
| 2529 |
assert sys.modules['circular_import'].a is sys.modules['circular_import.a'] |
| 2530 |
|
| 2531 |
|
| 2532 |
def test_singledispatch() -> None: |
| 2533 |
options = {'members': None} |
| 2534 |
actual = do_autodoc('module', 'target.singledispatch', options=options) |
| 2535 |
assert actual == [ |
| 2536 |
'', |
| 2537 |
'.. py:module:: target.singledispatch', |
| 2538 |
'', |
| 2539 |
'', |
| 2540 |
'.. py:function:: func(arg, kwarg=None)', |
| 2541 |
' func(arg: float, kwarg=None)', |
| 2542 |
' func(arg: int, kwarg=None)', |
| 2543 |
' func(arg: str, kwarg=None)', |
| 2544 |
' func(arg: dict, kwarg=None)', |
| 2545 |
' :module: target.singledispatch', |
| 2546 |
'', |
| 2547 |
' A function for general use.', |
| 2548 |
'', |
| 2549 |
] |
| 2550 |
|
| 2551 |
|
| 2552 |
def test_singledispatchmethod() -> None: |
| 2553 |
options = {'members': None} |
| 2554 |
actual = do_autodoc('module', 'target.singledispatchmethod', options=options) |
| 2555 |
assert actual == [ |
| 2556 |
'', |
| 2557 |
'.. py:module:: target.singledispatchmethod', |
| 2558 |
'', |
| 2559 |
'', |
| 2560 |
'.. py:class:: Foo()', |
| 2561 |
' :module: target.singledispatchmethod', |
| 2562 |
'', |
| 2563 |
' docstring', |
| 2564 |
'', |
| 2565 |
'', |
| 2566 |
' .. py:method:: Foo.meth(arg, kwarg=None)', |
| 2567 |
' Foo.meth(arg: float, kwarg=None)', |
| 2568 |
' Foo.meth(arg: int, kwarg=None)', |
| 2569 |
' Foo.meth(arg: str, kwarg=None)', |
| 2570 |
' Foo.meth(arg: dict, kwarg=None)', |
| 2571 |
' :module: target.singledispatchmethod', |
| 2572 |
'', |
| 2573 |
' A method for general use.', |
| 2574 |
'', |
| 2575 |
] |
| 2576 |
|
| 2577 |
|
| 2578 |
def test_singledispatchmethod_automethod() -> None: |
| 2579 |
actual = do_autodoc('method', 'target.singledispatchmethod.Foo.meth') |
| 2580 |
assert actual == [ |
| 2581 |
'', |
| 2582 |
'.. py:method:: Foo.meth(arg, kwarg=None)', |
| 2583 |
' Foo.meth(arg: float, kwarg=None)', |
| 2584 |
' Foo.meth(arg: int, kwarg=None)', |
| 2585 |
' Foo.meth(arg: str, kwarg=None)', |
| 2586 |
' Foo.meth(arg: dict, kwarg=None)', |
| 2587 |
' :module: target.singledispatchmethod', |
| 2588 |
'', |
| 2589 |
' A method for general use.', |
| 2590 |
'', |
| 2591 |
] |
| 2592 |
|
| 2593 |
|
| 2594 |
def test_singledispatchmethod_classmethod() -> None: |
| 2595 |
options = {'members': None} |
| 2596 |
actual = do_autodoc( |
| 2597 |
'module', 'target.singledispatchmethod_classmethod', options=options |
| 2598 |
) |
| 2599 |
assert actual == [ |
| 2600 |
'', |
| 2601 |
'.. py:module:: target.singledispatchmethod_classmethod', |
| 2602 |
'', |
| 2603 |
'', |
| 2604 |
'.. py:class:: Foo()', |
| 2605 |
' :module: target.singledispatchmethod_classmethod', |
| 2606 |
'', |
| 2607 |
' docstring', |
| 2608 |
'', |
| 2609 |
'', |
| 2610 |
' .. py:method:: Foo.class_meth(arg, kwarg=None)', |
| 2611 |
' Foo.class_meth(arg: float, kwarg=None)', |
| 2612 |
' Foo.class_meth(arg: int, kwarg=None)', |
| 2613 |
' Foo.class_meth(arg: str, kwarg=None)', |
| 2614 |
' Foo.class_meth(arg: dict, kwarg=None)', |
| 2615 |
' :module: target.singledispatchmethod_classmethod', |
| 2616 |
' :classmethod:', |
| 2617 |
'', |
| 2618 |
' A class method for general use.', |
| 2619 |
'', |
| 2620 |
] |
| 2621 |
|
| 2622 |
|
| 2623 |
def test_singledispatchmethod_classmethod_automethod() -> None: |
| 2624 |
actual = do_autodoc( |
| 2625 |
'method', 'target.singledispatchmethod_classmethod.Foo.class_meth' |
| 2626 |
) |
| 2627 |
assert actual == [ |
| 2628 |
'', |
| 2629 |
'.. py:method:: Foo.class_meth(arg, kwarg=None)', |
| 2630 |
' Foo.class_meth(arg: float, kwarg=None)', |
| 2631 |
' Foo.class_meth(arg: int, kwarg=None)', |
| 2632 |
' Foo.class_meth(arg: str, kwarg=None)', |
| 2633 |
' Foo.class_meth(arg: dict, kwarg=None)', |
| 2634 |
' :module: target.singledispatchmethod_classmethod', |
| 2635 |
' :classmethod:', |
| 2636 |
'', |
| 2637 |
' A class method for general use.', |
| 2638 |
'', |
| 2639 |
] |
| 2640 |
|
| 2641 |
|
| 2642 |
@pytest.mark.skipif( |
| 2643 |
sys.version_info[:2] >= (3, 13), |
| 2644 |
reason='Cython does not support Python 3.13 yet.', |
| 2645 |
) |
| 2646 |
@pytest.mark.skipif(pyximport is None, reason='cython is not installed') |
| 2647 |
def test_cython() -> None: |
| 2648 |
options = { |
| 2649 |
'members': None, |
| 2650 |
'undoc-members': None, |
| 2651 |
} |
| 2652 |
actual = do_autodoc('module', 'target.cython', options=options) |
| 2653 |
assert actual == [ |
| 2654 |
'', |
| 2655 |
'.. py:module:: target.cython', |
| 2656 |
'', |
| 2657 |
'', |
| 2658 |
'.. py:class:: Class()', |
| 2659 |
' :module: target.cython', |
| 2660 |
'', |
| 2661 |
' Docstring.', |
| 2662 |
'', |
| 2663 |
'', |
| 2664 |
' .. py:method:: Class.meth(name: str, age: int = 0) -> None', |
| 2665 |
' :module: target.cython', |
| 2666 |
'', |
| 2667 |
' Docstring.', |
| 2668 |
'', |
| 2669 |
'', |
| 2670 |
'.. py:function:: foo(x: int, *args, y: str, **kwargs)', |
| 2671 |
' :module: target.cython', |
| 2672 |
'', |
| 2673 |
' Docstring.', |
| 2674 |
'', |
| 2675 |
] |
| 2676 |
|
| 2677 |
|
| 2678 |
def test_final() -> None: |
| 2679 |
options = {'members': None} |
| 2680 |
actual = do_autodoc('module', 'target.final', options=options) |
| 2681 |
assert actual == [ |
| 2682 |
'', |
| 2683 |
'.. py:module:: target.final', |
| 2684 |
'', |
| 2685 |
'', |
| 2686 |
'.. py:class:: Class()', |
| 2687 |
' :module: target.final', |
| 2688 |
' :final:', |
| 2689 |
'', |
| 2690 |
' docstring', |
| 2691 |
'', |
| 2692 |
'', |
| 2693 |
' .. py:method:: Class.meth1()', |
| 2694 |
' :module: target.final', |
| 2695 |
' :final:', |
| 2696 |
'', |
| 2697 |
' docstring', |
| 2698 |
'', |
| 2699 |
'', |
| 2700 |
' .. py:method:: Class.meth2()', |
| 2701 |
' :module: target.final', |
| 2702 |
'', |
| 2703 |
' docstring', |
| 2704 |
'', |
| 2705 |
'', |
| 2706 |
' .. py:method:: Class.meth3()', |
| 2707 |
' :module: target.final', |
| 2708 |
' :final:', |
| 2709 |
'', |
| 2710 |
' docstring', |
| 2711 |
'', |
| 2712 |
'', |
| 2713 |
' .. py:method:: Class.meth4()', |
| 2714 |
' :module: target.final', |
| 2715 |
' :final:', |
| 2716 |
'', |
| 2717 |
' docstring', |
| 2718 |
'', |
| 2719 |
] |
| 2720 |
|
| 2721 |
|
| 2722 |
def test_overload() -> None: |
| 2723 |
options = {'members': None} |
| 2724 |
actual = do_autodoc('module', 'target.overload', options=options) |
| 2725 |
assert actual == [ |
| 2726 |
'', |
| 2727 |
'.. py:module:: target.overload', |
| 2728 |
'', |
| 2729 |
'', |
| 2730 |
'.. py:class:: Bar(x: int, y: int)', |
| 2731 |
' Bar(x: str, y: str)', |
| 2732 |
' :module: target.overload', |
| 2733 |
'', |
| 2734 |
' docstring', |
| 2735 |
'', |
| 2736 |
'', |
| 2737 |
'.. py:class:: Baz(x: int, y: int)', |
| 2738 |
' Baz(x: str, y: str)', |
| 2739 |
' :module: target.overload', |
| 2740 |
'', |
| 2741 |
' docstring', |
| 2742 |
'', |
| 2743 |
'', |
| 2744 |
'.. py:class:: Foo(x: int, y: int)', |
| 2745 |
' Foo(x: str, y: str)', |
| 2746 |
' :module: target.overload', |
| 2747 |
'', |
| 2748 |
' docstring', |
| 2749 |
'', |
| 2750 |
'', |
| 2751 |
'.. py:class:: Math()', |
| 2752 |
' :module: target.overload', |
| 2753 |
'', |
| 2754 |
' docstring', |
| 2755 |
'', |
| 2756 |
'', |
| 2757 |
' .. py:method:: Math.sum(x: int, y: int = 0) -> int', |
| 2758 |
' Math.sum(x: float, y: float = 0.0) -> float', |
| 2759 |
' Math.sum(x: str, y: str = None) -> str', |
| 2760 |
' :module: target.overload', |
| 2761 |
'', |
| 2762 |
' docstring', |
| 2763 |
'', |
| 2764 |
'', |
| 2765 |
'.. py:function:: sum(x: int, y: int = 0) -> int', |
| 2766 |
' sum(x: float, y: float = 0.0) -> float', |
| 2767 |
' sum(x: str, y: str = None) -> str', |
| 2768 |
' :module: target.overload', |
| 2769 |
'', |
| 2770 |
' docstring', |
| 2771 |
'', |
| 2772 |
] |
| 2773 |
|
| 2774 |
|
| 2775 |
def test_overload2() -> None: |
| 2776 |
options = {'members': None} |
| 2777 |
actual = do_autodoc('module', 'target.overload2', options=options) |
| 2778 |
assert actual == [ |
| 2779 |
'', |
| 2780 |
'.. py:module:: target.overload2', |
| 2781 |
'', |
| 2782 |
'', |
| 2783 |
'.. py:class:: Baz(x: int, y: int)', |
| 2784 |
' Baz(x: str, y: str)', |
| 2785 |
' :module: target.overload2', |
| 2786 |
'', |
| 2787 |
] |
| 2788 |
|
| 2789 |
|
| 2790 |
def test_overload3() -> None: |
| 2791 |
options = {'members': None} |
| 2792 |
actual = do_autodoc('module', 'target.overload3', options=options) |
| 2793 |
assert actual == [ |
| 2794 |
'', |
| 2795 |
'.. py:module:: target.overload3', |
| 2796 |
'', |
| 2797 |
'', |
| 2798 |
'.. py:function:: test(x: int) -> int', |
| 2799 |
' test(x: list[int]) -> list[int]', |
| 2800 |
' test(x: str) -> str', |
| 2801 |
' test(x: float) -> float', |
| 2802 |
' :module: target.overload3', |
| 2803 |
'', |
| 2804 |
' Documentation.', |
| 2805 |
'', |
| 2806 |
] |
| 2807 |
|
| 2808 |
|
| 2809 |
def test_pymodule_for_ModuleLevelDocumenter() -> None: |
| 2810 |
ref_context: dict[str, Any] = {'py:module': 'target.classes'} |
| 2811 |
actual = do_autodoc('class', 'Foo', ref_context=ref_context) |
| 2812 |
assert actual == [ |
| 2813 |
'', |
| 2814 |
'.. py:class:: Foo()', |
| 2815 |
' :module: target.classes', |
| 2816 |
'', |
| 2817 |
] |
| 2818 |
|
| 2819 |
|
| 2820 |
def test_pymodule_for_ClassLevelDocumenter() -> None: |
| 2821 |
ref_context: dict[str, Any] = {'py:module': 'target.methods'} |
| 2822 |
actual = do_autodoc('method', 'Base.meth', ref_context=ref_context) |
| 2823 |
assert actual == [ |
| 2824 |
'', |
| 2825 |
'.. py:method:: Base.meth()', |
| 2826 |
' :module: target.methods', |
| 2827 |
'', |
| 2828 |
] |
| 2829 |
|
| 2830 |
|
| 2831 |
def test_pyclass_for_ClassLevelDocumenter() -> None: |
| 2832 |
ref_context: dict[str, Any] = {'py:module': 'target.methods', 'py:class': 'Base'} |
| 2833 |
actual = do_autodoc('method', 'meth', ref_context=ref_context) |
| 2834 |
assert actual == [ |
| 2835 |
'', |
| 2836 |
'.. py:method:: Base.meth()', |
| 2837 |
' :module: target.methods', |
| 2838 |
'', |
| 2839 |
] |
| 2840 |
|
| 2841 |
|
| 2842 |
def test_autodoc(caplog: pytest.LogCaptureFixture) -> None: |
| 2843 |
# work around sphinx.util.logging.setup() |
| 2844 |
logger = logging.getLogger('sphinx') |
| 2845 |
logger.handlers[:] = [caplog.handler] |
| 2846 |
caplog.set_level(logging.WARNING) |
| 2847 |
|
| 2848 |
config = _AutodocConfig(autodoc_mock_imports=['dummy']) |
| 2849 |
options = {'members': None} |
| 2850 |
actual = do_autodoc( |
| 2851 |
'module', 'autodoc_dummy_module', config=config, options=options |
| 2852 |
) |
| 2853 |
assert actual == [ |
| 2854 |
'', |
| 2855 |
'.. py:module:: autodoc_dummy_module', |
| 2856 |
'', |
| 2857 |
'', |
| 2858 |
'.. py:function:: test()', |
| 2859 |
' :module: autodoc_dummy_module', |
| 2860 |
'', |
| 2861 |
' Dummy function using dummy.*', |
| 2862 |
'', |
| 2863 |
] |
| 2864 |
|
| 2865 |
# See: https://github.com/sphinx-doc/sphinx/issues/2437 |
| 2866 |
do_autodoc('module', 'bug2437.autodoc_dummy_foo', options=options) |
| 2867 |
actual = do_autodoc('module', 'autodoc_dummy_bar', options=options) |
| 2868 |
assert actual == [ |
| 2869 |
'', |
| 2870 |
'.. py:module:: autodoc_dummy_bar', |
| 2871 |
'', |
| 2872 |
'', |
| 2873 |
'.. py:class:: Bar()', |
| 2874 |
' :module: autodoc_dummy_bar', |
| 2875 |
'', |
| 2876 |
' Dummy class Bar with alias.', |
| 2877 |
'', |
| 2878 |
'', |
| 2879 |
' .. py:attribute:: Bar.my_name', |
| 2880 |
' :module: autodoc_dummy_bar', |
| 2881 |
'', |
| 2882 |
' alias of :py:class:`~bug2437.autodoc_dummy_foo.Foo`', |
| 2883 |
] |
| 2884 |
|
| 2885 |
assert not caplog.records |
| 2886 |
|
| 2887 |
|
| 2888 |
def test_name_conflict() -> None: |
| 2889 |
actual = do_autodoc('class', 'target.name_conflict.foo') |
| 2890 |
assert actual == [ |
| 2891 |
'', |
| 2892 |
'.. py:class:: foo()', |
| 2893 |
' :module: target.name_conflict', |
| 2894 |
'', |
| 2895 |
' docstring of target.name_conflict::foo.', |
| 2896 |
'', |
| 2897 |
] |
| 2898 |
|
| 2899 |
actual = do_autodoc('class', 'target.name_conflict.foo.bar') |
| 2900 |
assert actual == [ |
| 2901 |
'', |
| 2902 |
'.. py:class:: bar()', |
| 2903 |
' :module: target.name_conflict.foo', |
| 2904 |
'', |
| 2905 |
' docstring of target.name_conflict.foo::bar.', |
| 2906 |
'', |
| 2907 |
] |
| 2908 |
|
| 2909 |
|
| 2910 |
def test_name_mangling() -> None: |
| 2911 |
options = { |
| 2912 |
'members': None, |
| 2913 |
'undoc-members': None, |
| 2914 |
'private-members': None, |
| 2915 |
} |
| 2916 |
actual = do_autodoc('module', 'target.name_mangling', options=options) |
| 2917 |
assert actual == [ |
| 2918 |
'', |
| 2919 |
'.. py:module:: target.name_mangling', |
| 2920 |
'', |
| 2921 |
'', |
| 2922 |
'.. py:class:: Bar()', |
| 2923 |
' :module: target.name_mangling', |
| 2924 |
'', |
| 2925 |
'', |
| 2926 |
' .. py:attribute:: Bar._Baz__email', |
| 2927 |
' :module: target.name_mangling', |
| 2928 |
' :value: None', |
| 2929 |
'', |
| 2930 |
' a member having mangled-like name', |
| 2931 |
'', |
| 2932 |
'', |
| 2933 |
' .. py:attribute:: Bar.__address', |
| 2934 |
' :module: target.name_mangling', |
| 2935 |
' :value: None', |
| 2936 |
'', |
| 2937 |
'', |
| 2938 |
'.. py:class:: Foo()', |
| 2939 |
' :module: target.name_mangling', |
| 2940 |
'', |
| 2941 |
'', |
| 2942 |
' .. py:attribute:: Foo.__age', |
| 2943 |
' :module: target.name_mangling', |
| 2944 |
' :value: None', |
| 2945 |
'', |
| 2946 |
'', |
| 2947 |
' .. py:attribute:: Foo.__name', |
| 2948 |
' :module: target.name_mangling', |
| 2949 |
' :value: None', |
| 2950 |
'', |
| 2951 |
' name of Foo', |
| 2952 |
'', |
| 2953 |
] |
| 2954 |
|
| 2955 |
|
| 2956 |
def test_type_union_operator() -> None: |
| 2957 |
options = {'members': None} |
| 2958 |
actual = do_autodoc('module', 'target.pep604', options=options) |
| 2959 |
assert actual == [ |
| 2960 |
'', |
| 2961 |
'.. py:module:: target.pep604', |
| 2962 |
'', |
| 2963 |
'', |
| 2964 |
'.. py:class:: Foo()', |
| 2965 |
' :module: target.pep604', |
| 2966 |
'', |
| 2967 |
' docstring', |
| 2968 |
'', |
| 2969 |
'', |
| 2970 |
' .. py:attribute:: Foo.attr', |
| 2971 |
' :module: target.pep604', |
| 2972 |
' :type: int | str', |
| 2973 |
'', |
| 2974 |
' docstring', |
| 2975 |
'', |
| 2976 |
'', |
| 2977 |
' .. py:method:: Foo.meth(x: int | str, y: int | str) -> int | str', |
| 2978 |
' :module: target.pep604', |
| 2979 |
'', |
| 2980 |
' docstring', |
| 2981 |
'', |
| 2982 |
'', |
| 2983 |
'.. py:data:: attr', |
| 2984 |
' :module: target.pep604', |
| 2985 |
' :type: int | str', |
| 2986 |
'', |
| 2987 |
' docstring', |
| 2988 |
'', |
| 2989 |
'', |
| 2990 |
'.. py:function:: sum(x: int | str, y: int | str) -> int | str', |
| 2991 |
' :module: target.pep604', |
| 2992 |
'', |
| 2993 |
' docstring', |
| 2994 |
'', |
| 2995 |
] |
| 2996 |
|
| 2997 |
|
| 2998 |
def test_hide_value() -> None: |
| 2999 |
options = {'members': None} |
| 3000 |
actual = do_autodoc('module', 'target.hide_value', options=options) |
| 3001 |
assert actual == [ |
| 3002 |
'', |
| 3003 |
'.. py:module:: target.hide_value', |
| 3004 |
'', |
| 3005 |
'', |
| 3006 |
'.. py:class:: Foo()', |
| 3007 |
' :module: target.hide_value', |
| 3008 |
'', |
| 3009 |
' docstring', |
| 3010 |
'', |
| 3011 |
'', |
| 3012 |
' .. py:attribute:: Foo.SENTINEL1', |
| 3013 |
' :module: target.hide_value', |
| 3014 |
'', |
| 3015 |
' docstring', |
| 3016 |
'', |
| 3017 |
' :meta hide-value:', |
| 3018 |
'', |
| 3019 |
'', |
| 3020 |
' .. py:attribute:: Foo.SENTINEL2', |
| 3021 |
' :module: target.hide_value', |
| 3022 |
'', |
| 3023 |
' :meta hide-value:', |
| 3024 |
'', |
| 3025 |
'', |
| 3026 |
'.. py:data:: SENTINEL1', |
| 3027 |
' :module: target.hide_value', |
| 3028 |
'', |
| 3029 |
' docstring', |
| 3030 |
'', |
| 3031 |
' :meta hide-value:', |
| 3032 |
'', |
| 3033 |
'', |
| 3034 |
'.. py:data:: SENTINEL2', |
| 3035 |
' :module: target.hide_value', |
| 3036 |
'', |
| 3037 |
' :meta hide-value:', |
| 3038 |
'', |
| 3039 |
] |
| 3040 |
|
| 3041 |
|
| 3042 |
def test_canonical() -> None: |
| 3043 |
options = { |
| 3044 |
'members': None, |
| 3045 |
'imported-members': None, |
| 3046 |
} |
| 3047 |
actual = do_autodoc('module', 'target.canonical', options=options) |
| 3048 |
assert actual == [ |
| 3049 |
'', |
| 3050 |
'.. py:module:: target.canonical', |
| 3051 |
'', |
| 3052 |
'', |
| 3053 |
'.. py:class:: Bar()', |
| 3054 |
' :module: target.canonical', |
| 3055 |
'', |
| 3056 |
' docstring', |
| 3057 |
'', |
| 3058 |
'', |
| 3059 |
'.. py:class:: Foo()', |
| 3060 |
' :module: target.canonical', |
| 3061 |
' :canonical: target.canonical.original.Foo', |
| 3062 |
'', |
| 3063 |
' docstring', |
| 3064 |
'', |
| 3065 |
'', |
| 3066 |
' .. py:method:: Foo.meth()', |
| 3067 |
' :module: target.canonical', |
| 3068 |
'', |
| 3069 |
' docstring', |
| 3070 |
'', |
| 3071 |
] |
| 3072 |
|
| 3073 |
|
| 3074 |
def bounded_typevar_rst(name, bound): |
| 3075 |
return [ |
| 3076 |
'', |
| 3077 |
f'.. py:class:: {name}', |
| 3078 |
' :module: target.literal', |
| 3079 |
'', |
| 3080 |
' docstring', |
| 3081 |
'', |
| 3082 |
f' alias of TypeVar({name!r}, bound={bound})', |
| 3083 |
'', |
| 3084 |
] |
| 3085 |
|
| 3086 |
|
| 3087 |
def function_rst(name, sig): |
| 3088 |
return [ |
| 3089 |
'', |
| 3090 |
f'.. py:function:: {name}({sig})', |
| 3091 |
' :module: target.literal', |
| 3092 |
'', |
| 3093 |
' docstring', |
| 3094 |
'', |
| 3095 |
] |
| 3096 |
|
| 3097 |
|
| 3098 |
def test_literal_render() -> None: |
| 3099 |
config = _AutodocConfig(autodoc_typehints_format='short') |
| 3100 |
|
| 3101 |
# autodoc_typehints_format can take 'short' or 'fully-qualified' values |
| 3102 |
# and this will be interpreted as 'smart' or 'fully-qualified-except-typing' by restify() |
| 3103 |
# and 'smart' or 'fully-qualified' by stringify_annotation(). |
| 3104 |
|
| 3105 |
options = { |
| 3106 |
'members': None, |
| 3107 |
'exclude-members': 'MyEnum', |
| 3108 |
} |
| 3109 |
actual = do_autodoc('module', 'target.literal', config=config, options=options) |
| 3110 |
assert actual == [ |
| 3111 |
'', |
| 3112 |
'.. py:module:: target.literal', |
| 3113 |
'', |
| 3114 |
*bounded_typevar_rst('T', r"\ :py:obj:`~typing.Literal`\ [1234, 'abcd']"), |
| 3115 |
*bounded_typevar_rst( |
| 3116 |
'U', |
| 3117 |
r'\ :py:obj:`~typing.Literal`\ [' |
| 3118 |
r':py:attr:`~target.literal.MyEnum.a`, ' |
| 3119 |
r':py:attr:`~target.literal.MyEnum.b`]', |
| 3120 |
), |
| 3121 |
*function_rst('bar', "x: ~typing.Literal[1234, 'abcd']"), |
| 3122 |
*function_rst('foo', 'x: ~typing.Literal[MyEnum.a, MyEnum.b]'), |
| 3123 |
] |
| 3124 |
|
| 3125 |
# restify() assumes that 'fully-qualified' is 'fully-qualified-except-typing' |
| 3126 |
# because it is more likely that a user wants to suppress 'typing.*' |
| 3127 |
config = _AutodocConfig(autodoc_typehints_format='fully-qualified') |
| 3128 |
actual = do_autodoc('module', 'target.literal', config=config, options=options) |
| 3129 |
assert actual == [ |
| 3130 |
'', |
| 3131 |
'.. py:module:: target.literal', |
| 3132 |
'', |
| 3133 |
*bounded_typevar_rst('T', r"\ :py:obj:`~typing.Literal`\ [1234, 'abcd']"), |
| 3134 |
*bounded_typevar_rst( |
| 3135 |
'U', |
| 3136 |
r'\ :py:obj:`~typing.Literal`\ [' |
| 3137 |
r':py:attr:`target.literal.MyEnum.a`, ' |
| 3138 |
r':py:attr:`target.literal.MyEnum.b`]', |
| 3139 |
), |
| 3140 |
*function_rst('bar', "x: typing.Literal[1234, 'abcd']"), |
| 3141 |
*function_rst( |
| 3142 |
'foo', |
| 3143 |
'x: typing.Literal[target.literal.MyEnum.a, target.literal.MyEnum.b]', |
| 3144 |
), |
| 3145 |
] |
| 3146 |
|
| 3147 |
|
| 3148 |
def test_literal_render_pep604() -> None: |
| 3149 |
config = _AutodocConfig( |
| 3150 |
python_display_short_literal_types=True, |
| 3151 |
autodoc_typehints_format='short', |
| 3152 |
) |
| 3153 |
options = { |
| 3154 |
'members': None, |
| 3155 |
'exclude-members': 'MyEnum', |
| 3156 |
} |
| 3157 |
actual = do_autodoc('module', 'target.literal', config=config, options=options) |
| 3158 |
assert actual == [ |
| 3159 |
'', |
| 3160 |
'.. py:module:: target.literal', |
| 3161 |
'', |
| 3162 |
*bounded_typevar_rst('T', r"\ :py:obj:`~typing.Literal`\ [1234, 'abcd']"), |
| 3163 |
*bounded_typevar_rst( |
| 3164 |
'U', |
| 3165 |
r'\ :py:obj:`~typing.Literal`\ [' |
| 3166 |
r':py:attr:`~target.literal.MyEnum.a`, ' |
| 3167 |
r':py:attr:`~target.literal.MyEnum.b`]', |
| 3168 |
), |
| 3169 |
*function_rst('bar', "x: 1234 | 'abcd'"), |
| 3170 |
*function_rst('foo', 'x: MyEnum.a | MyEnum.b'), |
| 3171 |
] |
| 3172 |
|
| 3173 |
# restify() assumes that 'fully-qualified' is 'fully-qualified-except-typing' |
| 3174 |
# because it is more likely that a user wants to suppress 'typing.*' |
| 3175 |
config = _AutodocConfig( |
| 3176 |
python_display_short_literal_types=True, |
| 3177 |
autodoc_typehints_format='fully-qualified', |
| 3178 |
) |
| 3179 |
actual = do_autodoc('module', 'target.literal', config=config, options=options) |
| 3180 |
assert actual == [ |
| 3181 |
'', |
| 3182 |
'.. py:module:: target.literal', |
| 3183 |
'', |
| 3184 |
*bounded_typevar_rst('T', r"\ :py:obj:`~typing.Literal`\ [1234, 'abcd']"), |
| 3185 |
*bounded_typevar_rst( |
| 3186 |
'U', |
| 3187 |
r'\ :py:obj:`~typing.Literal`\ [' |
| 3188 |
r':py:attr:`target.literal.MyEnum.a`, ' |
| 3189 |
r':py:attr:`target.literal.MyEnum.b`]', |
| 3190 |
), |
| 3191 |
*function_rst('bar', "x: 1234 | 'abcd'"), |
| 3192 |
*function_rst('foo', 'x: target.literal.MyEnum.a | target.literal.MyEnum.b'), |
| 3193 |
] |
| 3194 |
|
| 3195 |
|
| 3196 |
def test_no_index_entry() -> None: |
| 3197 |
# modules can use no-index-entry |
| 3198 |
options = {'no-index-entry': None} |
| 3199 |
actual = do_autodoc('module', 'target.module', options=options) |
| 3200 |
assert ' :no-index-entry:' in actual |
| 3201 |
|
| 3202 |
# classes can use no-index-entry |
| 3203 |
actual = do_autodoc('class', 'target.classes.Foo', options=options) |
| 3204 |
assert ' :no-index-entry:' in actual |
| 3205 |
|
| 3206 |
# functions can use no-index-entry |
| 3207 |
actual = do_autodoc('function', 'target.functions.func', options=options) |
| 3208 |
assert ' :no-index-entry:' in actual |
| 3209 |
|
| 3210 |
# modules respect no-index-entry in autodoc_default_options |
| 3211 |
config = _AutodocConfig(autodoc_default_options={'no-index-entry': True}) |
| 3212 |
actual = do_autodoc('module', 'target.module', config=config) |
| 3213 |
assert ' :no-index-entry:' in actual |
| 3214 |
|
| 3215 |
# classes respect config-level no-index-entry |
| 3216 |
actual = do_autodoc('class', 'target.classes.Foo', config=config) |
| 3217 |
assert ' :no-index-entry:' in actual |
| 3218 |
|
| 3219 |
# functions respect config-level no-index-entry |
| 3220 |
actual = do_autodoc('function', 'target.functions.func', config=config) |
| 3221 |
assert ' :no-index-entry:' in actual |