From c49c7db48deaf9fae11a7f1782d65370be89c37a Mon Sep 17 00:00:00 2001 From: aouxwoux <81102668+aouxwoux@users.noreply.github.com> Date: Fri, 10 Jul 2026 17:18:44 +0530 Subject: [PATCH 1/2] Improve parametrize error for non-sequence values Signed-off-by: aouxwoux <81102668+aouxwoux@users.noreply.github.com> --- src/_pytest/mark/structures.py | 13 +++++++++++-- testing/test_mark.py | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/_pytest/mark/structures.py b/src/_pytest/mark/structures.py index 5449b17a1c6..07c202fb952 100644 --- a/src/_pytest/mark/structures.py +++ b/src/_pytest/mark/structures.py @@ -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" @@ -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, ) diff --git a/testing/test_mark.py b/testing/test_mark.py index 253cda94503..78a8c88e7e4 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -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( From 01ac732aa52abe17c981a145b5d1940f90246a24 Mon Sep 17 00:00:00 2001 From: aouxwoux <81102668+aouxwoux@users.noreply.github.com> Date: Fri, 10 Jul 2026 17:19:46 +0530 Subject: [PATCH 2/2] Add changelog for parametrize error Signed-off-by: aouxwoux <81102668+aouxwoux@users.noreply.github.com> --- changelog/14619.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/14619.bugfix.rst diff --git a/changelog/14619.bugfix.rst b/changelog/14619.bugfix.rst new file mode 100644 index 00000000000..e50fc05dd5f --- /dev/null +++ b/changelog/14619.bugfix.rst @@ -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``.