Conversation
… caching
When `pytest_fixture_setup` raises (e.g. because `-Werror` turns a
`PytestRemovedIn10Warning` into an error), `FixtureDef.execute()` exits
without setting `cached_result`. This leaves whatever was already
appended to `self._finalizers` (the `pytest_fixture_post_finalizer`
lambda registered just before the call) sitting there permanently,
because `finish()` is a no-op when `cached_result is None`.
The next test that uses the same `FixtureDef` then calls `execute()`
again and hits:
assert not self._finalizers
with an internal `AssertionError`, masking the real deprecation error.
Fix: clear `self._finalizers` at the start of the "fresh execution" path
in `execute()`, before registering new finalizers. This is safe because:
- If no previous setup ran, the list is already empty.
- If a previous setup succeeded, `finish()` already cleared it.
- If a previous setup failed (our case), the stale entries are removed so
the next execution gets a clean slate.
Fixes pytest-dev#14775
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 #14775.
Problem
When
-Werrorturns aPytestRemovedIn10Warninginto an error during fixture setup,FixtureDef.execute()exits without settingcached_result. Thepytest_fixture_post_finalizerlambda that was just registered inself._finalizersstays there permanently —finish()is a no-op whencached_result is None(it guards with "already finished").The next test that uses the same
FixtureDefcallsexecute()again and hits:This internal
AssertionErrormasks the real error (the deprecation warning), making the second test's failure confusing.Root cause
Fix
Clear
self._finalizersat the top of the "fresh execution" path, before any new finalizers are registered. This is safe:clear()is a no-op.finish()already cleared_finalizers.Result
Both
test_1andtest_2now reportPytestRemovedIn10Warning(the real error) instead oftest_2reporting an internalAssertionError.Test
Added
test_class_scoped_instance_method_werror_multiple_testsintesting/deprecated_test.pythat reproduces the exact scenario from the issue and asserts noAssertionErrorappears.