| 1 |
"""Test the autodoc extension. This tests mainly for autodoc events""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from typing import TYPE_CHECKING |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from sphinx.ext.autodoc import between, cut_lines |
| 10 |
|
| 11 |
from tests.test_ext_autodoc.autodoc_util import FakeEvents, do_autodoc |
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from typing import Any |
| 15 |
|
| 16 |
from sphinx.application import Sphinx |
| 17 |
from sphinx.ext.autodoc._property_types import _AutodocObjType |
| 18 |
|
| 19 |
pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path') |
| 20 |
|
| 21 |
|
| 22 |
def test_process_docstring() -> None: |
| 23 |
def on_process_docstring( |
| 24 |
app: Sphinx, |
| 25 |
what: _AutodocObjType, |
| 26 |
name: str, |
| 27 |
obj: Any, |
| 28 |
options: Any, |
| 29 |
lines: list[str], |
| 30 |
) -> None: |
| 31 |
lines.clear() |
| 32 |
lines.append('my docstring') |
| 33 |
|
| 34 |
events = FakeEvents() |
| 35 |
events.connect('autodoc-process-docstring', on_process_docstring) |
| 36 |
|
| 37 |
actual = do_autodoc('function', 'target.process_docstring.func', events=events) |
| 38 |
assert actual == [ |
| 39 |
'', |
| 40 |
'.. py:function:: func()', |
| 41 |
' :module: target.process_docstring', |
| 42 |
'', |
| 43 |
' my docstring', |
| 44 |
'', |
| 45 |
] |
| 46 |
|
| 47 |
|
| 48 |
def test_process_docstring_for_nondatadescriptor() -> None: |
| 49 |
def on_process_docstring( |
| 50 |
app: Sphinx, |
| 51 |
what: _AutodocObjType, |
| 52 |
name: str, |
| 53 |
obj: Any, |
| 54 |
options: Any, |
| 55 |
lines: list[str], |
| 56 |
) -> None: |
| 57 |
raise RuntimeError |
| 58 |
|
| 59 |
events = FakeEvents() |
| 60 |
events.connect('autodoc-process-docstring', on_process_docstring) |
| 61 |
|
| 62 |
actual = do_autodoc('attribute', 'target.AttCls.a1', events=events) |
| 63 |
assert actual == [ |
| 64 |
'', |
| 65 |
'.. py:attribute:: AttCls.a1', |
| 66 |
' :module: target', |
| 67 |
' :value: hello world', |
| 68 |
'', |
| 69 |
] |
| 70 |
|
| 71 |
|
| 72 |
def test_cut_lines() -> None: |
| 73 |
events = FakeEvents() |
| 74 |
events.connect('autodoc-process-docstring', cut_lines(2, 2, ['function'])) |
| 75 |
|
| 76 |
actual = do_autodoc('function', 'target.process_docstring.func', events=events) |
| 77 |
assert actual == [ |
| 78 |
'', |
| 79 |
'.. py:function:: func()', |
| 80 |
' :module: target.process_docstring', |
| 81 |
'', |
| 82 |
' second line', |
| 83 |
'', |
| 84 |
] |
| 85 |
|
| 86 |
|
| 87 |
def test_cut_lines_no_objtype() -> None: |
| 88 |
docstring_lines = [ |
| 89 |
'first line', |
| 90 |
'---', |
| 91 |
'second line', |
| 92 |
'---', |
| 93 |
'third line ', |
| 94 |
'', |
| 95 |
] |
| 96 |
process = cut_lines(2) |
| 97 |
|
| 98 |
process(None, 'function', 'func', None, {}, docstring_lines) # type: ignore[arg-type] |
| 99 |
assert docstring_lines == [ |
| 100 |
'second line', |
| 101 |
'---', |
| 102 |
'third line ', |
| 103 |
'', |
| 104 |
] |
| 105 |
|
| 106 |
|
| 107 |
def test_between() -> None: |
| 108 |
events = FakeEvents() |
| 109 |
events.connect('autodoc-process-docstring', between('---', ['function'])) |
| 110 |
|
| 111 |
actual = do_autodoc('function', 'target.process_docstring.func', events=events) |
| 112 |
assert actual == [ |
| 113 |
'', |
| 114 |
'.. py:function:: func()', |
| 115 |
' :module: target.process_docstring', |
| 116 |
'', |
| 117 |
' second line', |
| 118 |
'', |
| 119 |
] |
| 120 |
|
| 121 |
|
| 122 |
def test_between_exclude() -> None: |
| 123 |
events = FakeEvents() |
| 124 |
events.connect( |
| 125 |
'autodoc-process-docstring', between('---', ['function'], exclude=True) |
| 126 |
) |
| 127 |
|
| 128 |
actual = do_autodoc('function', 'target.process_docstring.func', events=events) |
| 129 |
assert actual == [ |
| 130 |
'', |
| 131 |
'.. py:function:: func()', |
| 132 |
' :module: target.process_docstring', |
| 133 |
'', |
| 134 |
' first line', |
| 135 |
' third line', |
| 136 |
'', |
| 137 |
] |
| 138 |
|
| 139 |
|
| 140 |
def test_skip_module_member() -> None: |
| 141 |
def autodoc_skip_member( |
| 142 |
app: Sphinx, |
| 143 |
what: _AutodocObjType, |
| 144 |
name: str, |
| 145 |
obj: Any, |
| 146 |
skip: bool, |
| 147 |
options: Any, |
| 148 |
) -> bool | None: |
| 149 |
if name == 'Class': |
| 150 |
return True # Skip "Class" class in __all__ |
| 151 |
elif name == 'raises': |
| 152 |
return False # Show "raises()" function (not in __all__) |
| 153 |
return None |
| 154 |
|
| 155 |
events = FakeEvents() |
| 156 |
events.connect('autodoc-skip-member', autodoc_skip_member) |
| 157 |
|
| 158 |
options = {'members': None} |
| 159 |
actual = do_autodoc('module', 'target', events=events, options=options) |
| 160 |
assert actual == [ |
| 161 |
'', |
| 162 |
'.. py:module:: target', |
| 163 |
'', |
| 164 |
# TODO: consider. |
| 165 |
# '', |
| 166 |
# '.. py:function:: raises(exc, func, *args, **kwds)', |
| 167 |
# ' :module: target', |
| 168 |
# '', |
| 169 |
# ' Raise AssertionError if ``func(*args, **kwds)`` does not raise *exc*.', |
| 170 |
# '', |
| 171 |
] |