Skip to content

Commit 65aad08

Browse files
committed
mypy: test_testing.py test_version.py
1 parent 1321803 commit 65aad08

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

Diff for: tests/test_testing.py

+46-36
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33

44
"""Tests that our test infrastructure is really working!"""
55

6+
from __future__ import annotations
7+
68
import datetime
79
import os
810
import re
911
import sys
1012
import warnings
1113

14+
from typing import List, Tuple
15+
1216
import pytest
1317

1418
import coverage
1519
from coverage.exceptions import CoverageWarning
1620
from coverage.files import actual_path
21+
from coverage.types import TArc
1722

1823
from tests.coveragetest import CoverageTest
1924
from tests.helpers import (
@@ -22,13 +27,13 @@
2227
)
2328

2429

25-
def test_xdist_sys_path_nuttiness_is_fixed():
30+
def test_xdist_sys_path_nuttiness_is_fixed() -> None:
2631
# See conftest.py:fix_xdist_sys_path
2732
assert sys.path[1] != ''
2833
assert os.environ.get('PYTHONPATH') is None
2934

3035

31-
def test_assert_count_equal():
36+
def test_assert_count_equal() -> None:
3237
assert_count_equal(set(), set())
3338
assert_count_equal({"a": 1, "b": 2}, ["b", "a"])
3439
with pytest.raises(AssertionError):
@@ -40,7 +45,7 @@ def test_assert_count_equal():
4045
class CoverageTestTest(CoverageTest):
4146
"""Test the methods in `CoverageTest`."""
4247

43-
def test_file_exists(self):
48+
def test_file_exists(self) -> None:
4449
self.make_file("whoville.txt", "We are here!")
4550
self.assert_exists("whoville.txt")
4651
self.assert_doesnt_exist("shadow.txt")
@@ -51,7 +56,7 @@ def test_file_exists(self):
5156
with pytest.raises(AssertionError, match=msg):
5257
self.assert_exists("shadow.txt")
5358

54-
def test_file_count(self):
59+
def test_file_count(self) -> None:
5560
self.make_file("abcde.txt", "abcde")
5661
self.make_file("axczz.txt", "axczz")
5762
self.make_file("afile.txt", "afile")
@@ -82,8 +87,8 @@ def test_file_count(self):
8287
with pytest.raises(AssertionError, match=msg):
8388
self.assert_file_count("*.q", 10)
8489

85-
def test_assert_recent_datetime(self):
86-
def now_delta(seconds):
90+
def test_assert_recent_datetime(self) -> None:
91+
def now_delta(seconds: int) -> datetime.datetime:
8792
"""Make a datetime `seconds` seconds from now."""
8893
return datetime.datetime.now() + datetime.timedelta(seconds=seconds)
8994

@@ -103,7 +108,7 @@ def now_delta(seconds):
103108
with pytest.raises(AssertionError):
104109
self.assert_recent_datetime(now_delta(1), seconds=120)
105110

106-
def test_assert_warnings(self):
111+
def test_assert_warnings(self) -> None:
107112
cov = coverage.Coverage()
108113

109114
# Make a warning, it should catch it properly.
@@ -152,7 +157,7 @@ def test_assert_warnings(self):
152157
with self.assert_warnings(cov, ["Hello there!"]):
153158
raise ZeroDivisionError("oops")
154159

155-
def test_assert_no_warnings(self):
160+
def test_assert_no_warnings(self) -> None:
156161
cov = coverage.Coverage()
157162

158163
# Happy path: no warnings.
@@ -165,7 +170,7 @@ def test_assert_no_warnings(self):
165170
with self.assert_warnings(cov, []):
166171
cov._warn("Watch out!")
167172

168-
def test_sub_python_is_this_python(self):
173+
def test_sub_python_is_this_python(self) -> None:
169174
# Try it with a Python command.
170175
self.set_environ('COV_FOOBAR', 'XYZZY')
171176
self.make_file("showme.py", """\
@@ -174,10 +179,10 @@ def test_sub_python_is_this_python(self):
174179
print(os.__file__)
175180
print(os.environ['COV_FOOBAR'])
176181
""")
177-
out = self.run_command("python showme.py").splitlines()
178-
assert actual_path(out[0]) == actual_path(sys.executable)
179-
assert out[1] == os.__file__
180-
assert out[2] == 'XYZZY'
182+
out_lines = self.run_command("python showme.py").splitlines()
183+
assert actual_path(out_lines[0]) == actual_path(sys.executable)
184+
assert out_lines[1] == os.__file__
185+
assert out_lines[2] == 'XYZZY'
181186

