1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
"""Test the autodoc extension."""
from __future__ import annotations
from typing import TYPE_CHECKING
import pytest
from tests.test_extensions.autodoc_util import do_autodoc
if TYPE_CHECKING:
from sphinx.testing.util import SphinxTestApp
@pytest.mark.sphinx(
'html',
testroot='ext-autodoc',
confoverrides={'autodoc_preserve_defaults': True},
)
def test_preserve_defaults(app: SphinxTestApp) -> None:
color = '0xFFFFFF'
options = {'members': None}
actual = do_autodoc(app, 'module', 'target.preserve_defaults', options)
assert list(actual) == [
'',
'.. py:module:: target.preserve_defaults',
'',
'',
'.. py:class:: Class()',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
' .. py:method:: Class.clsmeth(name: str = CONSTANT, sentinel: ~typing.Any = '
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s, *, '
'kwarg1, kwarg2=%s) -> None' % (color, color),
' :module: target.preserve_defaults',
' :classmethod:',
'',
' docstring',
'',
'',
' .. py:method:: Class.meth(name: str = CONSTANT, sentinel: ~typing.Any = '
'SENTINEL, now: ~datetime.datetime = datetime.now(), color: int = %s, *, '
'kwarg1, kwarg2=%s) -> None' % (color, color),
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
'.. py:class:: MultiLine()',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop1',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop2',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop3',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop4',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
' .. py:property:: MultiLine.prop5',
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
'.. py:function:: foo(name: str = CONSTANT, sentinel: ~typing.Any = SENTINEL, '
'now: ~datetime.datetime = datetime.now(), color: int = %s, *, kwarg1, '
'kwarg2=%s) -> None' % (color, color),
' :module: target.preserve_defaults',
'',
' docstring',
'',
'',
'.. py:function:: get_sentinel(custom=SENTINEL)',
' :module: target.preserve_defaults',
'',
' docstring',
'',
]
@pytest.mark.sphinx(
'html',
testroot='ext-autodoc',
confoverrides={'autodoc_preserve_defaults': True},
)
def test_preserve_defaults_special_constructs(app: SphinxTestApp) -> None:
options = {'members': None}
actual = do_autodoc(
app, 'module', 'target.preserve_defaults_special_constructs', options
)
# * dataclasses.dataclass:
# - __init__ source code is not available
# - default values specified at class level are not discovered
# - values wrapped in a field(...) expression cannot be analyzed
# easily even if annotations were to be parsed
# * typing.NamedTuple:
# - __init__ source code is not available
# - default values specified at class level are not discovered
# * collections.namedtuple:
# - default values are specified as "default=(d1, d2, ...)"
#
# In the future, it might be possible to find some additional default
# values by parsing the source code of the annotations but the task is
# rather complex.
assert list(actual) == [
'',
'.. py:module:: target.preserve_defaults_special_constructs',
'',
'',
'.. py:class:: DataClass('
'a: int, b: object = <object object>, c: list[int] = <factory>)',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
'',
'.. py:class:: DataClassNoInit()',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
'',
'.. py:class:: MyNamedTuple1('
'a: int, b: object = <object object>, c: list[int] = [1, 2, 3])',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
'',
' .. py:attribute:: MyNamedTuple1.a',
' :module: target.preserve_defaults_special_constructs',
' :type: int',
'',
' Alias for field number 0',
'',
'',
' .. py:attribute:: MyNamedTuple1.b',
' :module: target.preserve_defaults_special_constructs',
' :type: object',
'',
' Alias for field number 1',
'',
'',
' .. py:attribute:: MyNamedTuple1.c',
' :module: target.preserve_defaults_special_constructs',
' :type: list[int]',
'',
' Alias for field number 2',
'',
'',
'.. py:class:: MyNamedTuple2(a=0, b=<object object>)',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
'',
'.. py:class:: MyTypedDict',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
'',
'.. py:data:: SENTINEL',
' :module: target.preserve_defaults_special_constructs',
' :value: <object object>',
'',
' docstring',
'',
'',
'.. py:function:: foo(x, y, z=SENTINEL)',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
'',
'.. py:function:: ze_lambda(z=SENTINEL)',
' :module: target.preserve_defaults_special_constructs',
'',
' docstring',
'',
]
|