Repository navigation
Restore settings active at settings fixture setup - #1321
Open
Cherith1222 wants to merge 1 commit into
Open
Cherith1222 wants to merge 1 commit into
Cherith1222 wants to merge 1 commit into
Conversation
When an override_settings() was entered after the settings fixture was set up but exited before the fixture's teardown, finalizing the fixture restored that override's settings, leaking them into later tests and, after enough repetitions, causing a RecursionError (pytest-dev#809). The settings fixture now enables an empty override_settings() when it is created and disables it last, so teardown always returns to the settings that were active when the fixture was set up. AI-assisted (Cursor). Not yet reviewed by a human.
This branch has not been deployed
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
The
settingsfixture saved Django's settings object when the fixture was created, then restored that object at teardown. Ifoverride_settings()was entered after the fixture was set up and exited before the fixture was torn down, teardown restored the override's settings instead of the settings that were active at setup. Those values leaked into later tests and, after enough repetitions,django.conf.settingsraisedRecursionError.The fixture now enables an empty
override_settings()when it is created and disables that override last, so teardown returns to the settings that were active at setup. This is the workaround described on the issue.Fixes #809.
docs/changelog.rstalready links the unreleased bugfix to issue #809.Tests
On Windows, Python 3.13.3, Django 6.1.2, pytest 9.1.1, in this checkout (CI also runs
py313-dj61-sqlite):pytest_django/fixtures.pyatmain(ad5867df):python -m pytest tests/test_fixtures.py -k test_override_settings_exited_before_teardown -p no:cacheprovider→ 1 failed. The innertest_settings_restoredsawOVERRIDDENstill set (hasattrwas true). 54 deselected.ruff0.16.1checkandformat --checkpassed onpytest_django/fixtures.pyandtests/test_fixtures.py.Not run: the rest of the suite, tox, mypy, PostgreSQL, MySQL, xdist, and non-Windows.
AI assistance
Drafted with Cursor. A human has not line-reviewed the diff. There is no
Co-authored-byorSigned-off-bytrailer.