12
12
13
13
import pytest
14
14
15
+ from scanpydoc .elegant_typehints import _last_resolve , qualname_overrides
15
16
from scanpydoc .elegant_typehints ._formatting import typehints_formatter
16
17
17
18
18
19
if TYPE_CHECKING :
19
20
from types import ModuleType
20
21
from typing import Protocol
22
+ from collections .abc import Generator
21
23
22
24
from sphinx .application import Sphinx
23
25
@@ -32,6 +34,12 @@ def __call__( # noqa: D102
32
34
NONE_RTYPE = ":rtype: :sphinx_autodoc_typehints_type:`\\ :py\\ :obj\\ :\\ `None\\ ``"
33
35
34
36
37
+ @pytest .fixture (autouse = True )
38
+ def _reset_qualname_overrides () -> Generator [None , None , None ]:
39
+ yield
40
+ qualname_overrides .clear ()
41
+
42
+
35
43
@pytest .fixture
36
44
def testmod (make_module : Callable [[str , str ], ModuleType ]) -> ModuleType :
37
45
return make_module (
@@ -240,6 +248,40 @@ def fn_test(m: object) -> None: # pragma: no cover
240
248
]
241
249
242
250
251
+ def test_resolve (app : Sphinx ) -> None :
252
+ """Test that qualname_overrides affects _last_resolve as expected."""
253
+ from docutils .nodes import TextElement , reference
254
+ from sphinx .addnodes import pending_xref
255
+ from sphinx .ext .intersphinx import InventoryAdapter
256
+
257
+ app .setup_extension ("sphinx.ext.intersphinx" )
258
+
259
+ # Inventory contains documented name
260
+ InventoryAdapter (app .env ).main_inventory ["py:class" ] = {
261
+ "test.Class" : ("TestProj" , "1" , "https://x.com" , "Class" ),
262
+ }
263
+ # Node contains name from code
264
+ node = pending_xref (refdomain = "py" , reftarget = "testmod.Class" , reftype = "class" )
265
+
266
+ resolved = _last_resolve (app , app .env , node , TextElement ())
267
+ assert isinstance (resolved , reference )
268
+ assert resolved ["refuri" ] == "https://x.com"
269
+ assert resolved ["reftitle" ] == "(in TestProj v1)"
270
+
271
+
272
+ @pytest .mark .parametrize ("qualname" , ["testmod.Class" , "nonexistent.Class" ])
273
+ def test_resolve_failure (app : Sphinx , qualname : str ) -> None :
274
+ from docutils .nodes import TextElement
275
+ from sphinx .addnodes import pending_xref
276
+
277
+ app .setup_extension ("sphinx.ext.intersphinx" )
278
+ node = pending_xref (refdomain = "py" , reftarget = qualname , reftype = "class" )
279
+
280
+ resolved = _last_resolve (app , app .env , node , TextElement ())
281
+ assert resolved is None
282
+ assert node ["reftarget" ] == qualname_overrides .get (qualname , qualname )
283
+
284
+
243
285
# These guys aren’t listed as classes in Python’s intersphinx index:
244
286
@pytest .mark .parametrize (
245
287
"annotation" ,
0 commit comments