Skip to content

Restore settings active at settings fixture setup - #1321

Open
Cherith1222 wants to merge 1 commit into
pytest-dev:mainfrom
Cherith1222:fix/809-settings-override-leak
Open

Cherith1222 wants to merge 1 commit into
pytest-dev:mainfrom
Cherith1222:fix/809-settings-override-leak

Conversation

@Cherith1222

Copy link
Copy Markdown

Summary

The settings fixture saved Django's settings object when the fixture was created, then restored that object at teardown. If override_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.settings raised RecursionError.

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.rst already 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):

  • With pytest_django/fixtures.py at main (ad5867df): python -m pytest tests/test_fixtures.py -k test_override_settings_exited_before_teardown -p no:cacheprovider → 1 failed. The inner test_settings_restored saw OVERRIDDEN still set (hasattr was true). 54 deselected.
  • With this commit: the same command → 1 passed, 54 deselected.
  • ruff 0.16.1 check and format --check passed on pytest_django/fixtures.py and tests/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-by or Signed-off-by trailer.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RecursionError when mixing use of override_settings and settings fixture

1 participant