11
11
# 3rd party
12
12
import pytest
13
13
import sphinx
14
- from bs4 import BeautifulSoup , NavigableString # type: ignore[import-untyped]
14
+ from bs4 import BeautifulSoup , NavigableString , Tag
15
15
from sphinx .application import Sphinx
16
16
from sphinx_toolbox .testing import HTMLRegressionFixture
17
17
@@ -67,19 +67,19 @@ def preprocess_soup(soup: BeautifulSoup) -> None:
67
67
68
68
if sphinx .version_info >= (3 , 5 ): # pragma: no cover
69
69
for em in soup .select ("em.property" ):
70
- child = '' .join (c .string for c in em .contents )
70
+ child = '' .join (c .string for c in em .contents ) # type: ignore[attr-defined]
71
71
for c in em .children :
72
72
c .extract ()
73
73
em .contents = []
74
74
em .insert (0 , child )
75
75
76
76
for dl in soup .select ("dl.py.method dt" ): # .sig.sig-object.py
77
77
if return_arrow in dl .contents :
78
- arrow_idx = dl .contents .index (return_arrow )
79
- dl . contents [ arrow_idx ] = NavigableString (
80
- dl . contents [arrow_idx ] + dl . contents [arrow_idx + 1 ].contents [0 ]
81
- )
82
- dl . contents [arrow_idx + 1 ].extract ()
78
+ arrow_idx = dl .contents .index (return_arrow ) # type: ignore[arg-type]
79
+ contents = dl . contents
80
+ string = contents [arrow_idx ] + contents [arrow_idx + 1 ].contents [0 ] # type: ignore[attr-defined ]
81
+ contents [ arrow_idx ] = NavigableString ( string )
82
+ contents [arrow_idx + 1 ].extract ()
83
83
84
84
for dt in soup .select ("span.pre" ):
85
85
dt .replace_with_children ()
@@ -88,9 +88,9 @@ def preprocess_soup(soup: BeautifulSoup) -> None:
88
88
dt .replace_with (NavigableString (dt .get_text ()))
89
89
90
90
for div in soup .find_all ("script" ):
91
- if div .get ("src" ):
92
- div ["src" ] = div ["src" ].split ("?v=" )[0 ]
93
- print (div ["src" ])
91
+ if cast ( Tag , div ) .get ("src" ):
92
+ div ["src" ] = div ["src" ].split ("?v=" )[0 ] # type: ignore[union-attr,index]
93
+ print (div ["src" ]) # type: ignore[index]
94
94
95
95
for meta in cast (List [Dict ], soup .find_all ("meta" )):
96
96
if meta .get ("content" , '' ) == "width=device-width, initial-scale=0.9, maximum-scale=0.9" :
@@ -109,7 +109,7 @@ def preprocess_soup(soup: BeautifulSoup) -> None:
109
109
)
110
110
def test_index (page : BeautifulSoup , html_regression : HTMLRegressionFixture ):
111
111
# Make sure the page title is what you expect
112
- title = page .find ("h1" ).contents [0 ].strip ()
112
+ title = page .find ("h1" ).contents [0 ].strip () # type: ignore[union-attr]
113
113
assert "autoenum Demo" == title
114
114
115
115
preprocess_soup (page )
@@ -121,24 +121,26 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
121
121
class_count = 0
122
122
123
123
for class_ in page .find_all ("dl" ):
124
- if "enum" not in class_ ["class" ]:
124
+ if "enum" not in class_ ["class" ]: # type: ignore[index]
125
125
continue
126
126
127
+ dd = class_ .find ("dd" ) # type: ignore[union-attr]
127
128
if class_count == 0 :
128
- assert class_ .find ("dt" )["id" ] == "enum_tools.demo.People"
129
- assert class_ . find ( "dd" ) .find_all ('p' )[0 ].contents [0 ] == "An enumeration of people."
129
+ assert class_ .find ("dt" )["id" ] == "enum_tools.demo.People" # type: ignore[union-attr,index]
130
+ assert dd .find_all ('p' )[0 ].contents [0 ] == "An enumeration of people." # type: ignore[union-attr]
130
131
elif class_count == 1 :
131
- assert class_ .find ("dt" )["id" ] == "enum_tools.demo.NoMethods"
132
- assert class_ . find ( "dd" ). find_all ( 'p'
133
- )[0 ].contents [0 ] == "An enumeration of people without any methods."
132
+ assert class_ .find ("dt" )["id" ] == "enum_tools.demo.NoMethods" # type: ignore[union-attr,index]
133
+ expected = "An enumeration of people without any methods."
134
+ assert dd . find_all ( 'p' )[0 ].contents [0 ] == expected # type: ignore[union-attr]
134
135
135
136
tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
136
- assert str (class_ . find ( "dd" ) .find_all ('p' )[1 ].contents [0 ]) == tag
137
- assert class_ . find ( "dd" ) .find_all ('p' )[2 ].contents [0 ] == "Valid values are as follows:"
137
+ assert str (dd .find_all ('p' )[1 ].contents [0 ]) == tag # type: ignore[union-attr]
138
+ assert dd .find_all ('p' )[2 ].contents [0 ] == "Valid values are as follows:" # type: ignore[union-attr]
138
139
139
140
attr_count = 0
140
141
141
- for attr in class_ .find_all ("dl" ):
142
+ for attr in class_ .find_all ("dl" ): # type: ignore[union-attr]
143
+ attr = cast (Tag , attr )
142
144
if "attribute" not in attr ["class" ]:
143
145
continue
144
146
@@ -147,50 +149,53 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
147
149
else :
148
150
class_name = "NoMethods"
149
151
152
+ dt = attr .find ("dt" )
153
+ dd = attr .find ("dd" )
150
154
if attr_count == 0 :
151
- assert attr . find ( "dt" ) ["id" ] == f"enum_tools.demo.{ class_name } .Bob"
155
+ assert dt ["id" ] == f"enum_tools.demo.{ class_name } .Bob" # type: ignore[index]
152
156
153
157
if NEW_ENUM_REPR :
154
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = { class_name } .Bob"
158
+ assert dt .em .contents [0 ] == f" = { class_name } .Bob" # type: ignore[union-attr]
155
159
else :
156
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = <{ class_name } .Bob: 1>"
160
+ assert dt .em .contents [0 ] == f" = <{ class_name } .Bob: 1>" # type: ignore[union-attr]
157
161
158
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>A person called Bob</p>"
162
+ assert str (dd .contents [0 ]) == "<p>A person called Bob</p>" # type: ignore[union-attr]
159
163
160
164
elif attr_count == 1 :
161
- assert attr . find ( "dt" ) ["id" ] == f"enum_tools.demo.{ class_name } .Alice"
165
+ assert dt ["id" ] == f"enum_tools.demo.{ class_name } .Alice" # type: ignore[index]
162
166
163
167
if NEW_ENUM_REPR :
164
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = { class_name } .Alice"
168
+ assert dt .em .contents [0 ] == f" = { class_name } .Alice" # type: ignore[union-attr]
165
169
else :
166
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = <{ class_name } .Alice: 2>"
170
+ assert dt .em .contents [0 ] == f" = <{ class_name } .Alice: 2>" # type: ignore[union-attr]
167
171
168
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>A person called Alice</p>"
172
+ assert str (dd .contents [0 ]) == "<p>A person called Alice</p>" # type: ignore[union-attr]
169
173
170
174
elif attr_count == 2 :
171
- assert attr . find ( "dt" ) ["id" ] == f"enum_tools.demo.{ class_name } .Carol"
175
+ assert dt ["id" ] == f"enum_tools.demo.{ class_name } .Carol" # type: ignore[index]
172
176
173
177
if NEW_ENUM_REPR :
174
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = { class_name } .Carol"
178
+ assert dt .em .contents [0 ] == f" = { class_name } .Carol" # type: ignore[union-attr]
175
179
else :
176
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = <{ class_name } .Carol: 3>"
180
+ assert dt .em .contents [0 ] == f" = <{ class_name } .Carol: 3>" # type: ignore[union-attr]
177
181
178
182
if class_count == 0 :
179
- assert str (attr .find ("dd" ).contents [0 ]) == "<p>A person called Carol.</p>"
180
- assert str (attr .find ("dd" ).contents [1 ]) == '\n '
181
- assert str (attr .find ("dd" ).contents [2 ]) == "<p>This is a multiline docstring.</p>"
183
+ contents = dd .contents # type: ignore[union-attr]
184
+ assert str (contents [0 ]) == "<p>A person called Carol.</p>"
185
+ assert str (contents [1 ]) == '\n '
186
+ assert str (contents [2 ]) == "<p>This is a multiline docstring.</p>"
182
187
else :
183
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>A person called Carol</p>"
188
+ assert str (dd .contents [0 ]) == "<p>A person called Carol</p>" # type: ignore[union-attr]
184
189
185
190
elif attr_count == 3 :
186
- assert attr . find ( "dt" ) ["id" ] == f"enum_tools.demo.{ class_name } .Dennis"
191
+ assert dt ["id" ] == f"enum_tools.demo.{ class_name } .Dennis" # type: ignore[index]
187
192
188
193
if NEW_ENUM_REPR :
189
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = { class_name } .Dennis"
194
+ assert dt .em .contents [0 ] == f" = { class_name } .Dennis" # type: ignore[union-attr]
190
195
else :
191
- assert attr . find ( "dt" ) .em .contents [0 ] == f" = <{ class_name } .Dennis: 4>"
196
+ assert dt .em .contents [0 ] == f" = <{ class_name } .Dennis: 4>" # type: ignore[union-attr]
192
197
193
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>A person called Dennis</p>"
198
+ assert str (dd .contents [0 ]) == "<p>A person called Dennis</p>" # type: ignore[union-attr]
194
199
195
200
attr_count += 1
196
201
@@ -202,14 +207,10 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
202
207
203
208
204
209
@xfail_312
205
- @pytest .mark .parametrize (
206
- "page" , [
207
- "flag.html" ,
208
- ], indirect = True
209
- )
210
+ @pytest .mark .parametrize ("page" , ["flag.html" ], indirect = True )
210
211
def test_flag (page : BeautifulSoup , html_regression : HTMLRegressionFixture ):
211
212
# Make sure the page title is what you expect
212
- title = page .find ("h1" ).contents [0 ].strip ()
213
+ title = page .find ("h1" ).contents [0 ].strip () # type: ignore[union-attr]
213
214
assert "autoenum Demo - Flag" == title
214
215
215
216
preprocess_soup (page )
@@ -221,62 +222,66 @@ def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
221
222
class_count = 0
222
223
223
224
for class_ in page .find_all ("dl" ):
224
- if "flag" not in class_ ["class" ]:
225
+ if "flag" not in class_ ["class" ]: # type: ignore[index]
225
226
continue
226
227
227
228
if class_count == 0 :
228
- assert class_ .find ("dt" )["id" ] == "enum_tools.demo.StatusFlags"
229
+ assert class_ .find ("dt" )["id" ] == "enum_tools.demo.StatusFlags" # type: ignore[union-attr,index]
229
230
elif class_count == 1 :
230
- assert class_ .find ("dt" )["id" ] == "id0"
231
+ assert class_ .find ("dt" )["id" ] == "id0" # type: ignore[union-attr,index]
231
232
232
- assert class_ .find ("dd" ).find_all ('p' )[0 ].contents [0 ] == "An enumeration of status codes."
233
+ ps = class_ .find ("dd" ).find_all ('p' ) # type: ignore[union-attr]
234
+ assert ps [0 ].contents [0 ] == "An enumeration of status codes."
233
235
234
236
tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
235
- assert str (class_ . find ( "dd" ). find_all ( 'p' ) [1 ].contents [0 ]) == tag
236
- assert class_ . find ( "dd" ). find_all ( 'p' ) [2 ].contents [0 ] == "Valid values are as follows:"
237
+ assert str (ps [1 ].contents [0 ]) == tag
238
+ assert ps [2 ].contents [0 ] == "Valid values are as follows:"
237
239
238
240
attr_count = 0
239
241
240
- for attr in class_ .find_all ("dl" ):
242
+ for attr in class_ .find_all ("dl" ): # type: ignore[union-attr]
243
+ attr = cast (Tag , attr )
241
244
if "attribute" not in attr ["class" ]:
242
245
continue
243
246
247
+ dt = attr .find ("dt" )
248
+ dd = attr .find ("dd" )
244
249
if attr_count == 0 :
245
250
if class_count == 0 :
246
- assert attr . find ( "dt" ) ["id" ] == "enum_tools.demo.StatusFlags.Running"
251
+ assert dt ["id" ] == "enum_tools.demo.StatusFlags.Running" # type: ignore[index]
247
252
elif class_count == 1 :
248
- assert attr . find ( "dt" ) ["id" ] == "id1"
253
+ assert dt ["id" ] == "id1" # type: ignore[index]
249
254
250
255
if NEW_ENUM_REPR :
251
- assert attr . find ( "dt" ) .em .contents [0 ] == " = StatusFlags.Running"
256
+ assert dt .em .contents [0 ] == " = StatusFlags.Running" # type: ignore[union-attr]
252
257
else :
253
- assert attr . find ( "dt" ) .em .contents [0 ] == " = <StatusFlags.Running: 1>"
258
+ assert dt .em .contents [0 ] == " = <StatusFlags.Running: 1>" # type: ignore[union-attr]
254
259
255
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>The system is running.</p>"
260
+ assert str (dd .contents [0 ]) == "<p>The system is running.</p>" # type: ignore[union-attr]
256
261
elif attr_count == 1 :
257
262
if class_count == 0 :
258
- assert attr . find ( "dt" ) ["id" ] == "enum_tools.demo.StatusFlags.Stopped"
263
+ assert dt ["id" ] == "enum_tools.demo.StatusFlags.Stopped" # type: ignore[index]
259
264
elif class_count == 1 :
260
- assert attr . find ( "dt" ) ["id" ] == "id2"
265
+ assert dt ["id" ] == "id2" # type: ignore[index]
261
266
262
267
if NEW_ENUM_REPR :
263
- assert attr . find ( "dt" ) .em .contents [0 ] == " = StatusFlags.Stopped"
268
+ assert dt .em .contents [0 ] == " = StatusFlags.Stopped" # type: ignore[union-attr]
264
269
else :
265
- assert attr . find ( "dt" ) .em .contents [0 ] == " = <StatusFlags.Stopped: 2>"
270
+ assert dt .em .contents [0 ] == " = <StatusFlags.Stopped: 2>" # type: ignore[union-attr]
266
271
267
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>The system has stopped.</p>"
272
+ assert str (dd .contents [0 ]) == "<p>The system has stopped.</p>" # type: ignore[union-attr]
268
273
elif attr_count == 2 :
269
274
if class_count == 0 :
270
- assert attr . find ( "dt" ) ["id" ] == "enum_tools.demo.StatusFlags.Error"
275
+ assert dt ["id" ] == "enum_tools.demo.StatusFlags.Error" # type: ignore[index]
271
276
elif class_count == 1 :
272
- assert attr . find ( "dt" ) ["id" ] == "id3"
277
+ assert dt ["id" ] == "id3" # type: ignore[index]
273
278
274
279
if NEW_ENUM_REPR :
275
- assert attr . find ( "dt" ) .em .contents [0 ] == " = StatusFlags.Error"
280
+ assert dt .em .contents [0 ] == " = StatusFlags.Error" # type: ignore[union-attr]
276
281
else :
277
- assert attr . find ( "dt" ) .em .contents [0 ] == " = <StatusFlags.Error: 4>"
282
+ assert dt .em .contents [0 ] == " = <StatusFlags.Error: 4>" # type: ignore[union-attr]
278
283
279
- assert str (attr . find ( "dd" ) .contents [0 ]) == "<p>An error has occurred.</p>"
284
+ assert str (dd .contents [0 ]) == "<p>An error has occurred.</p>" # type: ignore[union-attr]
280
285
281
286
attr_count += 1
282
287
@@ -288,14 +293,10 @@ def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
288
293
289
294
290
295
@xfail_312
291
- @pytest .mark .parametrize (
292
- "page" , [
293
- "no-member-doc.html" ,
294
- ], indirect = True
295
- )
296
+ @pytest .mark .parametrize ("page" , ["no-member-doc.html" ], indirect = True )
296
297
def test_no_member_doc (page : BeautifulSoup , html_regression : HTMLRegressionFixture ):
297
298
# Make sure the page title is what you expect
298
- title = page .find ("h1" ).contents [0 ].strip ()
299
+ title = page .find ("h1" ).contents [0 ].strip () # type: ignore[union-attr]
299
300
assert "autoenum Demo - Members without docstrings" == title
300
301
301
302
preprocess_soup (page )
@@ -307,62 +308,65 @@ def test_no_member_doc(page: BeautifulSoup, html_regression: HTMLRegressionFixtu
307
308
class_count = 0
308
309
309
310
for class_ in page .find_all ("dl" ):
310
- if "enum" not in class_ ["class" ]:
311
+ if "enum" not in class_ ["class" ]: # type: ignore[index]
311
312
continue
312
313
313
- assert class_ .find ("dt" )["id" ] == "enum_tools.demo.NoMemberDoc"
314
- assert class_ .find ("dd" ).find_all ('p' )[0 ].contents [
315
- 0 ] == "An enumeration of people without any member docstrings."
314
+ dd = class_ .find ("dd" ) # type: ignore[union-attr]
315
+ assert class_ .find ("dt" )["id" ] == "enum_tools.demo.NoMemberDoc" # type: ignore[union-attr,index]
316
+ expected = "An enumeration of people without any member docstrings."
317
+ assert dd .find_all ('p' )[0 ].contents [0 ] == expected # type: ignore[union-attr]
316
318
317
319
if class_count == 0 :
318
320
tag = '<code class="xref py py-class docutils literal notranslate">int</code>'
319
- assert str (class_ . find ( "dd" ) .find_all ('p' )[1 ].contents [0 ]) == tag
320
- assert class_ . find ( "dd" ) .find_all ('p' )[2 ].contents [0 ] == "Valid values are as follows:"
321
+ assert str (dd .find_all ('p' )[1 ].contents [0 ]) == tag # type: ignore[union-attr]
322
+ assert dd .find_all ('p' )[2 ].contents [0 ] == "Valid values are as follows:" # type: ignore[union-attr]
321
323
else :
322
- assert class_ . find ( "dd" ) .find_all ('p' )[1 ].contents [0 ] == "Valid values are as follows:"
324
+ assert dd .find_all ('p' )[1 ].contents [0 ] == "Valid values are as follows:" # type: ignore[union-attr]
323
325
324
326
attr_count = 0
325
327
326
- for attr in class_ .find_all ("dl" ):
328
+ for attr in class_ .find_all ("dl" ): # type: ignore[union-attr]
329
+ attr = cast (Tag , attr )
327
330
if "attribute" not in attr ["class" ]:
328
331
continue
329
332
333
+ dt = attr .find ("dt" )
330
334
if attr_count == 0 :
331
335
if class_count == 0 :
332
- assert attr . find ( "dt" ) ["id" ] == "enum_tools.demo.NoMemberDoc.Bob"
336
+ assert dt ["id" ] == "enum_tools.demo.NoMemberDoc.Bob" # type: ignore[index]
333
337
elif class_count == 1 :
334
- assert attr . find ( "dt" ) ["id" ] == "id1"
338
+ assert dt ["id" ] == "id1" # type: ignore[index]
335
339
336
340
if NEW_ENUM_REPR :
337
- assert attr . find ( "dt" ) .em .contents [0 ] == " = NoMemberDoc.Bob"
341
+ assert dt .em .contents [0 ] == " = NoMemberDoc.Bob" # type: ignore[union-attr]
338
342
else :
339
- assert attr . find ( "dt" ) .em .contents [0 ] == " = <NoMemberDoc.Bob: 1>"
343
+ assert dt .em .contents [0 ] == " = <NoMemberDoc.Bob: 1>" # type: ignore[union-attr]
340
344
341
- assert not attr .find ("dd" ).contents
345
+ assert not attr .find ("dd" ).contents # type: ignore[union-attr]
342
346
elif attr_count == 1 :
343
347
if class_count == 0 :
344
- assert attr . find ( "dt" ) ["id" ] == "enum_tools.demo.NoMemberDoc.Alice"
348
+ assert dt ["id" ] == "enum_tools.demo.NoMemberDoc.Alice" # type: ignore[index]
345
349
elif class_count == 1 :
346
- assert attr . find ( "dt" ) ["id" ] == "id2"
350
+ assert dt ["id" ] == "id2" # type: ignore[index]
347
351
348
352
if NEW_ENUM_REPR :
349
- assert attr . find ( "dt" ) .em .contents [0 ] == " = NoMemberDoc.Alice"
353
+ assert dt .em .contents [0 ] == " = NoMemberDoc.Alice" # type: ignore[union-attr]
350
354
else :
351
- assert attr . find ( "dt" ) .em .contents [0 ] == " = <NoMemberDoc.Alice: 2>"
355
+ assert dt .em .contents [0 ] == " = <NoMemberDoc.Alice: 2>" # type: ignore[union-attr]
352
356
353
- assert not attr .find ("dd" ).contents
357
+ assert not attr .find ("dd" ).contents # type: ignore[union-attr]
354
358
elif attr_count == 2 :
355
359
if class_count == 0 :
356
- assert attr . find ( "dt" ) ["id" ] == "enum_tools.demo.NoMemberDoc.Carol"
360
+ assert dt ["id" ] == "enum_tools.demo.NoMemberDoc.Carol" # type: ignore[index]
357
361
elif class_count == 1 :
358
- assert attr . find ( "dt" ) ["id" ] == "id3"
362
+ assert dt ["id" ] == "id3" # type: ignore[index]
359
363
360
364
if NEW_ENUM_REPR :
361
- assert attr . find ( "dt" ) .em .contents [0 ] == " = NoMemberDoc.Carol"
365
+ assert dt .em .contents [0 ] == " = NoMemberDoc.Carol" # type: ignore[union-attr]
362
366
else :
363
- assert attr . find ( "dt" ) .em .contents [0 ] == " = <NoMemberDoc.Carol: 3>"
367
+ assert dt .em .contents [0 ] == " = <NoMemberDoc.Carol: 3>" # type: ignore[union-attr]
364
368
365
- assert not attr .find ("dd" ).contents
369
+ assert not attr .find ("dd" ).contents # type: ignore[union-attr]
366
370
367
371
attr_count += 1
368
372
0 commit comments