1
- from typing import Mapping , Any , Dict , Union
1
+ import typing as t
2
2
3
3
import pytest
4
4
from sphinx .application import Sphinx
@@ -29,8 +29,8 @@ def process_docstring(a):
29
29
30
30
31
31
def test_mapping (app ):
32
- assert _format_terse (Mapping [str , Any ]) == ":py:class:`~typing.Mapping`"
33
- assert _format_full (Mapping [str , Any ]) == (
32
+ assert _format_terse (t . Mapping [str , t . Any ]) == ":py:class:`~typing.Mapping`"
33
+ assert _format_full (t . Mapping [str , t . Any ]) == (
34
34
r":py:class:`~typing.Mapping`\["
35
35
r":py:class:`str`, "
36
36
r":py:data:`~typing.Any`"
@@ -39,7 +39,9 @@ def test_mapping(app):
39
39
40
40
41
41
def test_dict (app ):
42
- assert _format_terse (Dict [str , Any ]) == "{:py:class:`str`: :py:data:`~typing.Any`}"
42
+ assert _format_terse (t .Dict [str , t .Any ]) == (
43
+ "{:py:class:`str`: :py:data:`~typing.Any`}"
44
+ )
43
45
44
46
45
47
def test_qualname_overrides (app ):
@@ -52,10 +54,10 @@ def test_qualname_overrides(app):
52
54
def test_qualname_overrides_recursive (app ):
53
55
sparse = pytest .importorskip ("scipy.sparse" )
54
56
55
- assert _format_terse (Union [sparse .spmatrix , str ]) == (
57
+ assert _format_terse (t . Union [sparse .spmatrix , str ]) == (
56
58
r":py:class:`~scipy.sparse.spmatrix`, :py:class:`str`"
57
59
)
58
- assert _format_full (Union [sparse .spmatrix , str ]) == (
60
+ assert _format_full (t . Union [sparse .spmatrix , str ]) == (
59
61
r":py:data:`~typing.Union`\["
60
62
r":py:class:`~scipy.sparse.spmatrix`, "
61
63
r":py:class:`str`"
@@ -66,10 +68,10 @@ def test_qualname_overrides_recursive(app):
66
68
def test_fully_qualified (app ):
67
69
sparse = pytest .importorskip ("scipy.sparse" )
68
70
69
- assert _format_terse (Union [sparse .spmatrix , str ], True ) == (
71
+ assert _format_terse (t . Union [sparse .spmatrix , str ], True ) == (
70
72
r":py:class:`scipy.sparse.spmatrix`, :py:class:`str`"
71
73
)
72
- assert _format_full (Union [sparse .spmatrix , str ], True ) == (
74
+ assert _format_full (t . Union [sparse .spmatrix , str ], True ) == (
73
75
r":py:data:`typing.Union`\["
74
76
r":py:class:`scipy.sparse.spmatrix`, "
75
77
r":py:class:`str`"
@@ -83,3 +85,34 @@ def test_classes_get_added(app, parse):
83
85
assert doc [0 ][0 ].tagname == "inline"
84
86
assert doc [0 ][0 ]["classes" ] == ["annotation" , "full" ]
85
87
# print(doc.asdom().toprettyxml())
88
+
89
+
90
+ @pytest .mark .parametrize ("formatter" , [_format_terse , _format_full ], ids = "tf" )
91
+ # These guys aren’t listed as classes in Python’s intersphinx index:
92
+ @pytest .mark .parametrize (
93
+ "annotation" ,
94
+ [
95
+ t .Any ,
96
+ t .AnyStr ,
97
+ # t.NoReturn,
98
+ t .Callable [[int ], None ],
99
+ # t.ClassVar[t.Any],
100
+ t .Optional [int ],
101
+ t .Tuple [int , str ],
102
+ t .Tuple [float , ...],
103
+ t .Union [int , str ],
104
+ ],
105
+ ids = lambda p : str (p ).replace ("typing." , "" ),
106
+ )
107
+ def test_typing_classes (app , annotation , formatter ):
108
+ name = (
109
+ getattr (annotation , "_name" , None )
110
+ or getattr (annotation , "__name__" , None )
111
+ or annotation .__origin__ ._name
112
+ )
113
+ if name == "Union" :
114
+ if formatter is _format_terse :
115
+ pytest .skip ("Tested elsewhere" )
116
+ elif len (annotation .__args__ ) == 2 and type (None ) in annotation .__args__ :
117
+ name = "Optional"
118
+ assert formatter (annotation , True ).startswith (f":py:data:`typing.{ name } " )
0 commit comments