fix: prevent NOTSET sentinel leaking as test ID in parametrize with callable ids#14548
Closed
rusthype wants to merge 2 commits into
Closed
fix: prevent NOTSET sentinel leaking as test ID in parametrize with callable ids#14548rusthype wants to merge 2 commits into
rusthype wants to merge 2 commits into
Conversation
…allable ids When @pytest.mark.parametrize is used with an empty argvalues list and a callable ids parameter, pytest internally creates a placeholder ParameterSet with NOTSET sentinel values. Previously this placeholder used id='NOTSET', which caused the internal string to leak into the test output as 'test_foo[NOTSET]'. Additionally, if an ids callable was provided, it would be invoked with the NOTSET sentinel and crash with an AttributeError or similar. Fix: - Use id=None for the placeholder ParameterSet so the standard ID generation runs (produces e.g. 'test_foo[x0]' matching the existing workaround behavior) - Guard _idval_from_function to return None early when val is NOTSET, preventing the user's callable from being invoked with the sentinel Fixes pytest-dev#13235
for more information, see https://pre-commit.ci
Member
|
The NOTSET in this case is intentional. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #13235
When
@pytest.mark.parametrizeis used with an emptyargvalueslist and a callableidsparameter, two bugs occur:NOTSETsentinel string leaks into test output astest_foo[NOTSET]idscallable gets invoked with theNOTSETsentinel, causing anAttributeErrororValueErrorBefore:
test_foo[NOTSET]in output (string leaks) or crashAfter:
test_foo[matrix0]— clean, matches the auto-naming conventionRoot Cause
In
ParameterSet._for_parametrize(), whenargvalues=[], a placeholderParameterSetis created withid="NOTSET". This string was then yielded verbatim in_resolve_ids()(sinceid is not None), leaking the internal name. If the user provided a callableids, it was also called on theNOTSETsentinel value beforeid="NOTSET"was set (in older versions) or was bypassed (current main), but the string still leaked.Fix
src/_pytest/mark/structures.py— useid=Nonefor the placeholderParameterSetso standard ID generation runs:src/_pytest/python.py— guard_idval_from_functionto skip calling the user's callable whenval is NOTSET:This follows the existing pattern in
_idval_from_valuewhich already has a special case forNOTSET.Test
Added
TestMetafuncFunctional::test_parametrize_empty_argvalues_callable_idsintesting/python/metafunc.pyverifying:NOTSETappears in outputChecklist
changelog/13235.bugfix.rst)