1
1
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
2
2
from typing import Any
3
+ from typing import Dict
3
4
from typing import List
4
5
from typing import Mapping
5
6
from typing import Optional
37
38
from _pytest .python import Metafunc
38
39
from _pytest .python import Module
39
40
from _pytest .python import PyCollector
40
- from _pytest .reports import BaseReport
41
41
from _pytest .reports import CollectReport
42
42
from _pytest .reports import TestReport
43
43
from _pytest .runner import CallInfo
@@ -172,7 +172,7 @@ def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None:
172
172
173
173
174
174
@hookspec (firstresult = True )
175
- def pytest_cmdline_main (config : "Config" ) -> " Optional[Union[ExitCode, int]]" :
175
+ def pytest_cmdline_main (config : "Config" ) -> Optional [Union [" ExitCode" , int ]]:
176
176
""" called for performing the main command line action. The default
177
177
implementation will invoke the configure hooks and runtest_mainloop.
178
178
@@ -206,7 +206,7 @@ def pytest_load_initial_conftests(
206
206
207
207
208
208
@hookspec (firstresult = True )
209
- def pytest_collection (session : "Session" ) -> Optional [Any ]:
209
+ def pytest_collection (session : "Session" ) -> Optional [object ]:
210
210
"""Perform the collection protocol for the given session.
211
211
212
212
Stops at first non-None result, see :ref:`firstresult`.
@@ -242,39 +242,41 @@ def pytest_collection_modifyitems(
242
242
"""
243
243
244
244
245
- def pytest_collection_finish (session : "Session" ):
246
- """ called after collection has been performed and modified.
245
+ def pytest_collection_finish (session : "Session" ) -> None :
246
+ """Called after collection has been performed and modified.
247
247
248
248
:param _pytest.main.Session session: the pytest session object
249
249
"""
250
250
251
251
252
252
@hookspec (firstresult = True )
253
- def pytest_ignore_collect (path , config : "Config" ):
254
- """ return True to prevent considering this path for collection.
253
+ def pytest_ignore_collect (path : py .path .local , config : "Config" ) -> Optional [bool ]:
254
+ """Return True to prevent considering this path for collection.
255
+
255
256
This hook is consulted for all files and directories prior to calling
256
257
more specific hooks.
257
258
258
- Stops at first non-None result, see :ref:`firstresult`
259
+ Stops at first non-None result, see :ref:`firstresult`.
259
260
260
261
:param path: a :py:class:`py.path.local` - the path to analyze
261
262
:param _pytest.config.Config config: pytest config object
262
263
"""
263
264
264
265
265
266
@hookspec (firstresult = True , warn_on_impl = COLLECT_DIRECTORY_HOOK )
266
- def pytest_collect_directory (path , parent ):
267
- """ called before traversing a directory for collection files.
267
+ def pytest_collect_directory (path : py . path . local , parent ) -> Optional [ object ] :
268
+ """Called before traversing a directory for collection files.
268
269
269
- Stops at first non-None result, see :ref:`firstresult`
270
+ Stops at first non-None result, see :ref:`firstresult`.
270
271
271
272
:param path: a :py:class:`py.path.local` - the path to analyze
272
273
"""
273
274
274
275
275
276
def pytest_collect_file (path : py .path .local , parent ) -> "Optional[Collector]" :
276
- """ return collection Node or None for the given path. Any new node
277
- needs to have the specified ``parent`` as a parent.
277
+ """Return collection Node or None for the given path.
278
+
279
+ Any new node needs to have the specified ``parent`` as a parent.
278
280
279
281
:param path: a :py:class:`py.path.local` - the path to collect
280
282
"""
@@ -287,16 +289,16 @@ def pytest_collectstart(collector: "Collector") -> None:
287
289
""" collector starts collecting. """
288
290
289
291
290
- def pytest_itemcollected (item ) :
291
- """ we just collected a test item. """
292
+ def pytest_itemcollected (item : "Item" ) -> None :
293
+ """We just collected a test item."""
292
294
293
295
294
296
def pytest_collectreport (report : "CollectReport" ) -> None :
295
297
""" collector finished collecting. """
296
298
297
299
298
- def pytest_deselected (items ) :
299
- """ called for test items deselected , e.g. by keyword. """
300
+ def pytest_deselected (items : Sequence [ "Item" ]) -> None :
301
+ """Called for deselected test items, e.g. by keyword."""
300
302
301
303
302
304
@hookspec (firstresult = True )
@@ -312,25 +314,27 @@ def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectRepor
312
314
313
315
314
316
@hookspec (firstresult = True )
315
- def pytest_pycollect_makemodule (path : py .path .local , parent ) -> "Optional[Module]" :
316
- """ return a Module collector or None for the given path.
317
+ def pytest_pycollect_makemodule (path : py .path .local , parent ) -> Optional ["Module" ]:
318
+ """Return a Module collector or None for the given path.
319
+
317
320
This hook will be called for each matching test module path.
318
321
The pytest_collect_file hook needs to be used if you want to
319
322
create test modules for files that do not match as a test module.
320
323
321
- Stops at first non-None result, see :ref:`firstresult`
324
+ Stops at first non-None result, see :ref:`firstresult`.
322
325
323
326
:param path: a :py:class:`py.path.local` - the path of module to collect
324
327
"""
325
328
326
329
327
330
@hookspec (firstresult = True )
328
331
def pytest_pycollect_makeitem (
329
- collector : "PyCollector" , name : str , obj
330
- ) -> " Union[None, Item, Collector, List[Union[Item, Collector]]]" :
331
- """ return custom item/collector for a python object in a module, or None.
332
+ collector : "PyCollector" , name : str , obj : object
333
+ ) -> Union [None , " Item" , " Collector" , List [Union [" Item" , " Collector" ]]]:
334
+ """Return a custom item/collector for a Python object in a module, or None.
332
335
333
- Stops at first non-None result, see :ref:`firstresult` """
336
+ Stops at first non-None result, see :ref:`firstresult`.
337
+ """
334
338
335
339
336
340
@hookspec (firstresult = True )
@@ -466,7 +470,7 @@ def pytest_runtest_call(item: "Item") -> None:
466
470
"""
467
471
468
472
469
- def pytest_runtest_teardown (item : "Item" , nextitem : " Optional[Item]" ) -> None :
473
+ def pytest_runtest_teardown (item : "Item" , nextitem : Optional [" Item" ] ) -> None :
470
474
"""Called to perform the teardown phase for a test item.
471
475
472
476
The default implementation runs the finalizers and calls ``teardown()``
@@ -505,15 +509,19 @@ def pytest_runtest_logreport(report: "TestReport") -> None:
505
509
506
510
507
511
@hookspec (firstresult = True )
508
- def pytest_report_to_serializable (config : "Config" , report : "BaseReport" ):
512
+ def pytest_report_to_serializable (
513
+ config : "Config" , report : Union ["CollectReport" , "TestReport" ],
514
+ ) -> Optional [Dict [str , Any ]]:
509
515
"""
510
516
Serializes the given report object into a data structure suitable for sending
511
517
over the wire, e.g. converted to JSON.
512
518
"""
513
519
514
520
515
521
@hookspec (firstresult = True )
516
- def pytest_report_from_serializable (config : "Config" , data ):
522
+ def pytest_report_from_serializable (
523
+ config : "Config" , data : Dict [str , Any ],
524
+ ) -> Optional [Union ["CollectReport" , "TestReport" ]]:
517
525
"""
518
526
Restores a report object previously serialized with pytest_report_to_serializable().
519
527
"""
@@ -528,11 +536,11 @@ def pytest_report_from_serializable(config: "Config", data):
528
536
def pytest_fixture_setup (
529
537
fixturedef : "FixtureDef" , request : "SubRequest"
530
538
) -> Optional [object ]:
531
- """ performs fixture setup execution.
539
+ """Performs fixture setup execution.
532
540
533
- :return: The return value of the call to the fixture function
541
+ :return: The return value of the call to the fixture function.
534
542
535
- Stops at first non-None result, see :ref:`firstresult`
543
+ Stops at first non-None result, see :ref:`firstresult`.
536
544
537
545
.. note::
538
546
If the fixture function returns None, other implementations of
@@ -555,25 +563,25 @@ def pytest_fixture_post_finalizer(
555
563
556
564
557
565
def pytest_sessionstart (session : "Session" ) -> None :
558
- """ called after the ``Session`` object has been created and before performing collection
566
+ """Called after the ``Session`` object has been created and before performing collection
559
567
and entering the run test loop.
560
568
561
569
:param _pytest.main.Session session: the pytest session object
562
570
"""
563
571
564
572
565
573
def pytest_sessionfinish (
566
- session : "Session" , exitstatus : " Union[int, ExitCode]"
574
+ session : "Session" , exitstatus : Union [int , " ExitCode" ],
567
575
) -> None :
568
- """ called after whole test run finished, right before returning the exit status to the system.
576
+ """Called after whole test run finished, right before returning the exit status to the system.
569
577
570
578
:param _pytest.main.Session session: the pytest session object
571
579
:param int exitstatus: the status which pytest will return to the system
572
580
"""
573
581
574
582
575
583
def pytest_unconfigure (config : "Config" ) -> None :
576
- """ called before test process is exited.
584
+ """Called before test process is exited.
577
585
578
586
:param _pytest.config.Config config: pytest config object
579
587
"""
@@ -587,7 +595,7 @@ def pytest_unconfigure(config: "Config") -> None:
587
595
def pytest_assertrepr_compare (
588
596
config : "Config" , op : str , left : object , right : object
589
597
) -> Optional [List [str ]]:
590
- """return explanation for comparisons in failing assert expressions.
598
+ """Return explanation for comparisons in failing assert expressions.
591
599
592
600
Return None for no custom explanation, otherwise return a list
593
601
of strings. The strings will be joined by newlines but any newlines
@@ -598,7 +606,7 @@ def pytest_assertrepr_compare(
598
606
"""
599
607
600
608
601
- def pytest_assertion_pass (item , lineno : int , orig : str , expl : str ) -> None :
609
+ def pytest_assertion_pass (item : "Item" , lineno : int , orig : str , expl : str ) -> None :
602
610
"""
603
611
**(Experimental)**
604
612
@@ -665,12 +673,12 @@ def pytest_report_header(
665
673
666
674
667
675
def pytest_report_collectionfinish (
668
- config : "Config" , startdir : py .path .local , items : " Sequence[Item]"
676
+ config : "Config" , startdir : py .path .local , items : Sequence [" Item" ],
669
677
) -> Union [str , List [str ]]:
670
678
"""
671
679
.. versionadded:: 3.2
672
680
673
- return a string or list of strings to be displayed after collection has finished successfully.
681
+ Return a string or list of strings to be displayed after collection has finished successfully.
674
682
675
683
These strings will be displayed after the standard "collected X items" message.
676
684
@@ -689,7 +697,7 @@ def pytest_report_collectionfinish(
689
697
690
698
@hookspec (firstresult = True )
691
699
def pytest_report_teststatus (
692
- report : "BaseReport" , config : "Config"
700
+ report : Union [ "CollectReport" , "TestReport" ] , config : "Config"
693
701
) -> Tuple [
694
702
str , str , Union [str , Mapping [str , bool ]],
695
703
]:
@@ -734,7 +742,7 @@ def pytest_terminal_summary(
734
742
def pytest_warning_captured (
735
743
warning_message : "warnings.WarningMessage" ,
736
744
when : "Literal['config', 'collect', 'runtest']" ,
737
- item : " Optional[Item]" ,
745
+ item : Optional [" Item" ] ,
738
746
location : Optional [Tuple [str , int , str ]],
739
747
) -> None :
740
748
"""(**Deprecated**) Process a warning captured by the internal pytest warnings plugin.
@@ -797,18 +805,6 @@ def pytest_warning_recorded(
797
805
"""
798
806
799
807
800
- # -------------------------------------------------------------------------
801
- # doctest hooks
802
- # -------------------------------------------------------------------------
803
-
804
-
805
- @hookspec (firstresult = True )
806
- def pytest_doctest_prepare_content (content ):
807
- """ return processed content for a given doctest
808
-
809
- Stops at first non-None result, see :ref:`firstresult` """
810
-
811
-
812
808
# -------------------------------------------------------------------------
813
809
# error handling and internal debugging hooks
814
810
# -------------------------------------------------------------------------
@@ -831,7 +827,9 @@ def pytest_keyboard_interrupt(
831
827
832
828
833
829
def pytest_exception_interact (
834
- node : "Node" , call : "CallInfo[object]" , report : "Union[CollectReport, TestReport]"
830
+ node : "Node" ,
831
+ call : "CallInfo[object]" ,
832
+ report : Union ["CollectReport" , "TestReport" ],
835
833
) -> None :
836
834
"""Called when an exception was raised which can potentially be
837
835
interactively handled.
0 commit comments