182187
# Try it with a "coverage debug sys" command.
183188
out = self.run_command("coverage debug sys")
@@ -191,7 +196,7 @@ def test_sub_python_is_this_python(self):
191196
_, _, environ = environ.rpartition(":")
192197
assert environ.strip() == "COV_FOOBAR = XYZZY"
193198

194-
def test_run_command_stdout_stderr(self):
199+
def test_run_command_stdout_stderr(self) -> None:
195200
# run_command should give us both stdout and stderr.
196201
self.make_file("outputs.py", """\
197202
import sys
@@ -202,7 +207,7 @@ def test_run_command_stdout_stderr(self):
202207
assert "StdOut\n" in out
203208
assert "StdErr\n" in out
204209

205-
def test_stdout(self):
210+
def test_stdout(self) -> None:
206211
# stdout is captured.
207212
print("This is stdout")
208213
print("Line 2")
@@ -219,14 +224,19 @@ class CheckUniqueFilenamesTest(CoverageTest):
219224

220225
class Stub:
221226
"""A stand-in for the class we're checking."""
222-
def __init__(self, x):
227+
def __init__(self, x: int) -> None:
223228
self.x = x
224229

225-
def method(self, filename, a=17, b="hello"):
230+
def method(
231+
self,
232+
filename: str,
233+
a: int = 17,
234+
b: str = "hello",
235+
) -> Tuple[int, str, int, str]:
226236
"""The method we'll wrap, with args to be sure args work."""
227237
return (self.x, filename, a, b)
228238

229-
def test_detect_duplicate(self):
239+
def test_detect_duplicate(self) -> None:
230240
stub = self.Stub(23)
231241
CheckUniqueFilenames.hook(stub, "method")
232242

@@ -259,7 +269,7 @@ def oops(x):
259269
ARCZ_MISSING = "3-2 78 8B"
260270
ARCZ_UNPREDICTED = "79"
261271

262-
def test_check_coverage_possible(self):
272+
def test_check_coverage_possible(self) -> None:
263273
msg = r"(?s)Possible arcs differ: .*- \(6, 3\).*\+ \(6, 7\)"
264274
with pytest.raises(AssertionError, match=msg):
265275
self.check_coverage(
@@ -269,7 +279,7 @@ def test_check_coverage_possible(self):
269279
arcz_unpredicted=self.ARCZ_UNPREDICTED,
270280
)
271281

272-
def test_check_coverage_missing(self):
282+
def test_check_coverage_missing(self) -> None:
273283
msg = r"(?s)Missing arcs differ: .*- \(3, 8\).*\+ \(7, 8\)"
274284
with pytest.raises(AssertionError, match=msg):
275285
self.check_coverage(
@@ -279,7 +289,7 @@ def test_check_coverage_missing(self):
279289
arcz_unpredicted=self.ARCZ_UNPREDICTED,
280290
)
281291

282-
def test_check_coverage_unpredicted(self):
292+
def test_check_coverage_unpredicted(self) -> None:
283293
msg = r"(?s)Unpredicted arcs differ: .*- \(3, 9\).*\+ \(7, 9\)"
284294
with pytest.raises(AssertionError, match=msg):
285295
self.check_coverage(
@@ -300,7 +310,7 @@ class ReLinesTest(CoverageTest):
300310
("[13]", "line1\nline2\nline3\n", "line1\nline3\n"),
301311
("X", "line1\nline2\nline3\n", ""),
302312
])
303-
def test_re_lines(self, pat, text, result):
313+
def test_re_lines(self, pat: str, text: str, result: str) -> None:
304314
assert re_lines_text(pat, text) == result
305315
assert re_lines(pat, text) == result.splitlines()
306316

