Skip to content

Commit d463b69

Browse files
committed
[WIP] typing around terminal
1 parent 0489104 commit d463b69

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

src/_pytest/reports.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def __init__(
223223
#: a (filesystempath, lineno, domaininfo) tuple indicating the
224224
#: actual location of a test item - it might be different from the
225225
#: collected one e.g. if a method is inherited from a different module.
226-
self.location = location
226+
self.location = location # type: Tuple[str, Optional[int], str]
227227

228228
#: a name -> value dictionary containing all keywords and
229229
#: markers associated with a test invocation.

src/_pytest/terminal.py

+32-20
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import time
1111
from functools import partial
12+
from typing import Any
1213
from typing import Callable
1314
from typing import Dict
1415
from typing import List
@@ -24,7 +25,11 @@
2425

2526
import pytest
2627
from _pytest import nodes
28+
from _pytest.config import Config
2729
from _pytest.main import ExitCode
30+
from _pytest.main import Session
31+
from _pytest.reports import CollectReport
32+
from _pytest.reports import TestReport
2833

2934
REPORT_COLLECTING_RESOLUTION = 0.5
3035

@@ -148,7 +153,7 @@ def pytest_addoption(parser):
148153
)
149154

150155

151-
def pytest_configure(config):
156+
def pytest_configure(config: Config) -> None:
152157
reporter = TerminalReporter(config, sys.stdout)
153158
config.pluginmanager.register(reporter, "terminalreporter")
154159
if config.option.debug or config.option.traceconfig:
@@ -160,7 +165,7 @@ def mywriter(tags, args):
160165
config.trace.root.setprocessor("pytest:config", mywriter)
161166

162167

163-
def getreportopt(config):
168+
def getreportopt(config: Config) -> str:
164169
reportopts = ""
165170
reportchars = config.option.reportchars
166171
if not config.option.disable_warnings and "w" not in reportchars:
@@ -179,7 +184,7 @@ def getreportopt(config):
179184

180185

181186
@pytest.hookimpl(trylast=True) # after _pytest.runner
182-
def pytest_report_teststatus(report):
187+
def pytest_report_teststatus(report: TestReport) -> Tuple[str, str, str]:
183188
if report.passed:
184189
letter = "."
185190
elif report.skipped:
@@ -233,29 +238,30 @@ def get_location(self, config):
233238

234239

235240
class TerminalReporter:
236-
def __init__(self, config, file=None):
241+
def __init__(self, config: Config, file=None):
237242
import _pytest.config
238243

239244
self.config = config
240245
self._numcollected = 0
241-
self._session = None
246+
self._session = None # type: Optional[Session]
242247
self._showfspath = None
243248

244-
self.stats = {}
249+
# XXX: needs TypedDict? warnings => WarningReport, others => BaseReport
250+
self.stats = {} # type: Dict[str, List[Any]]
245251
self.startdir = config.invocation_dir
246252
if file is None:
247253
file = sys.stdout
248254
self._tw = _pytest.config.create_terminal_writer(config, file)
249255
# self.writer will be deprecated in pytest-3.4
250256
self.writer = self._tw
251257
self._screen_width = self._tw.fullwidth
252-
self.currentfspath = None
258+
self.currentfspath = None # type: Optional[int]
253259
self.reportchars = getreportopt(config)
254260
self.hasmarkup = self._tw.hasmarkup
255261
self.isatty = file.isatty()
256262
self._progress_nodeids_reported = set() # type: Set[str]
257263
self._show_progress_info = self._determine_show_progress_info()
258-
self._collect_report_last_write = None
264+
self._collect_report_last_write = None # type: Optional[float]
259265

260266
def _determine_show_progress_info(self):
261267
"""Return True if we should display progress information based on the current config"""
@@ -400,7 +406,7 @@ def pytest_runtest_logstart(self, nodeid, location):
400406
fsid = nodeid.split("::")[0]
401407
self.write_fspath_result(fsid, "")
402408

