Skip to content

Commit e3e2333

Browse files
committed
Add type annotations to _pytest.config.argparsing and corresponding Config code
1 parent 51f9cd0 commit e3e2333

File tree

4 files changed

+160
-107
lines changed

4 files changed

+160
-107
lines changed

src/_pytest/_argcomplete.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@
5353
which should throw a KeyError: 'COMPLINE' (which is properly set by the
5454
global argcomplete script).
5555
"""
56+
import argparse
5657
import os
5758
import sys
5859
from glob import glob
60+
from typing import Any
61+
from typing import List
5962
from typing import Optional
6063

6164

6265
class FastFilesCompleter:
6366
"Fast file completer class"
6467

65-
def __init__(self, directories=True):
68+
def __init__(self, directories: bool = True) -> None:
6669
self.directories = directories
6770

68-
def __call__(self, prefix, **kwargs):
71+
def __call__(self, prefix: str, **kwargs: Any) -> List[str]:
6972
"""only called on non option completions"""
7073
if os.path.sep in prefix[1:]:
7174
prefix_dir = len(os.path.dirname(prefix) + os.path.sep)
@@ -94,13 +97,13 @@ def __call__(self, prefix, **kwargs):
9497
sys.exit(-1)
9598
filescompleter = FastFilesCompleter() # type: Optional[FastFilesCompleter]
9699

97-
def try_argcomplete(parser):
100+
def try_argcomplete(parser: argparse.ArgumentParser) -> None:
98101
argcomplete.autocomplete(parser, always_complete_options=False)
99102

100103

101104
else:
102105

103-
def try_argcomplete(parser):
106+
def try_argcomplete(parser: argparse.ArgumentParser) -> None:
104107
pass
105108

106109
filescompleter = None

src/_pytest/config/__init__.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# mypy: check_untyped_defs
12
""" command line options, ini-file and conftest.py processing. """
23
import argparse
34
import copy
@@ -45,6 +46,8 @@
4546
if False: # TYPE_CHECKING
4647
from typing import Type
4748

49+
from .argparsing import Argument
50+
4851

4952
hookimpl = HookimplMarker("pytest")
5053
hookspec = HookspecMarker("pytest")
@@ -679,7 +682,7 @@ class InvocationParams:
679682
plugins = attr.ib()
680683
dir = attr.ib(type=Path)
681684

682-
def __init__(self, pluginmanager, *, invocation_params=None):
685+
def __init__(self, pluginmanager, *, invocation_params=None) -> None:
683686
from .argparsing import Parser, FILE_OR_DIR
684687

685688
if invocation_params is None:
@@ -792,19 +795,20 @@ def fromdictargs(cls, option_dict, args):
792795
config.pluginmanager.consider_pluginarg(x)
793796
return config
794797

795-
def _processopt(self, opt):
796-
for name in opt._short_opts + opt._long_opts:
797-
self._opt2dest[name] = opt.dest
798+
def _processopt(self, opt: "Argument") -> None:
799+
if opt.dest:
800+
for name in opt._short_opts + opt._long_opts:
801+
self._opt2dest[name] = opt.dest
798802

799-
if hasattr(opt, "default") and opt.dest:
800-
if not hasattr(self.option, opt.dest):
801-
setattr(self.option, opt.dest, opt.default)
803+
if hasattr(opt, "default"):
804+
if not hasattr(self.option, opt.dest):
805+
setattr(self.option, opt.dest, opt.default)
802806

803807
@hookimpl(trylast=True)
804808
def pytest_load_initial_conftests(self, early_config):
805809
self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
806810

807-
def _initini(self, args) -> None:
811+
def _initini(self, args: Sequence[str]) -> None:
808812
ns, unknown_args = self._parser.parse_known_and_unknown_args(
809813
args, namespace=copy.copy(self.option)
810814
)
@@ -821,7 +825,7 @@ def _initini(self, args) -> None:
821825
self._parser.addini("minversion", "minimally required pytest version")
822826
self._override_ini = ns.override_ini or ()
823827

824-
def _consider_importhook(self, args):
828+
def _consider_importhook(self, args: Sequence[str]) -> None:
825829
"""Install the PEP 302 import hook if using assertion rewriting.
826830
827831
Needs to parse the --assert=<mode> option from the commandline
@@ -861,19 +865,19 @@ def _mark_plugins_for_rewrite(self, hook):
861865
for name in _iter_rewritable_modules(package_files):
862866
hook.mark_rewrite(name)
863867

864-
def _validate_args(self, args, via):
868+
def _validate_args(self, args: List[str], via: str) -> List[str]:
865869
"""Validate known args."""
866-
self._parser._config_source_hint = via
870+
self._parser._config_source_hint = via # type: ignore
867871
try:
868872
self._parser.parse_known_and_unknown_args(
869873
args, namespace=copy.copy(self.option)
870874
)
871875
finally:
872-
del self._parser._config_source_hint
876+
del self._parser._config_source_hint # type: ignore
873877

874878
return args
875879

876-
def _preparse(self, args, addopts=True):
880+
def _preparse(self, args: List[str], addopts: bool = True) -> None:
877881
if addopts:
878882
env_addopts = os.environ.get("PYTEST_ADDOPTS", "")
879883
if len(env_addopts):
@@ -937,7 +941,7 @@ def _checkversion(self):
937941
)
938942
)
939943

940-
def parse(self, args, addopts=True):
944+
def parse(self, args: List[str], addopts: bool = True) -> None:
941945
# parse given cmdline arguments into this config object.
942946
assert not hasattr(
943947
self, "args"
@@ -948,7 +952,7 @@ def parse(self, args, addopts=True):
948952
self._preparse(args, addopts=addopts)
949953
# XXX deprecated hook:
950954
self.hook.pytest_cmdline_preparse(config=self, args=args)
951-
self._parser.after_preparse = True
955+
self._parser.after_preparse = True # type: ignore
952956
try:
953957
args = self._parser.parse_setoption(
954958
args, self.option, namespace=self.option

0 commit comments

Comments
 (0)