Skip to content

Commit 0ccb579

Browse files
committed
python: a bit nicer error on duplicate parametrization
Raising `ValueError` dumps all of the internal stacktrace to the user, which is not helpful. Raising `CollectError` is handled specially to just print the message. It would be nice to show source location or such - maybe another time. Fix #13457
1 parent 261e7f1 commit 0ccb579

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

changelog/13457.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The error message about duplicate parametrization no longer displays an internal stack trace.

src/_pytest/python.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ def setmulti(
10791079
arg2scope = dict(self._arg2scope)
10801080
for arg, val in zip(argnames, valset):
10811081
if arg in params:
1082-
raise ValueError(f"duplicate parametrization of {arg!r}")
1082+
raise nodes.Collector.CollectError(
1083+
f"duplicate parametrization of {arg!r}"
1084+
)
10831085
params[arg] = val
10841086
indices[arg] = param_index
10851087
arg2scope[arg] = scope

testing/python/metafunc.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,15 @@ def func(x, y):
8282

8383
metafunc = self.Metafunc(func)
8484
metafunc.parametrize("x", [1, 2])
85-
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6]))
86-
pytest.raises(ValueError, lambda: metafunc.parametrize("x", [5, 6]))
85+
with pytest.raises(pytest.Collector.CollectError):
86+
metafunc.parametrize("x", [5, 6])
87+
with pytest.raises(pytest.Collector.CollectError):
88+
metafunc.parametrize("x", [5, 6])
8789
metafunc.parametrize("y", [1, 2])
88-
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6]))
89-
pytest.raises(ValueError, lambda: metafunc.parametrize("y", [5, 6]))
90+
with pytest.raises(pytest.Collector.CollectError):
91+
metafunc.parametrize("y", [5, 6])
92+
with pytest.raises(pytest.Collector.CollectError):
93+
metafunc.parametrize("y", [5, 6])
9094

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

0 commit comments

Comments
 (0)