Merge pull request #6241 from bluetech/type-annotations-9
Add type annotations to _pytest.config.argparsing and corresponding Config code
This commit is contained in:
@@ -12,22 +12,22 @@ from _pytest.config.exceptions import UsageError
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def parser():
|
||||
def parser() -> parseopt.Parser:
|
||||
return parseopt.Parser()
|
||||
|
||||
|
||||
class TestParser:
|
||||
def test_no_help_by_default(self):
|
||||
def test_no_help_by_default(self) -> None:
|
||||
parser = parseopt.Parser(usage="xyz")
|
||||
pytest.raises(UsageError, lambda: parser.parse(["-h"]))
|
||||
|
||||
def test_custom_prog(self, parser):
|
||||
def test_custom_prog(self, parser: parseopt.Parser) -> None:
|
||||
"""Custom prog can be set for `argparse.ArgumentParser`."""
|
||||
assert parser._getparser().prog == os.path.basename(sys.argv[0])
|
||||
parser.prog = "custom-prog"
|
||||
assert parser._getparser().prog == "custom-prog"
|
||||
|
||||
def test_argument(self):
|
||||
def test_argument(self) -> None:
|
||||
with pytest.raises(parseopt.ArgumentError):
|
||||
# need a short or long option
|
||||
argument = parseopt.Argument()
|
||||
@@ -45,7 +45,7 @@ class TestParser:
|
||||
"Argument(_short_opts: ['-t'], _long_opts: ['--test'], dest: 'abc')"
|
||||
)
|
||||
|
||||
def test_argument_type(self):
|
||||
def test_argument_type(self) -> None:
|
||||
argument = parseopt.Argument("-t", dest="abc", type=int)
|
||||
assert argument.type is int
|
||||
argument = parseopt.Argument("-t", dest="abc", type=str)
|
||||
@@ -60,7 +60,7 @@ class TestParser:
|
||||
)
|
||||
assert argument.type is str
|
||||
|
||||
def test_argument_processopt(self):
|
||||
def test_argument_processopt(self) -> None:
|
||||
argument = parseopt.Argument("-t", type=int)
|
||||
argument.default = 42
|
||||
argument.dest = "abc"
|
||||
@@ -68,19 +68,19 @@ class TestParser:
|
||||
assert res["default"] == 42
|
||||
assert res["dest"] == "abc"
|
||||
|
||||
def test_group_add_and_get(self, parser):
|
||||
def test_group_add_and_get(self, parser: parseopt.Parser) -> None:
|
||||
group = parser.getgroup("hello", description="desc")
|
||||
assert group.name == "hello"
|
||||
assert group.description == "desc"
|
||||
|
||||
def test_getgroup_simple(self, parser):
|
||||
def test_getgroup_simple(self, parser: parseopt.Parser) -> None:
|
||||
group = parser.getgroup("hello", description="desc")
|
||||
assert group.name == "hello"
|
||||
assert group.description == "desc"
|
||||
group2 = parser.getgroup("hello")
|
||||
assert group2 is group
|
||||
|
||||
def test_group_ordering(self, parser):
|
||||
def test_group_ordering(self, parser: parseopt.Parser) -> None:
|
||||
parser.getgroup("1")
|
||||
parser.getgroup("2")
|
||||
parser.getgroup("3", after="1")
|
||||
@@ -88,20 +88,20 @@ class TestParser:
|
||||
groups_names = [x.name for x in groups]
|
||||
assert groups_names == list("132")
|
||||
|
||||
def test_group_addoption(self):
|
||||
def test_group_addoption(self) -> None:
|
||||
group = parseopt.OptionGroup("hello")
|
||||
group.addoption("--option1", action="store_true")
|
||||
assert len(group.options) == 1
|
||||
assert isinstance(group.options[0], parseopt.Argument)
|
||||
|
||||
def test_group_addoption_conflict(self):
|
||||
def test_group_addoption_conflict(self) -> None:
|
||||
group = parseopt.OptionGroup("hello again")
|
||||
group.addoption("--option1", "--option-1", action="store_true")
|
||||
with pytest.raises(ValueError) as err:
|
||||
group.addoption("--option1", "--option-one", action="store_true")
|
||||
assert str({"--option1"}) in str(err.value)
|
||||
|
||||
def test_group_shortopt_lowercase(self, parser):
|
||||
def test_group_shortopt_lowercase(self, parser: parseopt.Parser) -> None:
|
||||
group = parser.getgroup("hello")
|
||||
with pytest.raises(ValueError):
|
||||
group.addoption("-x", action="store_true")
|
||||
@@ -109,30 +109,30 @@ class TestParser:
|
||||
group._addoption("-x", action="store_true")
|
||||
assert len(group.options) == 1
|
||||
|
||||
def test_parser_addoption(self, parser):
|
||||
def test_parser_addoption(self, parser: parseopt.Parser) -> None:
|
||||
group = parser.getgroup("custom options")
|
||||
assert len(group.options) == 0
|
||||
group.addoption("--option1", action="store_true")
|
||||
assert len(group.options) == 1
|
||||
|
||||
def test_parse(self, parser):
|
||||
def test_parse(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--hello", dest="hello", action="store")
|
||||
args = parser.parse(["--hello", "world"])
|
||||
assert args.hello == "world"
|
||||
assert not getattr(args, parseopt.FILE_OR_DIR)
|
||||
|
||||
def test_parse2(self, parser):
|
||||
def test_parse2(self, parser: parseopt.Parser) -> None:
|
||||
args = parser.parse([py.path.local()])
|
||||
assert getattr(args, parseopt.FILE_OR_DIR)[0] == py.path.local()
|
||||
|
||||
def test_parse_known_args(self, parser):
|
||||
def test_parse_known_args(self, parser: parseopt.Parser) -> None:
|
||||
parser.parse_known_args([py.path.local()])
|
||||
parser.addoption("--hello", action="store_true")
|
||||
ns = parser.parse_known_args(["x", "--y", "--hello", "this"])
|
||||
assert ns.hello
|
||||
assert ns.file_or_dir == ["x"]
|
||||
|
||||
def test_parse_known_and_unknown_args(self, parser):
|
||||
def test_parse_known_and_unknown_args(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--hello", action="store_true")
|
||||
ns, unknown = parser.parse_known_and_unknown_args(
|
||||
["x", "--y", "--hello", "this"]
|
||||
@@ -141,7 +141,7 @@ class TestParser:
|
||||
assert ns.file_or_dir == ["x"]
|
||||
assert unknown == ["--y", "this"]
|
||||
|
||||
def test_parse_will_set_default(self, parser):
|
||||
def test_parse_will_set_default(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--hello", dest="hello", default="x", action="store")
|
||||
option = parser.parse([])
|
||||
assert option.hello == "x"
|
||||
@@ -149,25 +149,22 @@ class TestParser:
|
||||
parser.parse_setoption([], option)
|
||||
assert option.hello == "x"
|
||||
|
||||
def test_parse_setoption(self, parser):
|
||||
def test_parse_setoption(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--hello", dest="hello", action="store")
|
||||
parser.addoption("--world", dest="world", default=42)
|
||||
|
||||
class A:
|
||||
pass
|
||||
|
||||
option = A()
|
||||
option = argparse.Namespace()
|
||||
args = parser.parse_setoption(["--hello", "world"], option)
|
||||
assert option.hello == "world"
|
||||
assert option.world == 42
|
||||
assert not args
|
||||
|
||||
def test_parse_special_destination(self, parser):
|
||||
def test_parse_special_destination(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--ultimate-answer", type=int)
|
||||
args = parser.parse(["--ultimate-answer", "42"])
|
||||
assert args.ultimate_answer == 42
|
||||
|
||||
def test_parse_split_positional_arguments(self, parser):
|
||||
def test_parse_split_positional_arguments(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("-R", action="store_true")
|
||||
parser.addoption("-S", action="store_false")
|
||||
args = parser.parse(["-R", "4", "2", "-S"])
|
||||
@@ -181,7 +178,7 @@ class TestParser:
|
||||
assert args.R is True
|
||||
assert args.S is False
|
||||
|
||||
def test_parse_defaultgetter(self):
|
||||
def test_parse_defaultgetter(self) -> None:
|
||||
def defaultget(option):
|
||||
if not hasattr(option, "type"):
|
||||
return
|
||||
@@ -199,17 +196,17 @@ class TestParser:
|
||||
assert option.this == 42
|
||||
assert option.no is False
|
||||
|
||||
def test_drop_short_helper(self):
|
||||
def test_drop_short_helper(self) -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
formatter_class=parseopt.DropShorterLongHelpFormatter, allow_abbrev=False
|
||||
)
|
||||
parser.add_argument(
|
||||
"-t", "--twoword", "--duo", "--two-word", "--two", help="foo"
|
||||
).map_long_option = {"two": "two-word"}
|
||||
)
|
||||
# throws error on --deux only!
|
||||
parser.add_argument(
|
||||
"-d", "--deuxmots", "--deux-mots", action="store_true", help="foo"
|
||||
).map_long_option = {"deux": "deux-mots"}
|
||||
)
|
||||
parser.add_argument("-s", action="store_true", help="single short")
|
||||
parser.add_argument("--abc", "-a", action="store_true", help="bar")
|
||||
parser.add_argument("--klm", "-k", "--kl-m", action="store_true", help="bar")
|
||||
@@ -221,7 +218,7 @@ class TestParser:
|
||||
)
|
||||
parser.add_argument(
|
||||
"-x", "--exit-on-first", "--exitfirst", action="store_true", help="spam"
|
||||
).map_long_option = {"exitfirst": "exit-on-first"}
|
||||
)
|
||||
parser.add_argument("files_and_dirs", nargs="*")
|
||||
args = parser.parse_args(["-k", "--duo", "hallo", "--exitfirst"])
|
||||
assert args.twoword == "hallo"
|
||||
@@ -236,32 +233,32 @@ class TestParser:
|
||||
args = parser.parse_args(["file", "dir"])
|
||||
assert "|".join(args.files_and_dirs) == "file|dir"
|
||||
|
||||
def test_drop_short_0(self, parser):
|
||||
def test_drop_short_0(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--funcarg", "--func-arg", action="store_true")
|
||||
parser.addoption("--abc-def", "--abc-def", action="store_true")
|
||||
parser.addoption("--klm-hij", action="store_true")
|
||||
with pytest.raises(UsageError):
|
||||
parser.parse(["--funcarg", "--k"])
|
||||
|
||||
def test_drop_short_2(self, parser):
|
||||
def test_drop_short_2(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--func-arg", "--doit", action="store_true")
|
||||
args = parser.parse(["--doit"])
|
||||
assert args.func_arg is True
|
||||
|
||||
def test_drop_short_3(self, parser):
|
||||
def test_drop_short_3(self, parser: parseopt.Parser) -> None:
|
||||
parser.addoption("--func-arg", "--funcarg", "--doit", action="store_true")
|
||||
args = parser.parse(["abcd"])
|
||||
assert args.func_arg is False
|
||||
assert args.file_or_dir == ["abcd"]
|
||||
|
||||
def test_drop_short_help0(self, parser, capsys):
|
||||
def test_drop_short_help0(self, parser: parseopt.Parser, capsys) -> None:
|
||||
parser.addoption("--func-args", "--doit", help="foo", action="store_true")
|
||||
parser.parse([])
|
||||
help = parser.optparser.format_help()
|
||||
assert "--func-args, --doit foo" in help
|
||||
|
||||
# testing would be more helpful with all help generated
|
||||
def test_drop_short_help1(self, parser, capsys):
|
||||
def test_drop_short_help1(self, parser: parseopt.Parser, capsys) -> None:
|
||||
group = parser.getgroup("general")
|
||||
group.addoption("--doit", "--func-args", action="store_true", help="foo")
|
||||
group._addoption(
|
||||
@@ -275,7 +272,7 @@ class TestParser:
|
||||
help = parser.optparser.format_help()
|
||||
assert "-doit, --func-args foo" in help
|
||||
|
||||
def test_multiple_metavar_help(self, parser):
|
||||
def test_multiple_metavar_help(self, parser: parseopt.Parser) -> None:
|
||||
"""
|
||||
Help text for options with a metavar tuple should display help
|
||||
in the form "--preferences=value1 value2 value3" (#2004).
|
||||
@@ -290,7 +287,7 @@ class TestParser:
|
||||
assert "--preferences=value1 value2 value3" in help
|
||||
|
||||
|
||||
def test_argcomplete(testdir, monkeypatch):
|
||||
def test_argcomplete(testdir, monkeypatch) -> None:
|
||||
if not distutils.spawn.find_executable("bash"):
|
||||
pytest.skip("bash not available")
|
||||
script = str(testdir.tmpdir.join("test_argcomplete"))
|
||||
|
||||
Reference in New Issue
Block a user