@@ -309,26 +319,26 @@ def test_re_lines(self, pat, text, result):
309319
("[13]", "line1\nline2\nline3\n", "line2\n"),
310320
("X", "line1\nline2\nline3\n", "line1\nline2\nline3\n"),
311321
])
312-
def test_re_lines_inverted(self, pat, text, result):
322+
def test_re_lines_inverted(self, pat: str, text: str, result: str) -> None:
313323
assert re_lines_text(pat, text, match=False) == result
314324
assert re_lines(pat, text, match=False) == result.splitlines()
315325

316326
@pytest.mark.parametrize("pat, text, result", [
317327
("2", "line1\nline2\nline3\n", "line2"),
318328
])
319-
def test_re_line(self, pat, text, result):
329+
def test_re_line(self, pat: str, text: str, result: str) -> None:
320330
assert re_line(pat, text) == result
321331

322332
@pytest.mark.parametrize("pat, text", [
323333
("line", "line1\nline2\nline3\n"), # too many matches
324334
("X", "line1\nline2\nline3\n"), # no matches
325335
])
326-
def test_re_line_bad(self, pat, text):
336+
def test_re_line_bad(self, pat: str, text: str) -> None:
327337
with pytest.raises(AssertionError):
328338
re_line(pat, text)
329339

330340

