Skip to content

Improve parametrize error for scalar parameter sets - #14621

Closed
mike-lmctl wants to merge 2 commits into
pytest-dev:mainfrom
mike-lmctl:fix-parametrize-scalar-values-error
Closed

mike-lmctl wants to merge 2 commits into
pytest-dev:mainfrom
mike-lmctl:fix-parametrize-scalar-values-error

Conversation

@mike-lmctl

Copy link
Copy Markdown
Contributor

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:

@pyba-chronographer pyba-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.

The collection error now points users at the immediate fix for tuple-style single-argument parametrization: wrapping the value as a one-element tuple.

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: git diff --check -- src/_pytest/mark/structures.py testing/test_mark.py

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

Copy link
Copy Markdown

This was closed today without a merge. Is #14619 available for someone else to take over, or do you plan to continue it? I would like to improve the parameter-value diagnostics while preserving the existing tuple-style behaviour.

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually please reopen

This slipped out of my review window

It's Actually good to go

@user01010111

Copy link
Copy Markdown

The source fork is currently unavailable: mike-lmctl/pytest returns 404 from the GitHub API and Git reports “Repository not found”; it is also absent from the author’s public repository list. GitHub still retains the approved head 7cab31585b04c4b83727bd337340a22dd0d3c414 under this PR.

@RonnyPfannschmidt, if the author cannot restore the source and reopen this PR, would you prefer the approved patch carried into a fresh PR with Mike Ma’s authorship and credit preserved? I will hold off on a replacement pending agreement.

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

I just had Claude do a preservation

Ill wait a bit to see if comments from @mike-lmctl come in and merge mid week unless a complaint comes up

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

4 participants