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,7 @@ 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 (name : str , input : str , id_dict : dict [ int , str ], output = None , key = None , as_text_output = None ) -> None :
125
126
if output is None :
126
127
output = input
127
128
# First, check without semicolon
@@ -177,7 +178,7 @@ def make_id_v2():
177
178
178
179
179
180
def test_domain_cpp_ast_expressions () -> None :
180
- def expr_check (expr , id , id4 = None ):
181
+ def expr_check (expr : str , id : str , id4 : str | None = None ):
181
182
ids = 'IE1CIA%s_1aE'
182
183
# call .format() on the expr to unescape double curly braces
183
184
id_dict = {2 : ids % expr .format (), 3 : ids % id }
@@ -189,7 +190,7 @@ class Config:
189
190
cpp_id_attributes = ['id_attr' ]
190
191
cpp_paren_attributes = ['paren_attr' ]
191
192
192
- parser = DefinitionParser (expr , location = None , config = Config ())
193
+ parser = DefinitionParser (expr , location = None , config = Config ()) # type: ignore[arg-type]
193
194
parser .allowFallbackExpressionParsing = False
194
195
ast = parser .parse_expression ()
195
196
res = str (ast )
@@ -1472,12 +1473,12 @@ def test_domain_cpp_ast_attributes() -> None:
1472
1473
check ('enumerator' , '{key}Foo [[attr1]] [[attr2]] = 42' , {2 : '3Foo' })
1473
1474
1474
1475
1475
- def check_ast_xref_parsing (target ) :
1476
+ def check_ast_xref_parsing (target : str ) -> None :
1476
1477
class Config :
1477
1478
cpp_id_attributes = ['id_attr' ]
1478
1479
cpp_paren_attributes = ['paren_attr' ]
1479
1480
1480
- parser = DefinitionParser (target , location = '' , config = Config ())
1481
+ parser = DefinitionParser (target , location = '' , config = Config ()) # type: ignore[arg-type]
1481
1482
parser .parse_xref_object ()
1482
1483
parser .assert_end ()
1483
1484
@@ -1518,6 +1519,8 @@ def test_domain_cpp_ast_xref_parsing() -> None:
1518
1519
def test_domain_cpp_template_parameters_is_pack (param : str , is_pack : bool ):
1519
1520
def parse_template_parameter (param : str ):
1520
1521
ast = parse ('type' , 'template<' + param + '> X' )
1522
+ assert ast .templatePrefix is not None
1523
+ assert ast .templatePrefix .templates is not None
1521
1524
return ast .templatePrefix .templates [0 ].params [0 ]
1522
1525
1523
1526
ast = parse_template_parameter (param )
0 commit comments