Skip to content

Commit cd300cc

Browse files
committed
Fix check_untyped_defs = True for config/
1 parent d9a8c2c commit cd300cc

File tree

3 files changed

+49
-21
lines changed

3 files changed

+49
-21
lines changed

src/_pytest/config/__init__.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
import warnings
1010
from functools import lru_cache
1111
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
1221

1322
import attr
1423
import py
@@ -32,6 +41,10 @@
3241
from _pytest.outcomes import Skipped
3342
from _pytest.warning_types import PytestConfigWarning
3443

44+
if False: # TYPE_CHECKING
45+
from typing import Type
46+
47+
3548
hookimpl = HookimplMarker("pytest")
3649
hookspec = HookspecMarker("pytest")
3750

@@ -40,7 +53,7 @@ class ConftestImportFailure(Exception):
4053
def __init__(self, path, excinfo):
4154
Exception.__init__(self, path, excinfo)
4255
self.path = path
43-
self.excinfo = excinfo
56+
self.excinfo = excinfo # type: Tuple[Type[Exception], Exception, TracebackType]
4457

4558

4659
def main(args=None, plugins=None):
@@ -237,14 +250,18 @@ class PytestPluginManager(PluginManager):
237250

238251
def __init__(self):
239252
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]
241255

242256
# 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]
245261
self._confcutdir = None
246262
self._noconftest = False
247-
self._duplicatepaths = set()
263+
# Set of py.path.local's.
264+
self._duplicatepaths = set() # type: Set[Any]
248265

249266
self.add_hookspecs(_pytest.hookspec)
250267
self.register(self)
@@ -653,7 +670,7 @@ class InvocationParams:
653670

654671
args = attr.ib()
655672
plugins = attr.ib()
656-
dir = attr.ib()
673+
dir = attr.ib(type=Path)
657674

658675
def __init__(self, pluginmanager, *, invocation_params=None):
659676
from .argparsing import Parser, FILE_OR_DIR
@@ -674,10 +691,10 @@ def __init__(self, pluginmanager, *, invocation_params=None):
674691
self.pluginmanager = pluginmanager
675692
self.trace = self.pluginmanager.trace.root.get("config")
676693
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]]
681698
self.pluginmanager.register(self, "pytestconfig")
682699
self._configured = False
683700
self.hook.pytest_addoption.call_historic(kwargs=dict(parser=self._parser))
@@ -776,7 +793,7 @@ def _processopt(self, opt):
776793
def pytest_load_initial_conftests(self, early_config):
777794
self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
778795

779-
def _initini(self, args):
796+
def _initini(self, args) -> None:
780797
ns, unknown_args = self._parser.parse_known_and_unknown_args(
781798
args, namespace=copy.copy(self.option)
782799
)
@@ -877,8 +894,7 @@ def _preparse(self, args, addopts=True):
877894
self.hook.pytest_load_initial_conftests(
878895
early_config=self, args=args, parser=self._parser
879896
)
880-
except ConftestImportFailure:
881-
e = sys.exc_info()[1]
897+
except ConftestImportFailure as e:
882898
if ns.help or ns.version:
883899
# we don't want to prevent --help/--version to work
884900
# so just let is pass and print a warning at the end
@@ -944,7 +960,7 @@ def addinivalue_line(self, name, line):
944960
assert isinstance(x, list)
945961
x.append(line) # modifies the cached list inline
946962

947-
def getini(self, name):
963+
def getini(self, name: str):
948964
""" return configuration value from an :ref:`ini file <inifiles>`. If the
949965
specified name hasn't been registered through a prior
950966
:py:func:`parser.addini <_pytest.config.Parser.addini>`
@@ -955,7 +971,7 @@ def getini(self, name):
955971
self._inicache[name] = val = self._getini(name)
956972
return val
957973

958-
def _getini(self, name):
974+
def _getini(self, name: str) -> Any:
959975
try:
960976
description, type, default = self._parser._inidict[name]
961977
except KeyError:
@@ -1000,7 +1016,7 @@ def _getconftest_pathlist(self, name, path):
10001016
values.append(relroot)
10011017
return values
10021018

1003-
def _get_override_ini_value(self, name):
1019+
def _get_override_ini_value(self, name: str) -> Optional[str]:
10041020
value = None
10051021
# override_ini is a list of "ini=value" options
10061022
# 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):
10151031
value = user_ini_value
10161032
return value
10171033

1018-
def getoption(self, name, default=notset, skip=False):
1034+
def getoption(self, name: str, default=notset, skip: bool = False):
10191035
""" return command line option value.
10201036
10211037
:arg name: name of the option. You may also specify

src/_pytest/config/argparsing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def parse(self, args, namespace=None):
8585
args = [str(x) if isinstance(x, py.path.local) else x for x in args]
8686
return self.optparser.parse_args(args, namespace=namespace)
8787

88-
def _getparser(self):
88+
def _getparser(self) -> "MyOptionParser":
8989
from _pytest._argcomplete import filescompleter
9090

9191
optparser = MyOptionParser(self, self.extra_info, prog=self.prog)
@@ -111,13 +111,15 @@ def parse_setoption(self, args, option, namespace=None):
111111
setattr(option, name, value)
112112
return getattr(parsedoption, FILE_OR_DIR)
113113

114-
def parse_known_args(self, args, namespace=None):
114+
def parse_known_args(self, args, namespace=None) -> argparse.Namespace:
115115
"""parses and returns a namespace object with known arguments at this
116116
point.
117117
"""
118118
return self.parse_known_and_unknown_args(args, namespace=namespace)[0]
119119

120-
def parse_known_and_unknown_args(self, args, namespace=None):
120+
def parse_known_and_unknown_args(
121+
self, args, namespace=None
122+
) -> Tuple[argparse.Namespace, List[str]]:
121123
"""parses and returns a namespace object with known arguments, and
122124
the remaining arguments unknown at this point.
123125
"""

src/_pytest/config/findpaths.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import os
2+
from typing import List
3+
from typing import Optional
24

35
import py
46

57
from .exceptions import UsageError
68
from _pytest.outcomes import fail
79

10+
if False:
11+
from . import Config # noqa: F401
12+
813

914
def exists(path, ignore=EnvironmentError):
1015
try:
@@ -98,7 +103,12 @@ def get_dir_from_path(path):
98103
CFG_PYTEST_SECTION = "[pytest] section in {filename} files is no longer supported, change to [tool:pytest] instead."
99104

100105

101-
def determine_setup(inifile, args, rootdir_cmd_arg=None, config=None):
106+
def determine_setup(
107+
inifile: str,
108+
args: List[str],
109+
rootdir_cmd_arg: Optional[str] = None,
110+
config: Optional["Config"] = None,
111+
):
102112
dirs = get_dirs_from_args(args)
103113
if inifile:
104114
iniconfig = py.iniconfig.IniConfig(inifile)

0 commit comments

Comments
 (0)