9
9
import sys
10
10
import time
11
11
from functools import partial
12
+ from typing import Any
12
13
from typing import Callable
13
14
from typing import Dict
14
15
from typing import List
24
25
25
26
import pytest
26
27
from _pytest import nodes
28
+ from _pytest .config import Config
27
29
from _pytest .main import ExitCode
30
+ from _pytest .main import Session
31
+ from _pytest .reports import CollectReport
32
+ from _pytest .reports import TestReport
28
33
29
34
REPORT_COLLECTING_RESOLUTION = 0.5
30
35
@@ -148,7 +153,7 @@ def pytest_addoption(parser):
148
153
)
149
154
150
155
151
- def pytest_configure (config ) :
156
+ def pytest_configure (config : Config ) -> None :
152
157
reporter = TerminalReporter (config , sys .stdout )
153
158
config .pluginmanager .register (reporter , "terminalreporter" )
154
159
if config .option .debug or config .option .traceconfig :
@@ -160,7 +165,7 @@ def mywriter(tags, args):
160
165
config .trace .root .setprocessor ("pytest:config" , mywriter )
161
166
162
167
163
- def getreportopt (config ) :
168
+ def getreportopt (config : Config ) -> str :
164
169
reportopts = ""
165
170
reportchars = config .option .reportchars
166
171
if not config .option .disable_warnings and "w" not in reportchars :
@@ -179,7 +184,7 @@ def getreportopt(config):
179
184
180
185
181
186
@pytest .hookimpl (trylast = True ) # after _pytest.runner
182
- def pytest_report_teststatus (report ) :
187
+ def pytest_report_teststatus (report : TestReport ) -> Tuple [ str , str , str ] :
183
188
if report .passed :
184
189
letter = "."
185
190
elif report .skipped :
@@ -233,29 +238,29 @@ def get_location(self, config):
233
238
234
239
235
240
class TerminalReporter :
236
- def __init__ (self , config , file = None ):
241
+ def __init__ (self , config : Config , file = None ) -> None :
237
242
import _pytest .config
238
243
239
244
self .config = config
240
245
self ._numcollected = 0
241
- self ._session = None
246
+ self ._session = None # type: Optional[Session]
242
247
self ._showfspath = None
243
248
244
- self .stats = {}
249
+ self .stats = {} # type: Dict[str, List[Any]]
245
250
self .startdir = config .invocation_dir
246
251
if file is None :
247
252
file = sys .stdout
248
253
self ._tw = _pytest .config .create_terminal_writer (config , file )
249
254
# self.writer will be deprecated in pytest-3.4
250
255
self .writer = self ._tw
251
256
self ._screen_width = self ._tw .fullwidth
252
- self .currentfspath = None
257
+ self .currentfspath = None # type: Optional[int]
253
258
self .reportchars = getreportopt (config )
254
259
self .hasmarkup = self ._tw .hasmarkup
255
260
self .isatty = file .isatty ()
256
261
self ._progress_nodeids_reported = set () # type: Set[str]
257
262
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]
259
264
260
265
def _determine_show_progress_info (self ):
261
266
"""Return True if we should display progress information based on the current config"""
@@ -400,7 +405,7 @@ def pytest_runtest_logstart(self, nodeid, location):
400
405
fsid = nodeid .split ("::" )[0 ]
401
406
self .write_fspath_result (fsid , "" )
402
407
403
- def pytest_runtest_logreport (self , report ) :
408
+ def pytest_runtest_logreport (self , report : TestReport ) -> None :
404
409
self ._tests_ran = True
405
410
rep = report
406
411
res = self .config .hook .pytest_report_teststatus (report = rep , config = self .config )
@@ -440,7 +445,7 @@ def pytest_runtest_logreport(self, report):
440
445
self ._write_progress_information_filling_space ()
441
446
else :
442
447
self .ensure_newline ()
443
- self ._tw .write ("[%s]" % rep .node .gateway .id )
448
+ self ._tw .write ("[%s]" % rep .node .gateway .id ) # type: ignore
444
449
if self ._show_progress_info :
445
450
self ._tw .write (
446
451
self ._get_progress_information_message () + " " , cyan = True
@@ -452,6 +457,7 @@ def pytest_runtest_logreport(self, report):
452
457
self .currentfspath = - 2
453
458
454
459
def pytest_runtest_logfinish (self , nodeid ):
460
+ assert self ._session
455
461
if self .verbosity <= 0 and self ._show_progress_info :
456
462
if self ._show_progress_info == "count" :
457
463
num_tests = self ._session .testscollected
@@ -474,7 +480,8 @@ def pytest_runtest_logfinish(self, nodeid):
474
480
msg = self ._get_progress_information_message ()
475
481
self ._tw .write (msg + "\n " , ** {main_color : True })
476
482
477
- def _get_progress_information_message (self ):
483
+ def _get_progress_information_message (self ) -> str :
484
+ assert self ._session
478
485
collected = self ._session .testscollected
479
486
if self ._show_progress_info == "count" :
480
487
if collected :
@@ -485,8 +492,9 @@ def _get_progress_information_message(self):
485
492
return " [ {} / {} ]" .format (collected , collected )
486
493
else :
487
494
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
+ )
490
498
return " [100%]"
491
499
492
500
def _write_progress_information_filling_space (self , color = None ):
@@ -514,7 +522,7 @@ def pytest_collection(self):
514
522
elif self .config .option .verbose >= 1 :
515
523
self .write ("collecting ... " , bold = True )
516
524
517
- def pytest_collectreport (self , report ) :
525
+ def pytest_collectreport (self , report : CollectReport ) -> None :
518
526
if report .failed :
519
527
self .stats .setdefault ("error" , []).append (report )
520
528
elif report .skipped :
@@ -565,17 +573,18 @@ def report_collect(self, final=False):
565
573
self .write_line (line )
566
574
567
575
@pytest .hookimpl (trylast = True )
568
- def pytest_sessionstart (self , session ) :
576
+ def pytest_sessionstart (self , session : Session ) -> None :
569
577
self ._session = session
570
578
self ._sessionstarttime = time .time ()
571
579
if not self .showheader :
572
580
return
573
581
self .write_sep ("=" , "test session starts" , bold = True )
574
582
verinfo = platform .python_version ()
575
583
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 ])
579
588
msg += ", pytest-{}, py-{}, pluggy-{}" .format (
580
589
pytest .__version__ , py .__version__ , pluggy .__version__
581
590
)
@@ -625,9 +634,10 @@ def pytest_collection_finish(self, session):
625
634
self ._write_report_lines_from_hooks (lines )
626
635
627
636
if self .config .getoption ("collectonly" ):
628
- if self .stats .get ("failed" ):
637
+ failed = self .stats .get ("failed" )
638
+ if failed :
629
639
self ._tw .sep ("!" , "collection failures" )
630
- for rep in self . stats . get ( " failed" ) :
640
+ for rep in failed :
631
641
rep .toterminal (self ._tw )
632
642
633
643
def _printcollecteditems (self , items ):
0 commit comments