From c39a9407f2270592b7c5649527334d77f8198b45 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Fri, 10 Apr 2026 08:41:23 +0200 Subject: [PATCH 1/2] Fix support for pytest-main. Fixes #323 --- CHANGES.rst | 4 ++++ src/pytest_rerunfailures.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 8638e1c..0810fb0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -7,6 +7,10 @@ Changelog - Fix missing teardown for session and module scoped fixtures when fixture teardown fails. Fixes `#314 `_. +- Clear fixture finalizers when removing cached results from failed fixtures + to fix compatibility with pytest >= 9, which asserts that ``_finalizers`` is + empty before executing a fixture. + 16.1 (2025-10-10) ----------------- diff --git a/src/pytest_rerunfailures.py b/src/pytest_rerunfailures.py index 63223ae..a2cceed 100644 --- a/src/pytest_rerunfailures.py +++ b/src/pytest_rerunfailures.py @@ -244,6 +244,11 @@ def _remove_cached_results_from_failed_fixtures(item): result, _, err = getattr(fixture_def, cached_result) if err: # Deleting cached results for only failed fixtures setattr(fixture_def, cached_result, None) + # Clear finalizers registered during the failed execution + # so the fixture can be re-executed cleanly (pytest >= 9 + # asserts _finalizers is empty before executing a fixture). + if hasattr(fixture_def, "_finalizers"): + fixture_def._finalizers.clear() def _remove_failed_setup_state_from_session(item): From 86df76a57992f78f4ec30c02cf159073ee836997 Mon Sep 17 00:00:00 2001 From: Michael Howitz Date: Fri, 10 Apr 2026 08:48:19 +0200 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 0810fb0..ed6571b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -10,6 +10,7 @@ Changelog - Clear fixture finalizers when removing cached results from failed fixtures to fix compatibility with pytest >= 9, which asserts that ``_finalizers`` is empty before executing a fixture. + Fixes `#323 `_. 16.1 (2025-10-10)