Skip to content

Replace the use of private monkeypatch fixture API - #6095

Merged
davidism merged 1 commit into
pallets:stablefrom
mgorny:flask-delenv
Jul 30, 2026
Merged

Replace the use of private monkeypatch fixture API#6095
davidism merged 1 commit into
pallets:stablefrom
mgorny:flask-delenv

Conversation

@mgorny

@mgorny mgorny commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Replace the use of private monkeypatch API with the standard invocations to fix compatibility with pytest 9.1, as well as to make the tests more reliable in the future.

The previous code operated on monkeypatch fixture internals to trigger clearing environment variables expected by tests at the beginning of tests, and reverting any changes made by the tests. However, this does not seem strictly valid, and it actually lead to tests making wrong assumptions. For example, test_disable_dotenv_from_env relied on a previous test unsetting FOO in the environment, and failed if FOO was set while it was run alone.

The new logic uses public API only: it cleans up the standard set of environment variables for every test, and additionally cleans up variables specifically used by dotenv tests. With this approach, it is entirely possible for a test to leave the environment "dirty"; however, that's fine since other tests need to clean up the environment anyway to account for user-set environment variables.

Fixes #6071

Comment thread tests/conftest.py
standard values. Returns a list of operations that is used by
:func:`._reset_os_environ` after each test.
"""
mp = monkeypatch.MonkeyPatch()

@RonnyPfannschmidt RonnyPfannschmidt Jul 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actualy keep this fixture and use

with pytest.MonkeyPatch.context() as mp:
...
yield ....

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry; actually, does that change anything? I mean, either way we need a function scope fixture, and my understanding is that it will undo the changes just the same.

@davidism
davidism changed the base branch from main to stable July 30, 2026 17:02
@davidism
davidism merged commit 548994e into pallets:stable Jul 30, 2026
14 of 15 checks passed
@mgorny

mgorny commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

Thank you!

@mgorny
mgorny deleted the flask-delenv branch July 30, 2026 18:53
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.

Tests fail with pytest 9.1: _pytest.monkeypatch.notset removed

3 participants