Skip to content

Commit dac16cd

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

File tree

4 files changed

+161
-107
lines changed

4 files changed

+161
-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: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
if False: # TYPE_CHECKING
4646
from typing import Type
4747

48+
from .argparsing import Argument
49+
4850

4951
hookimpl = HookimplMarker("pytest")
5052
hookspec = HookspecMarker("pytest")
@@ -679,7 +681,7 @@ class InvocationParams:
679681
plugins = attr.ib()
680682
dir = attr.ib(type=Path)
681683

682-
def __init__(self, pluginmanager, *, invocation_params=None):
684+
def __init__(self, pluginmanager, *, invocation_params=None) -> None:
683685
from .argparsing import Parser, FILE_OR_DIR
684686

685687
if invocation_params is None:
@@ -792,19 +794,19 @@ def fromdictargs(cls, option_dict, args):
792794
config.pluginmanager.consider_pluginarg(x)
793795
return config
794796

795-
def _processopt(self, opt):
797+
def _processopt(self, opt: "Argument") -> None:
796798
for name in opt._short_opts + opt._long_opts:
797799
self._opt2dest[name] = opt.dest
798800

799-
if hasattr(opt, "default") and opt.dest:
801+
if hasattr(opt, "default"):
800802
if not hasattr(self.option, opt.dest):
801803
setattr(self.option, opt.dest, opt.default)
802804

803805
@hookimpl(trylast=True)
804806
def pytest_load_initial_conftests(self, early_config):
805807
self.pluginmanager._set_initial_conftests(early_config.known_args_namespace)
806808

807-
def _initini(self, args) -> None:
809+
def _initini(self, args: Sequence[str]) -> None:
808810
ns, unknown_args = self._parser.parse_known_and_unknown_args(
809811
args, namespace=copy.copy(self.option)
810812
)
@@ -821,7 +823,7 @@ def _initini(self, args) -> None:
821823
self._parser.addini("minversion", "minimally required pytest version")
822824
self._override_ini = ns.override_ini or ()
823825

824-
def _consider_importhook(self, args):
826+
def _consider_importhook(self, args: Sequence[str]) -> None:
825827
"""Install the PEP 302 import hook if using assertion rewriting.
826828
827829
Needs to parse the --assert=<mode> option from the commandline
@@ -861,19 +863,19 @@ def _mark_plugins_for_rewrite(self, hook):
861863
for name in _iter_rewritable_modules(package_files):
862864
hook.mark_rewrite(name)
863865

864-
def _validate_args(self, args, via):
866+
def _validate_args(self, args: List[str], via: str) -> List[str]:
865867
"""Validate known args."""
866-
self._parser._config_source_hint = via
868+
self._parser._config_source_hint = via # type: ignore
867869
try:
868870
self._parser.parse_known_and_unknown_args(
869871
args, namespace=copy.copy(self.option)
870872
)
871873
finally:
872-
del self._parser._config_source_hint
874+
del self._parser._config_source_hint # type: ignore
873875

874876
return args
875877

876-
def _preparse(self, args, addopts=True):
878+
def _preparse(self, args: List[str], addopts: bool = True) -> None:
877879
if addopts:
878880
env_addopts = os.environ.get("PYTEST_ADDOPTS", "")
879881
if len(env_addopts):
@@ -937,7 +939,7 @@ def _checkversion(self):
937939
)
938940
)
939941

940-
def parse(self, args, addopts=True):
942+
def parse(self, args: List[str], addopts: bool = True) -> None:
941943
# parse given cmdline arguments into this config object.
942944
assert not hasattr(
943945
self, "args"
@@ -948,7 +950,7 @@ def parse(self, args, addopts=True):
948950
self._preparse(args, addopts=addopts)
949951
# XXX deprecated hook:
950952
self.hook.pytest_cmdline_preparse(config=self, args=args)
951-
self._parser.after_preparse = True
953+
self._parser.after_preparse = True # type: ignore
952954
try:
953955
args = self._parser.parse_setoption(
954956
args, self.option, namespace=self.option

0 commit comments

Comments
 (0)