35
35
if TYPE_CHECKING :
36
36
from io import StringIO
37
37
38
+ from sphinx .domains .cpp ._ast import ASTDeclaration
38
39
39
- def parse (name , string ):
40
+
41
+ def parse (name : str , string : str ) -> ASTDeclaration :
40
42
class Config :
41
43
cpp_id_attributes = ['id_attr' ]
42
44
cpp_paren_attributes = ['paren_attr' ]
43
45
44
- parser = DefinitionParser (string , location = None , config = Config ())
46
+ parser = DefinitionParser (string , location = None , config = Config ()) # type: ignore[arg-type]
45
47
parser .allowFallbackExpressionParsing = False
46
48
ast = parser .parse_declaration (name , name )
47
49
parser .assert_end ()
48
50
# The scopedness would usually have been set by CPPEnumObject
49
51
if name == 'enum' :
50
- ast .scoped = None # simulate unscoped enum
52
+ ast .scoped = None # type: ignore[attr-defined] # simulate unscoped enum
51
53
return ast
52
54
53
55
54
- def _check (name , input , id_dict , output , key , as_text_output ):
56
+ def _check (name , input : str , id_dict : dict [ int , str ] , output , key , as_text_output ):
55
57
if key is None :
56
58
key = name
57
59
key += ' '
@@ -80,7 +82,7 @@ def _check(name, input, id_dict, output, key, as_text_output):
80
82
parent_node = addnodes .desc ()
81
83
signode = addnodes .desc_signature (input , '' )
82
84
parent_node += signode
83
- ast .describe_signature (signode , 'lastIsName' , symbol , options = {})
85
+ ast .describe_signature (signode , 'lastIsName' , symbol , options = {}) # type: ignore[arg-type]
84
86
res_as_text = parent_node .astext ()
85
87
if res_as_text != output_as_text :
86
88
print ()
@@ -90,13 +92,13 @@ def _check(name, input, id_dict, output, key, as_text_output):
90
92
print ('Node:' , parent_node )
91
93
raise DefinitionError
92
94
93
- id_expected = [None ]
95
+ id_expected : list [ str | None ] = [None ]
94
96
for i in range (1 , _max_id + 1 ):
95
97
if i in id_dict :
96
98
id_expected .append (id_dict [i ])
97
99
else :
98
100
id_expected .append (id_expected [i - 1 ])
99
- id_actual = [None ]
101
+ id_actual : list [ str | None ] = [None ]
100
102
for i in range (1 , _max_id + 1 ):
101
103
try :
102
104
id = ast .get_id (version = i )
@@ -105,14 +107,13 @@ def _check(name, input, id_dict, output, key, as_text_output):
105
107
except NoOldIdError :
106
108
id_actual .append (None )
107
109
108
- res = [True ]
109
- for i in range (1 , _max_id + 1 ):
110
- res .append (id_expected [i ] == id_actual [i ])
110
+ res_bools = [True ]
111
+ res_bools .extend (id_expected [i ] == id_actual [i ] for i in range (1 , _max_id + 1 ))
111
112
112
- if not all (res ):
113
+ if not all (res_bools ):
113
114
print ('input: %s' % input .rjust (20 ))
114
115
for i in range (1 , _max_id + 1 ):
115
- if res [i ]:
116
+ if res_bools [i ]:
116
117
continue
117
118
print ('Error in id version %d.' % i )
118
119
print ('result: %s' % id_actual [i ])
@@ -121,7 +122,14 @@ def _check(name, input, id_dict, output, key, as_text_output):
121
122
raise DefinitionError
122
123
123
124
124
- def check (name , input , id_dict , output = None , key = None , as_text_output = None ):
125
+ def check (
126
+ name : str ,
127
+ input : str ,
128
+ id_dict : dict [int , str ],
129
+ output = None ,
130
+ key = None ,
131
+ as_text_output = None ,
132
+ ) -> None :
125
133
if output is None :
126
134
output = input
127
135
# First, check without semicolon
@@ -177,7 +185,7 @@ def make_id_v2():
177
185
178
186
179
187
def test_domain_cpp_ast_expressions () -> None :
180
- def expr_check (expr , id , id4 = None ):
188
+ def expr_check (expr : str , id : str , id4 : str | None = None ):
181
189
ids = 'IE1CIA%s_1aE'
182
190
# call .format() on the expr to unescape double curly braces
183
191
id_dict = {2 : ids % expr .format (), 3 : ids % id }
@@ -189,7 +197,7 @@ class Config:
189
197
cpp_id_attributes = ['id_attr' ]
190
198
cpp_paren_attributes = ['paren_attr' ]
191
199
192
- parser = DefinitionParser (expr , location = None , config = Config ())
200
+ parser = DefinitionParser (expr , location = None , config = Config ()) # type: ignore[arg-type]
193
201
parser .allowFallbackExpressionParsing = False
194
202
ast = parser .parse_expression ()
195
203
res = str (ast )
@@ -1472,12 +1480,12 @@ def test_domain_cpp_ast_attributes() -> None:
1472
1480
check ('enumerator' , '{key}Foo [[attr1]] [[attr2]] = 42' , {2 : '3Foo' })
1473
1481
1474
1482
1475
- def check_ast_xref_parsing (target ) :
1483
+ def check_ast_xref_parsing (target : str ) -> None :
1476
1484
class Config :
1477
1485
cpp_id_attributes = ['id_attr' ]
1478
1486
cpp_paren_attributes = ['paren_attr' ]
1479
1487
1480
- parser = DefinitionParser (target , location = '' , config = Config ())
1488
+ parser = DefinitionParser (target , location = '' , config = Config ()) # type: ignore[arg-type]
1481
1489
parser .parse_xref_object ()
1482
1490
parser .assert_end ()
1483
1491
@@ -1518,6 +1526,8 @@ def test_domain_cpp_ast_xref_parsing() -> None:
1518
1526
def test_domain_cpp_template_parameters_is_pack (param : str , is_pack : bool ):
1519
1527
def parse_template_parameter (param : str ):
1520
1528
ast = parse ('type' , 'template<' + param + '> X' )
1529
+ assert ast .templatePrefix is not None
1530
+ assert ast .templatePrefix .templates is not None
1521
1531
return ast .templatePrefix .templates [0 ].params [0 ]
1522
1532
1523
1533
ast = parse_template_parameter (param )
0 commit comments