Skip to content

Replace the use of private monkeypatch fixture API#6095

Open
mgorny wants to merge 1 commit into
pallets:mainfrom
mgorny:flask-delenv
Open

Replace the use of private monkeypatch fixture API#6095
mgorny wants to merge 1 commit into
pallets:mainfrom
mgorny:flask-delenv

Conversation

@mgorny

@mgorny mgorny commented Jul 14, 2026

Copy link
Copy Markdown

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

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 pallets#6071

Signed-off-by: Michał Górny <mgorny@gentoo.org>
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
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
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.

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

2 participants