Skip to content

Commit 8c6e01d

Browse files
committed
config: remove _config_source_hint, use extra_info instead
Let's use a single mechanism. The error is now shown in its own line but that's fine.
1 parent 6ff0670 commit 8c6e01d

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

src/_pytest/config/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,13 +1307,13 @@ def _unconfigure_python_path(self) -> None:
13071307

13081308
def _validate_args(self, args: list[str], via: str) -> list[str]:
13091309
"""Validate known args."""
1310-
self._parser._config_source_hint = via
1310+
self._parser.extra_info["config source"] = via
13111311
try:
13121312
self._parser.parse_known_and_unknown_args(
13131313
args, namespace=copy.copy(self.option)
13141314
)
13151315
finally:
1316-
self._parser._config_source_hint = None
1316+
self._parser.extra_info.pop("config source", None)
13171317

13181318
return args
13191319

src/_pytest/config/argparsing.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def __init__(
5353
# Maps alias -> canonical name.
5454
self._ini_aliases: dict[str, str] = {}
5555
self.extra_info: dict[str, Any] = {}
56-
self._config_source_hint: str | None = None
5756

5857
def processoption(self, option: Argument) -> None:
5958
if self._processopt:
@@ -446,8 +445,6 @@ def __init__(
446445
def error(self, message: str) -> NoReturn:
447446
"""Transform argparse error message into UsageError."""
448447
msg = f"{self.prog}: error: {message}"
449-
if self._parser._config_source_hint is not None:
450-
msg = f"{msg} ({self._parser._config_source_hint})"
451448
if self.extra_info:
452449
msg += "\n" + "\n".join(
453450
f" {k}: {v}" for k, v in sorted(self.extra_info.items())

testing/test_config.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,9 +2290,10 @@ def test_addopts_from_env_not_concatenated(
22902290
with pytest.raises(UsageError) as excinfo:
22912291
config._preparse(["cache_dir=ignored"], addopts=True)
22922292
assert (
2293-
"error: argument -o/--override-ini: expected one argument (via PYTEST_ADDOPTS)"
2293+
"error: argument -o/--override-ini: expected one argument"
22942294
in excinfo.value.args[0]
22952295
)
2296+
assert "via PYTEST_ADDOPTS" in excinfo.value.args[0]
22962297

22972298
def test_addopts_from_ini_not_concatenated(self, pytester: Pytester) -> None:
22982299
"""`addopts` from configuration should not take values from normal args (#4265)."""
@@ -2303,10 +2304,11 @@ def test_addopts_from_ini_not_concatenated(self, pytester: Pytester) -> None:
23032304
"""
23042305
)
23052306
result = pytester.runpytest("cache_dir=ignored")
2307+
config = pytester._request.config
23062308
result.stderr.fnmatch_lines(
23072309
[
2308-
f"{pytester._request.config._parser.optparser.prog}: error: "
2309-
f"argument -o/--override-ini: expected one argument (via addopts config)"
2310+
f"{config._parser.optparser.prog}: error: argument -o/--override-ini: expected one argument",
2311+
" config source: via addopts config",
23102312
]
23112313
)
23122314
assert result.ret == _pytest.config.ExitCode.USAGE_ERROR

0 commit comments

Comments
 (0)