Skip to content

Improve parametrize error for scalar parameter sets#14621

Open
mike-lmctl wants to merge 1 commit into
pytest-dev:mainfrom
mike-lmctl:fix-parametrize-scalar-values-error
Open

Improve parametrize error for scalar parameter sets#14621
mike-lmctl wants to merge 1 commit into
pytest-dev:mainfrom
mike-lmctl:fix-parametrize-scalar-values-error

Conversation

@mike-lmctl

Copy link
Copy Markdown

Closes #14619.

This turns a TypeError during collection into a normal parametrize collection error when tuple-style argnames receive a scalar parameter set.

For example, @pytest.mark.parametrize("x,", [None]) now points at the offending test and reports that the parameter set should be a sequence, instead of showing an internal len(None) crash.

I added a regression test for the None case and a changelog entry.

I used AI assistance while preparing this change, reviewed the patch locally, and take responsibility for the contribution.

Checks run locally:

PYTHONPATH=src python3 -m pytest testing/test_mark.py::test_parametrized_collect_with_non_sequence_values testing/test_mark.py::test_parametrized_collect_with_wrong_args -q
PYTHONPATH=src python3 -m pytest testing/test_mark.py -q
git diff --check -- src/_pytest/mark/structures.py testing/test_mark.py changelog/14619.bugfix.rst AUTHORS
python3 -m py_compile src/_pytest/mark/structures.py testing/test_mark.py

testing/python/metafunc.py was not run locally because hypothesis is not installed in this environment.

Checklist:

@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jun 17, 2026
Tuple-style parametrization expects each parameter set to be a sequence of values. When a scalar like None was passed, pytest crashed while trying to format the validation error because it called len() on the scalar.

Report a normal collection error instead, pointing at the offending test.

Tested: PYTHONPATH=src python3 -m pytest testing/test_mark.py::test_parametrized_collect_with_non_sequence_values testing/test_mark.py::test_parametrized_collect_with_wrong_args -q

Tested: PYTHONPATH=src python3 -m pytest testing/test_mark.py -q

Tested: git diff --check -- src/_pytest/mark/structures.py testing/test_mark.py changelog/14619.bugfix.rst AUTHORS

Tested: python3 -m py_compile src/_pytest/mark/structures.py testing/test_mark.py

Not-tested: testing/python/metafunc.py because hypothesis is not installed in this environment.

Co-authored-by: OpenAI Codex <codex@openai.com>
@mike-lmctl mike-lmctl force-pushed the fix-parametrize-scalar-values-error branch from bc13fe9 to 76907f3 Compare June 25, 2026 17:40

@DTiming24 DTiming24 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good improvement turning len(None) into a collection error. Since the repro in the body uses @pytest.mark.parametrize("x,", [None]), I think the error message should probably mention the single-value tuple syntax explicitly (('value',)), because that's the quickest fix users can make.

@DTiming24

This comment was marked as low quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spurious comma in parametrize causes obfuscated crash

2 participants