9
9
import warnings
10
10
from functools import lru_cache
11
11
from pathlib import Path
12
+ from types import TracebackType
13
+ from typing import Any
14
+ from typing import Callable
15
+ from typing import Dict
16
+ from typing import List
17
+ from typing import Optional
18
+ from typing import Sequence
19
+ from typing import Set
20
+ from typing import Tuple
12
21
13
22
import attr
14
23
import py
32
41
from _pytest .outcomes import Skipped
33
42
from _pytest .warning_types import PytestConfigWarning
34
43
44
+ if False : # TYPE_CHECKING
45
+ from typing import Type
46
+
47
+
35
48
hookimpl = HookimplMarker ("pytest" )
36
49
hookspec = HookspecMarker ("pytest" )
37
50
@@ -40,7 +53,7 @@ class ConftestImportFailure(Exception):
40
53
def __init__ (self , path , excinfo ):
41
54
Exception .__init__ (self , path , excinfo )
42
55
self .path = path
43
- self .excinfo = excinfo
56
+ self .excinfo = excinfo # type: Tuple[Type[Exception], Exception, TracebackType]
44
57
45
58
46
59
def main (args = None , plugins = None ):
@@ -237,14 +250,18 @@ class PytestPluginManager(PluginManager):
237
250
238
251
def __init__ (self ):
239
252
super ().__init__ ("pytest" )
240
- self ._conftest_plugins = set ()
253
+ # The objects are module objects, only used generically.
254
+ self ._conftest_plugins = set () # type: Set[object]
241
255
242
256
# state related to local conftest plugins
243
- self ._dirpath2confmods = {}
244
- self ._conftestpath2mod = {}
257
+ # Maps a py.path.local to a list of module objects.
258
+ self ._dirpath2confmods = {} # type: Dict[Any, List[object]]
259
+ # Maps a py.path.local to a module object.
260
+ self ._conftestpath2mod = {} # type: Dict[Any, object]
245
261
self ._confcutdir = None
246
262
self ._noconftest = False
247
- self ._duplicatepaths = set ()
263
+ # Set of py.path.local's.
264
+ self ._duplicatepaths = set () # type: Set[Any]
248
265
249
266
self .add_hookspecs (_pytest .hookspec )
250
267
self .register (self )
@@ -653,7 +670,7 @@ class InvocationParams:
653
670
654
671
args = attr .ib ()
655
672
plugins = attr .ib ()
656
- dir = attr .ib ()
673
+ dir = attr .ib (type = Path )
657
674
658
675
def __init__ (self , pluginmanager , * , invocation_params = None ):
659
676
from .argparsing import Parser , FILE_OR_DIR
@@ -674,10 +691,10 @@ def __init__(self, pluginmanager, *, invocation_params=None):
674
691
self .pluginmanager = pluginmanager
675
692
self .trace = self .pluginmanager .trace .root .get ("config" )
676
693
self .hook = self .pluginmanager .hook
677
- self ._inicache = {}
678
- self ._override_ini = ()
679
- self ._opt2dest = {}
680
- self ._cleanup = []
694
+ self ._inicache = {} # type: Dict[str, Any]
695
+ self ._override_ini = () # type: Sequence[str]
696
+ self ._opt2dest = {} # type: Dict[str, str]
697
+ self ._cleanup = [] # type: List[Callable[[], None]]
681
698
self .pluginmanager .register (self , "pytestconfig" )
682
699
self ._configured = False
683
700
self .hook .pytest_addoption .call_historic (kwargs = dict (parser = self ._parser ))
@@ -776,7 +793,7 @@ def _processopt(self, opt):
776
793
def pytest_load_initial_conftests (self , early_config ):
777
794
self .pluginmanager ._set_initial_conftests (early_config .known_args_namespace )
778
795
779
- def _initini (self , args ):
796
+ def _initini (self , args ) -> None :
780
797
ns , unknown_args = self ._parser .parse_known_and_unknown_args (
781
798
args , namespace = copy .copy (self .option )
782
799
)
@@ -877,8 +894,7 @@ def _preparse(self, args, addopts=True):
877
894
self .hook .pytest_load_initial_conftests (
878
895
early_config = self , args = args , parser = self ._parser
879
896
)
880
- except ConftestImportFailure :
881
- e = sys .exc_info ()[1 ]
897
+ except ConftestImportFailure as e :
882
898
if ns .help or ns .version :
883
899
# we don't want to prevent --help/--version to work
884
900
# so just let is pass and print a warning at the end
@@ -944,7 +960,7 @@ def addinivalue_line(self, name, line):
944
960
assert isinstance (x , list )
945
961
x .append (line ) # modifies the cached list inline
946
962
947
- def getini (self , name ):
963
+ def getini (self , name : str ):
948
964
""" return configuration value from an :ref:`ini file <inifiles>`. If the
949
965
specified name hasn't been registered through a prior
950
966
:py:func:`parser.addini <_pytest.config.Parser.addini>`
@@ -955,7 +971,7 @@ def getini(self, name):
955
971
self ._inicache [name ] = val = self ._getini (name )
956
972
return val
957
973
958
- def _getini (self , name ) :
974
+ def _getini (self , name : str ) -> Any :
959
975
try :
960
976
description , type , default = self ._parser ._inidict [name ]
961
977
except KeyError :
@@ -1000,7 +1016,7 @@ def _getconftest_pathlist(self, name, path):
1000
1016
values .append (relroot )
1001
1017
return values
1002
1018
1003
- def _get_override_ini_value (self , name ) :
1019
+ def _get_override_ini_value (self , name : str ) -> Optional [ str ] :
1004
1020
value = None
1005
1021
# override_ini is a list of "ini=value" options
1006
1022
# always use the last item if multiple values are set for same ini-name,
@@ -1015,7 +1031,7 @@ def _get_override_ini_value(self, name):
1015
1031
value = user_ini_value
1016
1032
return value
1017
1033
1018
- def getoption (self , name , default = notset , skip = False ):
1034
+ def getoption (self , name : str , default = notset , skip : bool = False ):
1019
1035
""" return command line option value.
1020
1036
1021
1037
:arg name: name of the option. You may also specify
0 commit comments