From 29cc2a3e8ecdfbf9b8546a589e3a9acfbda08095 Mon Sep 17 00:00:00 2001 From: Ishaan Kapur <64529428+ishaanlabs-gg@users.noreply.github.com> Date: Thu, 2 Jul 2026 21:44:59 +0530 Subject: [PATCH] Fix pytest 9.1 monkeypatch sentinel imports --- src/flask/cli.py | 13 ++++++++++++- tests/conftest.py | 16 ++++++++++------ tests/test_blueprints.py | 7 ++++++- tests/test_cli.py | 11 ++++++++--- tests/test_views.py | 7 ++++++- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/flask/cli.py b/src/flask/cli.py index 1a9159ec7c..bd8845138f 100644 --- a/src/flask/cli.py +++ b/src/flask/cli.py @@ -777,7 +777,18 @@ def show_server_banner(debug: bool, app_import_path: str | None) -> None: click.echo(f" * Debug mode: {'on' if debug else 'off'}") -class CertParamType(click.ParamType[t.Any]): +if t.TYPE_CHECKING: + + class _CertParamTypeBase(click.ParamType[t.Any]): + pass + +else: + + class _CertParamTypeBase(click.ParamType): + pass + + +class CertParamType(_CertParamTypeBase): """Click option type for the ``--cert`` option. Allows either an existing file, the string ``'adhoc'``, or an import for a :class:`~ssl.SSLContext` object. diff --git a/tests/conftest.py b/tests/conftest.py index 0414b9e22f..913f63ae57 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,6 +7,10 @@ from flask import Flask from flask.globals import app_ctx as _app_ctx +_MONKEYPATCH_NOTSET = ( + monkeypatch.NOTSET if hasattr(monkeypatch, "NOTSET") else monkeypatch.notset +) + @pytest.fixture(scope="session", autouse=True) def _standard_os_environ(): @@ -16,15 +20,15 @@ def _standard_os_environ(): """ 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), + (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), ) for _, key, value in out: - if value is monkeypatch.notset: + if value is _MONKEYPATCH_NOTSET: mp.delenv(key, False) else: mp.setenv(key, value) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 6eb7d1d521..dc93f8faaa 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -1,6 +1,11 @@ import pytest from jinja2 import TemplateNotFound -from werkzeug.http import parse_cache_control_header +from werkzeug.datastructures.cache_control import ResponseCacheControl + +if hasattr(ResponseCacheControl, "from_header"): + parse_cache_control_header = ResponseCacheControl.from_header +else: + from werkzeug.http import parse_cache_control_header import flask diff --git a/tests/test_cli.py b/tests/test_cli.py index 2a34088bd5..2673bb412a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -11,7 +11,7 @@ import click import pytest -from _pytest.monkeypatch import notset +from _pytest import monkeypatch as _pytest_monkeypatch from click.testing import CliRunner from flask import Blueprint @@ -31,6 +31,11 @@ cwd = Path.cwd() test_path = (Path(__file__) / ".." / "test_apps").resolve() +_MONKEYPATCH_NOTSET = ( + _pytest_monkeypatch.NOTSET + if hasattr(_pytest_monkeypatch, "NOTSET") + else _pytest_monkeypatch.notset +) @pytest.fixture @@ -537,7 +542,7 @@ def dotenv_not_available(): 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._setitem.append((os.environ, item, _MONKEYPATCH_NOTSET)) monkeypatch.setenv("EGGS", "3") monkeypatch.chdir(test_path) @@ -560,7 +565,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._setitem.append((os.environ, item, _MONKEYPATCH_NOTSET)) load_dotenv(test_path / ".flaskenv") assert Path.cwd() == cwd diff --git a/tests/test_views.py b/tests/test_views.py index d002b50439..2cfa30af3d 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,5 +1,10 @@ import pytest -from werkzeug.http import parse_set_header +from werkzeug.datastructures import HeaderSet + +if hasattr(HeaderSet, "from_header"): + parse_set_header = HeaderSet.from_header +else: + from werkzeug.http import parse_set_header import flask.views from flask.testing import FlaskClient