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