Skip to content

Commit 62ddf7a

Browse files
committed
resultlog: add missing type annotations
1 parent 303030c commit 62ddf7a

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/_pytest/resultlog.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""log machine-parseable test session result information to a plain text file."""
22
import os
3+
from typing import IO
4+
from typing import Union
35

46
import py
57

@@ -52,16 +54,18 @@ def pytest_unconfigure(config: Config) -> None:
5254

5355

5456
class ResultLog:
55-
def __init__(self, config, logfile):
57+
def __init__(self, config: Config, logfile: IO[str]) -> None:
5658
self.config = config
5759
self.logfile = logfile # preferably line buffered
5860

59-
def write_log_entry(self, testpath, lettercode, longrepr):
61+
def write_log_entry(self, testpath: str, lettercode: str, longrepr: str) -> None:
6062
print("{} {}".format(lettercode, testpath), file=self.logfile)
6163
for line in longrepr.splitlines():
6264
print(" %s" % line, file=self.logfile)
6365

64-
def log_outcome(self, report, lettercode, longrepr):
66+
def log_outcome(
67+
self, report: Union[TestReport, CollectReport], lettercode: str, longrepr: str
68+
) -> None:
6569
testpath = getattr(report, "nodeid", None)
6670
if testpath is None:
6771
testpath = report.fspath
@@ -73,7 +77,7 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
7377
res = self.config.hook.pytest_report_teststatus(
7478
report=report, config=self.config
7579
)
76-
code = res[1]
80+
code = res[1] # type: str
7781
if code == "x":
7882
longrepr = str(report.longrepr)
7983
elif code == "X":

testing/test_resultlog.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import os
22
from io import StringIO
3+
from typing import List
34

45
import _pytest._code
56
import pytest
7+
from _pytest.pytester import Testdir
68
from _pytest.resultlog import pytest_configure
79
from _pytest.resultlog import pytest_unconfigure
810
from _pytest.resultlog import ResultLog
911
from _pytest.resultlog import resultlog_key
1012

13+
1114
pytestmark = pytest.mark.filterwarnings("ignore:--result-log is deprecated")
1215

1316

14-
def test_write_log_entry():
15-
reslog = ResultLog(None, None)
17+
def test_write_log_entry() -> None:
18+
reslog = ResultLog(None, None) # type: ignore[arg-type]
1619
reslog.logfile = StringIO()
1720
reslog.write_log_entry("name", ".", "")
1821
entry = reslog.logfile.getvalue()
@@ -54,14 +57,14 @@ class TestWithFunctionIntegration:
5457
# XXX (hpk) i think that the resultlog plugin should
5558
# provide a Parser object so that one can remain
5659
# ignorant regarding formatting details.
57-
def getresultlog(self, testdir, arg):
60+
def getresultlog(self, testdir: Testdir, arg: str) -> List[str]:
5861
resultlog = testdir.tmpdir.join("resultlog")
5962
testdir.plugins.append("resultlog")
6063
args = ["--resultlog=%s" % resultlog] + [arg]
6164
testdir.runpytest(*args)
6265
return [x for x in resultlog.readlines(cr=0) if x]
6366

64-
def test_collection_report(self, testdir):
67+
def test_collection_report(self, testdir: Testdir) -> None:
6568
ok = testdir.makepyfile(test_collection_ok="")
6669
fail = testdir.makepyfile(test_collection_fail="XXX")
6770
lines = self.getresultlog(testdir, ok)
@@ -75,7 +78,7 @@ def test_collection_report(self, testdir):
7578
assert x.startswith(" ")
7679
assert "XXX" in "".join(lines[1:])
7780

78-
def test_log_test_outcomes(self, testdir):
81+
def test_log_test_outcomes(self, testdir: Testdir) -> None:
7982
mod = testdir.makepyfile(
8083
test_mod="""
8184
import pytest
@@ -111,16 +114,17 @@ def test_xpass(): pass
111114
assert len(lines) == 15
112115

113116
@pytest.mark.parametrize("style", ("native", "long", "short"))
114-
def test_internal_exception(self, style):
117+
def test_internal_exception(self, style) -> None:
115118
# they are produced for example by a teardown failing
116119
# at the end of the run or a failing hook invocation
117120
try:
118121
raise ValueError
119122
except ValueError:
120123
excinfo = _pytest._code.ExceptionInfo.from_current()
121-
reslog = ResultLog(None, StringIO())
124+
file = StringIO()
125+
reslog = ResultLog(None, file) # type: ignore[arg-type]
122126
reslog.pytest_internalerror(excinfo.getrepr(style=style))
123-
entry = reslog.logfile.getvalue()
127+
entry = file.getvalue()
124128
entry_lines = entry.splitlines()
125129

126130
assert entry_lines[0].startswith("! ")
@@ -130,7 +134,7 @@ def test_internal_exception(self, style):
130134
assert "ValueError" in entry
131135

132136

133-
def test_generic(testdir, LineMatcher):
137+
def test_generic(testdir: Testdir, LineMatcher) -> None:
134138
testdir.plugins.append("resultlog")
135139
testdir.makepyfile(
136140
"""
@@ -162,7 +166,7 @@ def test_xfail_norun():
162166
)
163167

164168

165-
def test_makedir_for_resultlog(testdir, LineMatcher):
169+
def test_makedir_for_resultlog(testdir: Testdir, LineMatcher) -> None:
166170
"""--resultlog should automatically create directories for the log file"""
167171
testdir.plugins.append("resultlog")
168172
testdir.makepyfile(
@@ -177,7 +181,7 @@ def test_pass():
177181
LineMatcher(lines).fnmatch_lines([". *:test_pass"])
178182

179183

180-
def test_no_resultlog_on_workers(testdir):
184+
def test_no_resultlog_on_workers(testdir: Testdir) -> None:
181185
config = testdir.parseconfig("-p", "resultlog", "--resultlog=resultlog")
182186

183187
assert resultlog_key not in config._store
@@ -186,14 +190,14 @@ def test_no_resultlog_on_workers(testdir):
186190
pytest_unconfigure(config)
187191
assert resultlog_key not in config._store
188192

189-
config.workerinput = {}
193+
config.workerinput = {} # type: ignore[attr-defined]
190194
pytest_configure(config)
191195
assert resultlog_key not in config._store
192196
pytest_unconfigure(config)
193197
assert resultlog_key not in config._store
194198

195199

196-
def test_unknown_teststatus(testdir):
200+
def test_unknown_teststatus(testdir: Testdir) -> None:
197201
"""Ensure resultlog correctly handles unknown status from pytest_report_teststatus
198202
199203
Inspired on pytest-rerunfailures.
@@ -229,7 +233,7 @@ def pytest_runtest_makereport():
229233
assert lines[0] == "r test_unknown_teststatus.py::test"
230234

231235

232-
def test_failure_issue380(testdir):
236+
def test_failure_issue380(testdir: Testdir) -> None:
233237
testdir.makeconftest(
234238
"""
235239
import pytest

0 commit comments

Comments
 (0)