Issue a fresh PytestWarning instance per warn() call - #15095
Closed
Dextheking1 wants to merge 1 commit into
Closed
Dextheking1 wants to merge 1 commit into
Dextheking1 wants to merge 1 commit into
Conversation
Convert the module-level deprecation constants in _pytest.deprecated from shared PytestWarning instances to UnformattedWarning, formatted into a fresh instance at each warn site. Reusing the same instance across warnings.warn() calls made CPython append to the instance's existing __traceback__ on every raise under -W error, corrupting failure reports with stale frames from previous raises. Fixes pytest-dev#14912.
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.
Fixes #14912.
src/_pytest/deprecated.pystored deprecations as module-levelPytestWarninginstances that call sites passed straight to
warnings.warn(). When warningsare errors,
warnraises that exact object, and CPython appends to anexception's existing
__traceback__instead of resetting it, so each raisegrew the traceback of a process-lifetime global (measured 2 → 4 → 6 → 8 frames
over four raises). With two tests tripping the same deprecation, the second
report carried 20+ stale frames from the first run.
All 12 plain-instance constants are now
UnformattedWarning, formatted into afresh instance at each warn site (the 4 existing
UnformattedWarningconstantswere already fine). The module docstring now documents the invariant.
Tests: added
test_deprecation_constants_are_not_shared_instances(structuralguard over every constant in the module) and
test_deprecation_warning_traceback_does_not_accumulate_under_error(raises thesame deprecation 3x under
-W error, asserts stable frame counts) intesting/deprecated_test.py. Both fail on pristine code, pass with the fix.testing/deprecated_test.py,testing/test_warnings.py,testing/test_warning_types.py(105 passed, 4 skipped),testing/test_pastebin.py+testing/test_monkeypatch.py(54 passed, 1 skipped)all green locally.