331-
def _same_python_executable(e1, e2):
341+
def _same_python_executable(e1: str, e2: str) -> bool:
332342
"""Determine if `e1` and `e2` refer to the same Python executable.
333343
334344
Either path could include symbolic links. The two paths might not refer
@@ -365,7 +375,7 @@ class ArczTest(CoverageTest):
365375
("-11 12 2-5", [(-1, 1), (1, 2), (2, -5)]),
366376
("-QA CB IT Z-A", [(-26, 10), (12, 11), (18, 29), (35, -10)]),
367377
])
368-
def test_arcz_to_arcs(self, arcz, arcs):
378+
def test_arcz_to_arcs(self, arcz: str, arcs: List[TArc]) -> None:
369379
assert arcz_to_arcs(arcz) == arcs
370380

371381
@pytest.mark.parametrize("arcs, arcz_repr", [
@@ -382,58 +392,58 @@ def test_arcz_to_arcs(self, arcz, arcs):
382392
)
383393
),
384394
])
385-
def test_arcs_to_arcz_repr(self, arcs, arcz_repr):
395+
def test_arcs_to_arcz_repr(self, arcs: List[TArc], arcz_repr: str) -> None:
386396
assert arcs_to_arcz_repr(arcs) == arcz_repr
387397

388398

389399
class AssertCoverageWarningsTest(CoverageTest):
390400
"""Tests of assert_coverage_warnings"""
391401

392-
def test_one_warning(self):
402+
def test_one_warning(self) -> None:
393403
with pytest.warns(Warning) as warns:
394404
warnings.warn("Hello there", category=CoverageWarning)
395405
assert_coverage_warnings(warns, "Hello there")
396406

397-
def test_many_warnings(self):
407+
def test_many_warnings(self) -> None:
398408
with pytest.warns(Warning) as warns:
399409
warnings.warn("The first", category=CoverageWarning)
400410
warnings.warn("The second", category=CoverageWarning)
401411
warnings.warn("The third", category=CoverageWarning)
402412
assert_coverage_warnings(warns, "The first", "The second", "The third")
403413

404-
def test_wrong_type(self):
414+
def test_wrong_type(self) -> None:
405415
with pytest.warns(Warning) as warns:
406416
warnings.warn("Not ours", category=Warning)
407417
with pytest.raises(AssertionError):
408418
assert_coverage_warnings(warns, "Not ours")
409419

410-
def test_wrong_message(self):
420+
def test_wrong_message(self) -> None:
411421
with pytest.warns(Warning) as warns:
412422
warnings.warn("Goodbye", category=CoverageWarning)
413423
with pytest.raises(AssertionError):
414424
assert_coverage_warnings(warns, "Hello there")
415425

416-
def test_wrong_number_too_many(self):
426+
def test_wrong_number_too_many(self) -> None:
417427
with pytest.warns(Warning) as warns:
418428
warnings.warn("The first", category=CoverageWarning)
419429
warnings.warn("The second", category=CoverageWarning)
420430
with pytest.raises(AssertionError):
421431
assert_coverage_warnings(warns, "The first", "The second", "The third")
422432

423-
def test_wrong_number_too_few(self):
433+
def test_wrong_number_too_few(self) -> None:
424434
with pytest.warns(Warning) as warns:
425435
warnings.warn("The first", category=CoverageWarning)
426436
warnings.warn("The second", category=CoverageWarning)
427437
warnings.warn("The third", category=CoverageWarning)
428438
with pytest.raises(AssertionError):
429439
assert_coverage_warnings(warns, "The first", "The second")
430440

431-
def test_regex_matches(self):
441+
def test_regex_matches(self) -> None:
432442
with pytest.warns(Warning) as warns:
433443
warnings.warn("The first", category=CoverageWarning)
434444
assert_coverage_warnings(warns, re.compile("f?rst"))
435445

436-
def test_regex_doesnt_match(self):
446+
def test_regex_doesnt_match(self) -> None:
437447
with pytest.warns(Warning) as warns:
438448
warnings.warn("The first", category=CoverageWarning)
439449
with pytest.raises(AssertionError):

Diff for: tests/test_version.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
"""Tests of version.py."""
55

6+
from __future__ import annotations
7+
68
import coverage
79
from coverage.version import _make_url, _make_version
810

@@ -14,13 +16,13 @@ class VersionTest(CoverageTest):
1416

1517
run_in_temp_dir = False
1618

17-
def test_version_info(self):
19+
def test_version_info(self) -> None:
1820
# Make sure we didn't screw up the version_info tuple.
1921
assert isinstance(coverage.version_info, tuple)
2022
assert [type(d) for d in coverage.version_info] == [int, int, int, str, int]
2123
assert coverage.version_info[3] in {'alpha', 'beta', 'candidate', 'final'}
2224

23-
def test_make_version(self):
25+
def test_make_version(self) -> None:
2426
assert _make_version(4, 0, 0, 'alpha') == "4.0.0a0"
2527
assert _make_version(4, 0, 0, 'alpha', 1) == "4.0.0a1"
2628
assert _make_version(4, 0, 0, 'final') == "4.0.0"
@@ -30,7 +32,7 @@ def test_make_version(self):
3032
assert _make_version(5, 10, 2, 'candidate', 7) == "5.10.2rc7"
3133
assert _make_version(5, 10, 2, 'candidate', 7, 3) == "5.10.2rc7.dev3"
3234

33-
def test_make_url(self):
35+
def test_make_url(self) -> None:
3436
assert _make_url(4, 0, 0, 'final') == "https://coverage.readthedocs.io"
3537
expected = "https://coverage.readthedocs.io/en/4.1.2b3"
3638
assert _make_url(4, 1, 2, 'beta', 3) == expected

Diff for: tox.ini

+4-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ setenv =
106106
T3=tests/test_config.py tests/test_context.py tests/test_coverage.py tests/test_data.py tests/test_debug.py tests/test_execfile.py
107107
T4=tests/test_filereporter.py tests/test_files.py tests/test_goldtest.py tests/test_html.py tests/test_json.py tests/test_lcov.py
108108
T5=tests/test_misc.py tests/test_mixins.py tests/test_numbits.py tests/test_oddball.py tests/test_parser.py tests/test_phystokens.py
109-
T6=tests/test_process.py tests/test_python.py tests/test_report.py tests/test_results.py tests/test_setup.py tests/test_summary.py tests/test_xml.py
110-
# not done yet: test_plugins.py
111-
TYPEABLE={env:C1} {env:C2} {env:C3} {env:C4} {env:C5} {env:C6} {env:T1} {env:T2} {env:T3} {env:T4} {env:T5} {env:T6}
109+
T6=tests/test_process.py tests/test_python.py tests/test_report.py tests/test_results.py tests/test_setup.py
110+
T7=tests/test_summary.py tests/test_testing.py tests/test_version.py tests/test_xml.py
111+
# not done yet: test_plugins.py test_templite.py test_venv.py
112+
TYPEABLE={env:C1} {env:C2} {env:C3} {env:C4} {env:C5} {env:C6} {env:T1} {env:T2} {env:T3} {env:T4} {env:T5} {env:T6} {env:T7}
112113

113114
commands =
114115
# PYVERSIONS

0 commit comments

Comments
 (0)