Skip to content

Commit 0e05bfe

Browse files
committed
config: move _opt2dest maintenance to Parser
It's a low-level concern so low level should handle it. And one less thing in the `_processopt` callback which hopefully we can eventually remove.
1 parent 38e8676 commit 0e05bfe

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/_pytest/config/__init__.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,6 @@ def __init__(
10831083
self.trace = self.pluginmanager.trace.root.get("config")
10841084
self.hook: pluggy.HookRelay = PathAwareHookProxy(self.pluginmanager.hook) # type: ignore[assignment]
10851085
self._inicache: dict[str, Any] = {}
1086-
self._opt2dest: dict[str, str] = {}
10871086
self._cleanup_stack = contextlib.ExitStack()
10881087
self.pluginmanager.register(self, "pytestconfig")
10891088
self._configured = False
@@ -1208,9 +1207,6 @@ def fromdictargs(cls, option_dict: Mapping[str, Any], args: list[str]) -> Config
12081207
return config
12091208

12101209
def _processopt(self, opt: Argument) -> None:
1211-
for name in opt.names():
1212-
self._opt2dest[name] = opt.dest
1213-
12141210
if not hasattr(self.option, opt.dest):
12151211
setattr(self.option, opt.dest, opt.default)
12161212

@@ -1842,7 +1838,7 @@ def getoption(self, name: str, default: Any = NOTSET, skip: bool = False):
18421838
:param skip: If ``True``, raise :func:`pytest.skip` if option is undeclared or has a ``None`` value.
18431839
Note that even if ``True``, if a default was specified it will be returned instead of a skip.
18441840
"""
1845-
name = self._opt2dest.get(name, name)
1841+
name = self._parser._opt2dest.get(name, name)
18461842
try:
18471843
val = getattr(self.option, name)
18481844
if val is None and skip:

src/_pytest/config/argparsing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def __init__(
4848
anonymous_arggroup, "_anonymous", self, _ispytest=True
4949
)
5050
self._groups = [self._anonymous]
51+
# Maps option strings -> dest, e.g. "-V" and "--version" to "version".
52+
self._opt2dest: dict[str, str] = {}
5153
file_or_dir_arg = self.optparser.add_argument(FILE_OR_DIR, nargs="*")
5254
file_or_dir_arg.completer = filescompleter # type: ignore
5355

@@ -368,6 +370,8 @@ def _addoption_inner(
368370
option = Argument(action)
369371
self.options.append(option)
370372
if self.parser:
373+
for name in option.names():
374+
self.parser._opt2dest[name] = option.dest
371375
self.parser.processoption(option)
372376

373377

0 commit comments

Comments
 (0)