1
1
import inspect
2
2
import re
3
+ import sys
3
4
import typing as t
5
+ from pathlib import Path
6
+ from types import ModuleType
4
7
5
8
try :
6
9
from typing import Literal
19
22
from scanpydoc .elegant_typehints .return_tuple import process_docstring
20
23
21
24
22
- TestCls = type ("Class" , (), {})
23
- TestCls .__module__ = "_testmod"
24
- TestExc = type ("Excep" , (RuntimeError ,), {})
25
- TestExc .__module__ = "_testmod"
25
+ _testmod = sys .modules ["_testmod" ] = ModuleType ("_testmod" )
26
+ _testmod .Class = type ("Class" , (), dict (__module__ = "_testmod" ))
27
+ _testmod .SubCl = type ("SubCl" , (_testmod .Class ,), dict (__module__ = "_testmod" ))
28
+ _testmod .Excep = type ("Excep" , (RuntimeError ,), dict (__module__ = "_testmod" ))
29
+ _testmod .Excep2 = type ("Excep2" , (_testmod .Excep ,), dict (__module__ = "_testmod" ))
26
30
27
31
28
32
@pytest .fixture
29
33
def app (make_app_setup ) -> Sphinx :
30
34
return make_app_setup (
35
+ master_doc = "index" ,
31
36
extensions = [
37
+ "sphinx.ext.autodoc" ,
32
38
"sphinx.ext.napoleon" ,
33
39
"sphinx_autodoc_typehints" ,
34
40
"scanpydoc.elegant_typehints" ,
35
41
],
36
42
qualname_overrides = {
37
43
"_testmod.Class" : "test.Class" ,
44
+ "_testmod.SubCl" : "test.SubCl" ,
38
45
"_testmod.Excep" : "test.Excep" ,
46
+ "_testmod.Excep2" : "test.Excep2" ,
39
47
},
40
48
)
41
49
@@ -143,20 +151,20 @@ def test_literal(app):
143
151
144
152
145
153
def test_qualname_overrides_class (app ):
146
- assert TestCls .__module__ == "_testmod"
147
- assert _format_terse (TestCls ) == ":py:class:`~test.Class`"
154
+ assert _testmod . Class .__module__ == "_testmod"
155
+ assert _format_terse (_testmod . Class ) == ":py:class:`~test.Class`"
148
156
149
157
150
158
def test_qualname_overrides_exception (app ):
151
- assert TestExc .__module__ == "_testmod"
152
- assert _format_terse (TestExc ) == ":py:exc:`~test.Excep`"
159
+ assert _testmod . Excep .__module__ == "_testmod"
160
+ assert _format_terse (_testmod . Excep ) == ":py:exc:`~test.Excep`"
153
161
154
162
155
163
def test_qualname_overrides_recursive (app ):
156
- assert _format_terse (t .Union [TestCls , str ]) == (
164
+ assert _format_terse (t .Union [_testmod . Class , str ]) == (
157
165
r":py:class:`~test.Class`, :py:class:`str`"
158
166
)
159
- assert _format_full (t .Union [TestCls , str ]) == (
167
+ assert _format_full (t .Union [_testmod . Class , str ]) == (
160
168
r":py:data:`~typing.Union`\["
161
169
r":py:class:`~test.Class`, "
162
170
r":py:class:`str`"
@@ -165,10 +173,10 @@ def test_qualname_overrides_recursive(app):
165
173
166
174
167
175
def test_fully_qualified (app ):
168
- assert _format_terse (t .Union [TestCls , str ], True ) == (
176
+ assert _format_terse (t .Union [_testmod . Class , str ], True ) == (
169
177
r":py:class:`test.Class`, :py:class:`str`"
170
178
)
171
- assert _format_full (t .Union [TestCls , str ], True ) == (
179
+ assert _format_full (t .Union [_testmod . Class , str ], True ) == (
172
180
r":py:data:`typing.Union`\[" r":py:class:`test.Class`, " r":py:class:`str`" r"]"
173
181
)
174
182
@@ -222,6 +230,28 @@ def test_typing_class_nested(app):
222
230
)
223
231
224
232
233
+ @pytest .mark .parametrize (
234
+ "direc,base,sub" ,
235
+ [
236
+ ("autoclass" , "Class" , "SubCl" ),
237
+ ("autoexception" , "Excep" , "Excep2" ),
238
+ ],
239
+ )
240
+ def test_autodoc (app , direc , base , sub ):
241
+ Path (app .srcdir , "index.rst" ).write_text (
242
+ f"""\
243
+ .. { direc } :: _testmod.{ sub }
244
+ :show-inheritance:
245
+ """
246
+ )
247
+ app .build ()
248
+ out = Path (app .outdir , "index.html" ).read_text ()
249
+ assert not app ._warning .getvalue (), app ._warning .getvalue ()
250
+ assert re .search (rf"<code[^>]*>test\.</code><code[^>]*>{ sub } </code>" , out ), out
251
+ assert f'<a class="headerlink" href="#test.{ sub } "' in out , out
252
+ assert re .search (rf"Bases: <code[^>]*><span[^>]*>test\.{ base } " , out ), out
253
+
254
+
225
255
@pytest .mark .parametrize (
226
256
"docstring" ,
227
257
[
0 commit comments