Skip to content
Open
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/14619.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Parametrization now reports a clear collection error when a trailing-comma argument receives a non-sequence value, instead of raising an internal ``TypeError``.
13 changes: 11 additions & 2 deletions src/_pytest/mark/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,16 @@ def _for_parametrize(
if parameters:
# Check all parameter sets have the correct number of values.
for param in parameters:
if len(param.values) != len(argnames):
try:
values_len = len(param.values)
except TypeError:
fail(
f'{nodeid}: in "parametrize" expected a sequence of values, '
f"got {param.values!r}",
pytrace=False,
)

if values_len != len(argnames):
msg = (
'{nodeid}: in "parametrize" the number of names ({names_len}):\n'
" {names}\n"
Expand All @@ -227,7 +236,7 @@ def _for_parametrize(
values=param.values,
names=argnames,
names_len=len(argnames),
values_len=len(param.values),
values_len=values_len,
),
pytrace=False,
)
Expand Down
20 changes: 20 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,26 @@ def test_func(foo, bar):
)


def test_parametrized_collect_with_non_sequence_value(pytester: Pytester) -> None:
"""Test parametrization reports non-sequence values without crashing."""
py_file = pytester.makepyfile(
"""
import pytest

@pytest.mark.parametrize("value,", [None])
def test_func(value):
pass
"""
)

result = pytester.runpytest(py_file)
result.stdout.fnmatch_lines(
[
'*in "parametrize" expected a sequence of values, got None',
]
)


def test_parametrized_with_kwargs(pytester: Pytester) -> None:
"""Test collect parametrized func with wrong number of args."""
py_file = pytester.makepyfile(
Expand Down
Loading