Skip to content

Commit 4f07180

Browse files
committed
config: inline parse_setoption
It only makes the code harder to understand in my opinion. Also remove the manual `setattr(option, name, value)` which are unneeded because `parse` already updates the `Namespace`.
1 parent 95e0cf2 commit 4f07180

File tree

3 files changed

+6
-22
lines changed

3 files changed

+6
-22
lines changed

src/_pytest/config/__init__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
from _pytest._io import TerminalWriter
5757
from _pytest.compat import assert_never
5858
from _pytest.config.argparsing import Argument
59+
from _pytest.config.argparsing import FILE_OR_DIR
5960
from _pytest.config.argparsing import Parser
6061
import _pytest.deprecated
6162
import _pytest.hookspec
@@ -1538,13 +1539,11 @@ def parse(self, args: list[str], addopts: bool = True) -> None:
15381539
self._preparse(args, addopts=addopts)
15391540
self._parser.after_preparse = True # type: ignore
15401541
try:
1541-
args = self._parser.parse_setoption(
1542-
args, self.option, namespace=self.option
1543-
)
1542+
parsed = self._parser.parse(args, namespace=self.option)
15441543
except PrintHelp:
15451544
return
15461545
self.args, self.args_source = self._decide_args(
1547-
args=args,
1546+
args=getattr(parsed, FILE_OR_DIR),
15481547
pyargs=self.known_args_namespace.pyargs,
15491548
testpaths=self.getini("testpaths"),
15501549
invocation_dir=self.invocation_params.dir,

src/_pytest/config/argparsing.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from collections.abc import Sequence
88
import os
99
from typing import Any
10-
from typing import cast
1110
from typing import final
1211
from typing import Literal
1312
from typing import NoReturn
@@ -134,17 +133,6 @@ def _getparser(self) -> PytestArgumentParser:
134133
file_or_dir_arg.completer = filescompleter # type: ignore
135134
return optparser
136135

137-
def parse_setoption(
138-
self,
139-
args: Sequence[str | os.PathLike[str]],
140-
option: argparse.Namespace,
141-
namespace: argparse.Namespace | None = None,
142-
) -> list[str]:
143-
parsedoption = self.parse(args, namespace=namespace)
144-
for name, value in parsedoption.__dict__.items():
145-
setattr(option, name, value)
146-
return cast(list[str], getattr(parsedoption, FILE_OR_DIR))
147-
148136
def parse_known_args(
149137
self,
150138
args: Sequence[str | os.PathLike[str]],

testing/test_parseopt.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,16 @@ def test_parse_will_set_default(self, parser: parseopt.Parser) -> None:
158158
parser.addoption("--hello", dest="hello", default="x", action="store")
159159
option = parser.parse([])
160160
assert option.hello == "x"
161-
del option.hello
162-
parser.parse_setoption([], option)
163-
assert option.hello == "x"
164161

165-
def test_parse_setoption(self, parser: parseopt.Parser) -> None:
162+
def test_parse_set_options(self, parser: parseopt.Parser) -> None:
166163
parser.addoption("--hello", dest="hello", action="store")
167164
parser.addoption("--world", dest="world", default=42)
168165

169166
option = argparse.Namespace()
170-
args = parser.parse_setoption(["--hello", "world"], option)
167+
parser.parse(["--hello", "world"], option)
171168
assert option.hello == "world"
172169
assert option.world == 42
173-
assert not args
170+
assert getattr(option, parseopt.FILE_OR_DIR) == []
174171

175172
def test_parse_special_destination(self, parser: parseopt.Parser) -> None:
176173
parser.addoption("--ultimate-answer", type=int)

0 commit comments

Comments
 (0)