8000 python: a bit nicer error on duplicate parametrization by bluetech · Pull Request #13458 · pytest-dev/pytest · GitHub
[go: up one dir, main page]

Skip to content

python: a bit nicer error on duplicate parametrization #13458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/13457.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The error message about duplicate parametrization no longer displays an internal stack trace.
10 changes: 8 additions & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1073,13 +1073,16 @@ def setmulti(
marks: Iterable[Mark | MarkDecorator],
scope: Scope,
param_index: int,
nodeid: str,
) -> CallSpec2:
params = self.params.copy()
indices = self.indices.copy()
arg2scope = dict(self._arg2scope)
for arg, val in zip(argnames, valset):
if arg in params:
raise ValueError(f"duplicate parametrization of {arg!r}")
raise nodes.Collector.CollectError(
f"{nodeid}: duplicate parametrization of {arg!r}"
)
params[arg] = val
indices[arg] = param_index
arg2scope[arg] = scope
Expand Down Expand Up @@ -1233,6 +1236,8 @@ def parametrize(
It will also override any fixture-function defined scope, allowing
to set a dynamic scope using test context or configuration.
"""
nodeid = self.definition.nodeid

argnames, parametersets = ParameterSet._for_parametrize(
argnames,
argvalues,
Expand All @@ -1244,7 +1249,7 @@ def parametrize(

if "request" in argnames:
fail(
"'request' is a reserved name and cannot be used in @pytest.mark.parametrize",
f"{nodeid}: 'request' is a reserved name and cannot be used in @pytest.mark.parametrize",
pytrace=False,
)

Expand Down Expand Up @@ -1339,6 +1344,7 @@ def parametrize(
marks=param_set.marks,
scope=scope_,
param_index=param_index,
nodeid=nodeid,
)
newcalls.append(newcallspec)
self._calls = newcalls
Expand Down
12 changes: 8 additions & 4 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ def func(x, y):

metafunc = self.Metafunc(func)
metafunc.parametrize("x", [1, 2])
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6]))
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6]))
with pytest.raises(pytest.Collector.CollectError):
metafunc.parametrize("x", [5, 6])
with pytest.raises(pytest.Collector.CollectError):
metafunc.parametrize("x", [5, 6])
metafunc.parametrize("y", [1, 2])
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6]))
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6]))
with pytest.raises(pytest.Collector.CollectError):
metafunc.parametrize("y", [5, 6])
with pytest.raises(pytest.Collector.CollectError):
metafunc.parametrize("y", [5, 6])

with pytest.raises(TypeError, match="^ids must be a callable or an iterable$"):
metafunc.parametrize("y", [5, 6], ids=42) # type: ignore[arg-type]
Expand Down
0