Skip to content

Commit 0bbc032

Browse files
committed
[WIP] typing around terminal
1 parent 245e1f1 commit 0bbc032

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

src/_pytest/reports.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def getslaveinfoline(node):
3636

3737
class BaseReport:
3838
when = None # type: Optional[str]
39-
location = None
39+
location = None # type: Optional[Tuple[str, Optional[int], str]]
4040
longrepr = None
4141
sections = [] # type: List[Tuple[str, str]]
4242
nodeid = None # type: str
@@ -207,7 +207,7 @@ class TestReport(BaseReport):
207207
def __init__(
208208
self,
209209
nodeid,
210-
location,
210+
location: Tuple[str, Optional[int], str],
211211
keywords,
212212
outcome,
213213
longrepr,
@@ -216,14 +216,14 @@ def __init__(
216216
duration=0,
217217
user_properties=None,
218218
**extra
219-
):
219+
) -> None:
220220
#: normalized collection node id
221221
self.nodeid = nodeid
222222

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

+30-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,29 @@ 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) -> 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+
self.stats = {} # type: Dict[str, List[Any]]
245250
self.startdir = config.invocation_dir
246251
if file is None:
247252
file = sys.stdout
248253
self._tw = _pytest.config.create_terminal_writer(config, file)
249254
# self.writer will be deprecated in pytest-3.4
250255
self.writer = self._tw
251256
self._screen_width = self._tw.fullwidth
252-
self.currentfspath = None
257+
self.currentfspath = None # type: Optional[int]
253258
self.reportchars = getreportopt(config)
254259
self.hasmarkup = self._tw.hasmarkup
255260
self.isatty = file.isatty()
256261
self._progress_nodeids_reported = set() # type: Set[str]
257262
self._show_progress_info = self._determine_show_progress_info()
258-
self._collect_report_last_write = None
263+
self._collect_report_last_write = None # type: Optional[float]
259264

260265
def _determine_show_progress_info(self):
261266
"""Return True if we should display progress information based on the current config"""
@@ -400,7 +405,7 @@ def pytest_runtest_logstart(self, nodeid, location):
400405
fsid = nodeid.split("::")[0]
401406
self.write_fspath_result(fsid, "")
402407

403-
def pytest_runtest_logreport(self, report):
408+
def pytest_runtest_logreport(self, report: TestReport) -> None:
404409
self._tests_ran = True
405410
rep = report
406411
res = self.config.hook.pytest_report_teststatus(report=rep, config=self.config)
@@ -440,7 +445,7 @@ def pytest_runtest_logreport(self, report):
440445
self._write_progress_information_filling_space()
441446
else:
442447
self.ensure_newline()
443-
self._tw.write("[%s]" % rep.node.gateway.id)
448+
self._tw.write("[%s]" % rep.node.gateway.id) # type: ignore
444449
if self._show_progress_info:
445450
self._tw.write(
446451
self._get_progress_information_message() + " ", cyan=True
@@ -452,6 +457,7 @@ def pytest_runtest_logreport(self, report):
452457
self.currentfspath = -2
453458

454459
def pytest_runtest_logfinish(self, nodeid):
460+
assert self._session
455461
if self.verbosity <= 0 and self._show_progress_info:
456462
if self._show_progress_info == "count":
457463
num_tests = self._session.testscollected
@@ -474,7 +480,8 @@ def pytest_runtest_logfinish(self, nodeid):
474480
msg = self._get_progress_information_message()
475481
self._tw.write(msg + "\n", **{main_color: True})
476482

477-
def _get_progress_information_message(self):
483+
def _get_progress_information_message(self) -> str:
484+
assert self._session
478485
collected = self._session.testscollected
479486
if self._show_progress_info == "count":
480487
if collected:
@@ -485,8 +492,9 @@ def _get_progress_information_message(self):
485492
return " [ {} / {} ]".format(collected, collected)
486493
else:
487494
if collected:
488-
progress = len(self._progress_nodeids_reported) * 100 // collected
489-
return " [{:3d}%]".format(progress)
495+
return " [{:3d}%]".format(
496+
len(self._progress_nodeids_reported) * 100 // collected
497+
)
490498
return " [100%]"
491499

492500
def _write_progress_information_filling_space(self, color=None):
@@ -514,7 +522,7 @@ def pytest_collection(self):
514522
elif self.config.option.verbose >= 1:
515523
self.write("collecting ... ", bold=True)
516524

517-
def pytest_collectreport(self, report):
525+
def pytest_collectreport(self, report: CollectReport) -> None:
518526
if report.failed:
519527
self.stats.setdefault("error", []).append(report)
520528
elif report.skipped:
@@ -565,17 +573,18 @@ def report_collect(self, final=False):
565573
self.write_line(line)
566574

567575
@pytest.hookimpl(trylast=True)
568-
def pytest_sessionstart(self, session):
576+
def pytest_sessionstart(self, session: Session) -> None:
569577
self._session = session
570578
self._sessionstarttime = time.time()
571579
if not self.showheader:
572580
return
573581
self.write_sep("=", "test session starts", bold=True)
574582
verinfo = platform.python_version()
575583
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])
584+
pypy_version_info = getattr(sys, "pypy_version_info", None)
585+
if pypy_version_info:
586+
verinfo = ".".join(map(str, pypy_version_info[:3]))
587+
msg += "[pypy-{}-{}]".format(verinfo, pypy_version_info[3])
579588
msg += ", pytest-{}, py-{}, pluggy-{}".format(
580589
pytest.__version__, py.__version__, pluggy.__version__
581590
)
@@ -625,9 +634,10 @@ def pytest_collection_finish(self, session):
625634
self._write_report_lines_from_hooks(lines)
626635

627636
if self.config.getoption("collectonly"):
628-
if self.stats.get("failed"):
637+
failed = self.stats.get("failed")
638+
if failed:
629639
self._tw.sep("!", "collection failures")
630-
for rep in self.stats.get("failed"):
640+
for rep in failed:
631641
rep.toterminal(self._tw)
632642

633643
def _printcollecteditems(self, items):

0 commit comments

Comments
 (0)