Debian Codegraph @

tests/test_ext_autodoc/test_ext_autodoc_autoattribute.py

raw
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 pytest
10
11 from tests.test_ext_autodoc.autodoc_util import do_autodoc
12
13 pytestmark = pytest.mark.usefixtures('inject_autodoc_root_into_sys_path')
14
15
16 def test_autoattribute() -> None:
17 actual = do_autodoc('attribute', 'target.Class.attr')
18 assert actual == [
19 '',
20 '.. py:attribute:: Class.attr',
21 ' :module: target',
22 " :value: 'bar'",
23 '',
24 ' should be documented -- süß',
25 '',
26 ]
27
28
29 def test_autoattribute_novalue() -> None:
30 options = {'no-value': None}
31 actual = do_autodoc('attribute', 'target.Class.attr', options=options)
32 assert actual == [
33 '',
34 '.. py:attribute:: Class.attr',
35 ' :module: target',
36 '',
37 ' should be documented -- süß',
38 '',
39 ]
40
41
42 def test_autoattribute_typed_variable() -> None:
43 actual = do_autodoc('attribute', 'target.typed_vars.Class.attr2')
44 assert actual == [
45 '',
46 '.. py:attribute:: Class.attr2',
47 ' :module: target.typed_vars',
48 ' :type: int',
49 '',
50 ]
51
52
53 def test_autoattribute_typed_variable_in_alias() -> None:
54 actual = do_autodoc('attribute', 'target.typed_vars.Alias.attr2')
55 assert actual == [
56 '',
57 '.. py:attribute:: Alias.attr2',
58 ' :module: target.typed_vars',
59 ' :type: int',
60 '',
61 ]
62
63
64 def test_autoattribute_instance_variable() -> None:
65 actual = do_autodoc('attribute', 'target.typed_vars.Class.attr4')
66 assert actual == [
67 '',
68 '.. py:attribute:: Class.attr4',
69 ' :module: target.typed_vars',
70 ' :type: int',
71 '',
72 ' attr4',
73 '',
74 ]
75
76
77 def test_autoattribute_instance_variable_in_alias() -> None:
78 actual = do_autodoc('attribute', 'target.typed_vars.Alias.attr4')
79 assert actual == [
80 '',
81 '.. py:attribute:: Alias.attr4',
82 ' :module: target.typed_vars',
83 ' :type: int',
84 '',
85 ' attr4',
86 '',
87 ]
88
89
90 def test_autoattribute_instance_variable_without_comment() -> None:
91 actual = do_autodoc('attribute', 'target.instance_variable.Bar.attr4')
92 assert actual == [
93 '',
94 '.. py:attribute:: Bar.attr4',
95 ' :module: target.instance_variable',
96 '',
97 ]
98
99
100 def test_autoattribute_slots_variable_list() -> None:
101 actual = do_autodoc('attribute', 'target.slots.Foo.attr')
102 assert actual == [
103 '',
104 '.. py:attribute:: Foo.attr',
105 ' :module: target.slots',
106 '',
107 ]
108
109
110 def test_autoattribute_slots_variable_dict() -> None:
111 actual = do_autodoc('attribute', 'target.slots.Bar.attr1')
112 assert actual == [
113 '',
114 '.. py:attribute:: Bar.attr1',
115 ' :module: target.slots',
116 ' :type: int',
117 '',
118 ' docstring of attr1',
119 '',
120 ]
121
122
123 def test_autoattribute_slots_variable_str() -> None:
124 actual = do_autodoc('attribute', 'target.slots.Baz.attr')
125 assert actual == [
126 '',
127 '.. py:attribute:: Baz.attr',
128 ' :module: target.slots',
129 '',
130 ]
131
132
133 def test_autoattribute_GenericAlias() -> None:
134 actual = do_autodoc('attribute', 'target.genericalias.Class.T')
135 assert actual == [
136 '',
137 '.. py:attribute:: Class.T',
138 ' :module: target.genericalias',
139 '',
140 ' A list of int',
141 '',
142 ' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
143 '',
144 ]
145
146
147 def test_autoattribute_hide_value() -> None:
148 actual = do_autodoc('attribute', 'target.hide_value.Foo.SENTINEL1')
149 assert actual == [
150 '',
151 '.. py:attribute:: Foo.SENTINEL1',
152 ' :module: target.hide_value',
153 '',
154 ' docstring',
155 '',
156 ' :meta hide-value:',
157 '',
158 ]
159
160 actual = do_autodoc('attribute', 'target.hide_value.Foo.SENTINEL2')
161 assert actual == [
162 '',
163 '.. py:attribute:: Foo.SENTINEL2',
164 ' :module: target.hide_value',
165 '',
166 ' :meta hide-value:',
167 '',
168 ]