403-
def pytest_runtest_logreport(self, report):
409+
def pytest_runtest_logreport(self, report: TestReport) -> None:
404410
self._tests_ran = True
405411
rep = report
406412
res = self.config.hook.pytest_report_teststatus(report=rep, config=self.config)
@@ -440,7 +446,7 @@ def pytest_runtest_logreport(self, report):
440446
self._write_progress_information_filling_space()
441447
else:
442448
self.ensure_newline()
443-
self._tw.write("[%s]" % rep.node.gateway.id)
449+
self._tw.write("[%s]" % rep.node.gateway.id) # type: ignore
444450
if self._show_progress_info:
445451
self._tw.write(
446452
self._get_progress_information_message() + " ", cyan=True
@@ -452,6 +458,7 @@ def pytest_runtest_logreport(self, report):
452458
self.currentfspath = -2
453459

454460
def pytest_runtest_logfinish(self, nodeid):
461+
assert self._session
455462
if self.verbosity <= 0 and self._show_progress_info:
456463
if self._show_progress_info == "count":
457464
num_tests = self._session.testscollected
@@ -474,7 +481,8 @@ def pytest_runtest_logfinish(self, nodeid):
474481
msg = self._get_progress_information_message()
475482
self._tw.write(msg + "\n", **{main_color: True})
476483

477-
def _get_progress_information_message(self):
484+
def _get_progress_information_message(self) -> str:
485+
assert self._session
478486
collected = self._session.testscollected
479487
if self._show_progress_info == "count":
480488
if collected:
@@ -485,8 +493,9 @@ def _get_progress_information_message(self):
485493
return " [ {} / {} ]".format(collected, collected)
486494
else:
487495
if collected:
488-
progress = len(self._progress_nodeids_reported) * 100 // collected
489-
return " [{:3d}%]".format(progress)
496+
return " [{:3d}%]".format(
497+
len(self._progress_nodeids_reported) * 100 // collected
498+
)
490499
return " [100%]"
491500

492501
def _write_progress_information_filling_space(self, color=None):
@@ -514,7 +523,7 @@ def pytest_collection(self):
514523
elif self.config.option.verbose >= 1:
515524
self.write("collecting ... ", bold=True)
516525

517-
def pytest_collectreport(self, report):
526+
def pytest_collectreport(self, report: CollectReport):
518527
if report.failed:
519528
self.stats.setdefault("error", []).append(report)
520529
elif report.skipped:
@@ -565,17 +574,18 @@ def report_collect(self, final=False):
565574
self.write_line(line)
566575

567576
@pytest.hookimpl(trylast=True)
568-
def pytest_sessionstart(self, session):
577+
def pytest_sessionstart(self, session: Session) -> None:
569578
self._session = session
570579
self._sessionstarttime = time.time()
571580
if not self.showheader:
572581
return
573582
self.write_sep("=", "test session starts", bold=True)
574583
verinfo = platform.python_version()
575584
msg = "platform {} -- Python {}".format(sys.platform, verinfo)
576-
if hasattr(sys, "pypy_version_info"):
577-
verinfo = ".".join(map(str, sys.pypy_version_info[:3]))
578-
msg += "[pypy-{}-{}]".format(verinfo, sys.pypy_version_info[3])
585+
pypy_version_info = getattr(sys, "pypy_version_info")
586+
if pypy_version_info:
587+
verinfo = ".".join(map(str, pypy_version_info[:3]))
588+
msg += "[pypy-{}-{}]".format(verinfo, pypy_version_info[3])
579589
msg += ", pytest-{}, py-{}, pluggy-{}".format(
580590
pytest.__version__, py.__version__, pluggy.__version__
581591
)
@@ -625,9 +635,10 @@ def pytest_collection_finish(self, session):
625635
self._write_report_lines_from_hooks(lines)
626636

627637
if self.config.getoption("collectonly"):
628-
if self.stats.get("failed"):
638+
failed = self.stats.get("failed")
639+
if failed:
629640
self._tw.sep("!", "collection failures")
630-
for rep in self.stats.get("failed"):
641+
for rep in failed:
631642
rep.toterminal(self._tw)
632643

633644
def _printcollecteditems(self, items):
@@ -767,6 +778,7 @@ def getreports(self, name):
767778

768779
def summary_warnings(self):
769780
if self.hasopt("w"):
781+
# XXX: this needs to be fixed
770782
all_warnings = self.stats.get(
771783
"warnings"
772784
) # type: Optional[List[WarningReport]]

0 commit comments

Comments
 (0)