From f1946be8e487d2029b39198fc2ee7e3046ce8a15 Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Mon, 20 Jul 2026 07:19:48 +0530 Subject: [PATCH] tests: replace private _pytest.monkeypatch.notset with public delenv API pytest 9.1 removed the notset sentinel from _pytest.monkeypatch, moving it to _pytest.compat.NOTSET (a private location). Importing from either private path breaks on pytest 9.1+. Both usages only needed notset to signal 'delete this env var before the test runs'. monkeypatch.delenv(key, raising=False) does exactly that using the stable public API, so drop the sentinel entirely. tests/conftest.py: - Remove 'from _pytest import monkeypatch' import. - Replace notset sentinel tuple with a plain tuple of key names. - Use pytest.MonkeyPatch() directly (public class). - _reset_os_environ now calls monkeypatch.delenv() per key instead of appending to the private _setitem list. tests/test_cli.py: - Remove 'from _pytest.monkeypatch import notset'. - Replace _setitem.append(..., notset) with monkeypatch.delenv(raising=False) in test_load_dotenv and test_dotenv_path. Fixes #6071 --- tests/conftest.py | 41 ++++++++++++++++++----------------------- tests/test_cli.py | 6 ++---- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 0414b9e22f..aefe3cf54d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,43 +2,38 @@ import sys import pytest -from _pytest import monkeypatch from flask import Flask from flask.globals import app_ctx as _app_ctx +_FLASK_ENV_KEYS = ( + "FLASK_ENV_FILE", + "FLASK_APP", + "FLASK_DEBUG", + "FLASK_RUN_FROM_CLI", + "WERKZEUG_RUN_MAIN", +) + @pytest.fixture(scope="session", autouse=True) def _standard_os_environ(): - """Set up ``os.environ`` at the start of the test session to have - standard values. Returns a list of operations that is used by - :func:`._reset_os_environ` after each test. - """ - mp = monkeypatch.MonkeyPatch() - out = ( - (os.environ, "FLASK_ENV_FILE", monkeypatch.notset), - (os.environ, "FLASK_APP", monkeypatch.notset), - (os.environ, "FLASK_DEBUG", monkeypatch.notset), - (os.environ, "FLASK_RUN_FROM_CLI", monkeypatch.notset), - (os.environ, "WERKZEUG_RUN_MAIN", monkeypatch.notset), - ) + """Remove Flask-related environment variables at the start of the test + session so they don't leak in from the developer's shell.""" + mp = pytest.MonkeyPatch() - for _, key, value in out: - if value is monkeypatch.notset: - mp.delenv(key, False) - else: - mp.setenv(key, value) + for key in _FLASK_ENV_KEYS: + mp.delenv(key, raising=False) - yield out + yield mp.undo() @pytest.fixture(autouse=True) def _reset_os_environ(monkeypatch, _standard_os_environ): - """Reset ``os.environ`` to the standard environ after each test, - in case a test changed something without cleaning up. - """ - monkeypatch._setitem.extend(_standard_os_environ) + """Remove Flask-related environment variables before each test in case a + previous test set them without cleaning up.""" + for key in _FLASK_ENV_KEYS: + monkeypatch.delenv(key, raising=False) @pytest.fixture diff --git a/tests/test_cli.py b/tests/test_cli.py index 2a34088bd5..e605b2b63e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -11,7 +11,6 @@ import click import pytest -from _pytest.monkeypatch import notset from click.testing import CliRunner from flask import Blueprint @@ -535,9 +534,8 @@ def dotenv_not_available(): @need_dotenv def test_load_dotenv(monkeypatch): - # can't use monkeypatch.delitem since the keys don't exist yet for item in ("FOO", "BAR", "SPAM", "HAM"): - monkeypatch._setitem.append((os.environ, item, notset)) + monkeypatch.delenv(item, raising=False) monkeypatch.setenv("EGGS", "3") monkeypatch.chdir(test_path) @@ -560,7 +558,7 @@ def test_load_dotenv(monkeypatch): @need_dotenv def test_dotenv_path(monkeypatch): for item in ("FOO", "BAR", "EGGS"): - monkeypatch._setitem.append((os.environ, item, notset)) + monkeypatch.delenv(item, raising=False) load_dotenv(test_path / ".flaskenv") assert Path.cwd() == cwd