Skip to content

Commit e41dfa6

Browse files
committed
Appease mypy
1 parent 6a1b366 commit e41dfa6

File tree

2 files changed

+101
-97
lines changed

2 files changed

+101
-97
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# 3rd party
88
import pytest
99
import sphinx
10-
from bs4 import BeautifulSoup # type: ignore[import-untyped]
10+
from bs4 import BeautifulSoup
1111
from sphinx.application import Sphinx
1212

1313
if sys.version_info >= (3, 10):

tests/test_autoenum.py

Lines changed: 100 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# 3rd party
1212
import pytest
1313
import sphinx
14-
from bs4 import BeautifulSoup, NavigableString # type: ignore[import-untyped]
14+
from bs4 import BeautifulSoup, NavigableString, Tag
1515
from sphinx.application import Sphinx
1616
from sphinx_toolbox.testing import HTMLRegressionFixture
1717

@@ -67,19 +67,19 @@ def preprocess_soup(soup: BeautifulSoup) -> None:
6767

6868
if sphinx.version_info >= (3, 5): # pragma: no cover
6969
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]
7171
for c in em.children:
7272
c.extract()
7373
em.contents = []
7474
em.insert(0, child)
7575

7676
for dl in soup.select("dl.py.method dt"): # .sig.sig-object.py
7777
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()
8383

8484
for dt in soup.select("span.pre"):
8585
dt.replace_with_children()
@@ -88,9 +88,9 @@ def preprocess_soup(soup: BeautifulSoup) -> None:
8888
dt.replace_with(NavigableString(dt.get_text()))
8989

9090
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]
9494

9595
for meta in cast(List[Dict], soup.find_all("meta")):
9696
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:
109109
)
110110
def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
111111
# 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]
113113
assert "autoenum Demo" == title
114114

115115
preprocess_soup(page)
@@ -121,24 +121,26 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
121121
class_count = 0
122122

123123
for class_ in page.find_all("dl"):
124-
if "enum" not in class_["class"]:
124+
if "enum" not in class_["class"]: # type: ignore[index]
125125
continue
126126

127+
dd = class_.find("dd") # type: ignore[union-attr]
127128
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]
130131
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]
134135

135136
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]
138139

139140
attr_count = 0
140141

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)
142144
if "attribute" not in attr["class"]:
143145
continue
144146

@@ -147,50 +149,53 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
147149
else:
148150
class_name = "NoMethods"
149151

152+
dt = attr.find("dt")
153+
dd = attr.find("dd")
150154
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]
152156

153157
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]
155159
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]
157161

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]
159163

160164
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]
162166

163167
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]
165169
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]
167171

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]
169173

170174
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]
172176

173177
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]
175179
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]
177181

178182
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>"
182187
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]
184189

185190
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]
187192

188193
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]
190195
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]
192197

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]
194199

195200
attr_count += 1
196201

@@ -202,14 +207,10 @@ def test_index(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
202207

203208

204209
@xfail_312
205-
@pytest.mark.parametrize(
206-
"page", [
207-
"flag.html",
208-
], indirect=True
209-
)
210+
@pytest.mark.parametrize("page", ["flag.html"], indirect=True)
210211
def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
211212
# 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]
213214
assert "autoenum Demo - Flag" == title
214215

215216
preprocess_soup(page)
@@ -221,62 +222,66 @@ def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
221222
class_count = 0
222223

223224
for class_ in page.find_all("dl"):
224-
if "flag" not in class_["class"]:
225+
if "flag" not in class_["class"]: # type: ignore[index]
225226
continue
226227

227228
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]
229230
elif class_count == 1:
230-
assert class_.find("dt")["id"] == "id0"
231+
assert class_.find("dt")["id"] == "id0" # type: ignore[union-attr,index]
231232

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."
233235

234236
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:"
237239

238240
attr_count = 0
239241

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)
241244
if "attribute" not in attr["class"]:
242245
continue
243246

247+
dt = attr.find("dt")
248+
dd = attr.find("dd")
244249
if attr_count == 0:
245250
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]
247252
elif class_count == 1:
248-
assert attr.find("dt")["id"] == "id1"
253+
assert dt["id"] == "id1" # type: ignore[index]
249254

250255
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]
252257
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]
254259

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]
256261
elif attr_count == 1:
257262
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]
259264
elif class_count == 1:
260-
assert attr.find("dt")["id"] == "id2"
265+
assert dt["id"] == "id2" # type: ignore[index]
261266

262267
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]
264269
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]
266271

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]
268273
elif attr_count == 2:
269274
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]
271276
elif class_count == 1:
272-
assert attr.find("dt")["id"] == "id3"
277+
assert dt["id"] == "id3" # type: ignore[index]
273278

274279
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]
276281
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]
278283

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]
280285

281286
attr_count += 1
282287

@@ -288,14 +293,10 @@ def test_flag(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
288293

289294

290295
@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)
296297
def test_no_member_doc(page: BeautifulSoup, html_regression: HTMLRegressionFixture):
297298
# 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]
299300
assert "autoenum Demo - Members without docstrings" == title
300301

301302
preprocess_soup(page)
@@ -307,62 +308,65 @@ def test_no_member_doc(page: BeautifulSoup, html_regression: HTMLRegressionFixtu
307308
class_count = 0
308309

309310
for class_ in page.find_all("dl"):
310-
if "enum" not in class_["class"]:
311+
if "enum" not in class_["class"]: # type: ignore[index]
311312
continue
312313

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]
316318

317319
if class_count == 0:
318320
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]
321323
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]
323325

324326
attr_count = 0
325327

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)
327330
if "attribute" not in attr["class"]:
328331
continue
329332

333+
dt = attr.find("dt")
330334
if attr_count == 0:
331335
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]
333337
elif class_count == 1:
334-
assert attr.find("dt")["id"] == "id1"
338+
assert dt["id"] == "id1" # type: ignore[index]
335339

336340
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]
338342
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]
340344

341-
assert not attr.find("dd").contents
345+
assert not attr.find("dd").contents # type: ignore[union-attr]
342346
elif attr_count == 1:
343347
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]
345349
elif class_count == 1:
346-
assert attr.find("dt")["id"] == "id2"
350+
assert dt["id"] == "id2" # type: ignore[index]
347351

348352
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]
350354
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]
352356

353-
assert not attr.find("dd").contents
357+
assert not attr.find("dd").contents # type: ignore[union-attr]
354358
elif attr_count == 2:
355359
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]
357361
elif class_count == 1:
358-
assert attr.find("dt")["id"] == "id3"
362+
assert dt["id"] == "id3" # type: ignore[index]
359363

360364
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]
362366
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]
364368

365-
assert not attr.find("dd").contents
369+
assert not attr.find("dd").contents # type: ignore[union-attr]
366370

367371
attr_count += 1
368372

0 commit comments

Comments
 (0)