From 9f9550247a2798b9dcc28081c15e266442f4e28a Mon Sep 17 00:00:00 2001 From: David Lord Date: Mon, 21 Aug 2023 09:39:10 -0700 Subject: [PATCH] bump werkzeug 2.3.7 use importlib.metadata.version --- CHANGES.rst | 2 +- pyproject.toml | 2 +- requirements/tests-pallets-min.in | 2 +- requirements/tests-pallets-min.txt | 4 +-- src/flask/cli.py | 9 ++++--- src/flask/testing.py | 15 ++++++++++- tests/test_cli.py | 12 ++++----- tests/test_testing.py | 40 ++++++++++++++++-------------- 8 files changed, 50 insertions(+), 36 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 3264a25e32..5a1adc3b44 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,7 +4,7 @@ Version 2.3.3 Unreleased - Python 3.12 compatibility. -- Require Werkzeug >= 2.3.6. +- Require Werkzeug >= 2.3.7. - Use ``flit_core`` instead of ``setuptools`` as build backend. - Refactor how an app's root and instance paths are determined. :issue:`5160` diff --git a/pyproject.toml b/pyproject.toml index 2c2832fc6c..7e5b21ffcc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -19,7 +19,7 @@ classifiers = [ ] requires-python = ">=3.8" dependencies = [ - "Werkzeug>=2.3.6", + "Werkzeug>=2.3.7", "Jinja2>=3.1.2", "itsdangerous>=2.1.2", "click>=8.1.3", diff --git a/requirements/tests-pallets-min.in b/requirements/tests-pallets-min.in index d216863385..68f3fc7bf0 100644 --- a/requirements/tests-pallets-min.in +++ b/requirements/tests-pallets-min.in @@ -1,4 +1,4 @@ -Werkzeug==2.3.6 +Werkzeug==2.3.7 Jinja2==3.1.2 MarkupSafe==2.1.1 itsdangerous==2.1.2 diff --git a/requirements/tests-pallets-min.txt b/requirements/tests-pallets-min.txt index 9778c74884..d1e8b1631c 100644 --- a/requirements/tests-pallets-min.txt +++ b/requirements/tests-pallets-min.txt @@ -1,4 +1,4 @@ -# SHA1:5bd33f1c06b3fb16f7ab50e80ed309655d110713 +# SHA1:fe057f95a98251b053eec8fa27df0feb722c70e8 # # This file is autogenerated by pip-compile-multi # To update, run: @@ -18,5 +18,5 @@ markupsafe==2.1.1 # -r requirements/tests-pallets-min.in # jinja2 # werkzeug -werkzeug==2.3.6 +werkzeug==2.3.7 # via -r requirements/tests-pallets-min.in diff --git a/src/flask/cli.py b/src/flask/cli.py index d2677aa564..dda266b30c 100644 --- a/src/flask/cli.py +++ b/src/flask/cli.py @@ -1,6 +1,7 @@ from __future__ import annotations import ast +import importlib.metadata import inspect import os import platform @@ -241,13 +242,13 @@ def get_version(ctx, param, value): if not value or ctx.resilient_parsing: return - import werkzeug - from . import __version__ + flask_version = importlib.metadata.version("flask") + werkzeug_version = importlib.metadata.version("werkzeug") click.echo( f"Python {platform.python_version()}\n" - f"Flask {__version__}\n" - f"Werkzeug {werkzeug.__version__}", + f"Flask {flask_version}\n" + f"Werkzeug {werkzeug_version}", color=ctx.color, ) ctx.exit() diff --git a/src/flask/testing.py b/src/flask/testing.py index 773f1525ee..69aa785188 100644 --- a/src/flask/testing.py +++ b/src/flask/testing.py @@ -1,5 +1,6 @@ from __future__ import annotations +import importlib.metadata import typing as t from contextlib import contextmanager from contextlib import ExitStack @@ -92,6 +93,18 @@ def json_dumps(self, obj: t.Any, **kwargs: t.Any) -> str: # type: ignore return self.app.json.dumps(obj, **kwargs) +_werkzeug_version = "" + + +def _get_werkzeug_version() -> str: + global _werkzeug_version + + if not _werkzeug_version: + _werkzeug_version = importlib.metadata.version("werkzeug") + + return _werkzeug_version + + class FlaskClient(Client): """Works like a regular Werkzeug test client but has knowledge about Flask's contexts to defer the cleanup of the request context until @@ -115,7 +128,7 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None: self._context_stack = ExitStack() self.environ_base = { "REMOTE_ADDR": "127.0.0.1", - "HTTP_USER_AGENT": f"werkzeug/{werkzeug.__version__}", + "HTTP_USER_AGENT": f"Werkzeug/{_get_werkzeug_version()}", } @contextmanager diff --git a/tests/test_cli.py b/tests/test_cli.py index 4b6995fec0..79de1fc8d1 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,8 @@ # This file was part of Flask-CLI and was modified under the terms of # its Revised BSD License. Copyright © 2015 CERN. +import importlib.metadata import os +import platform import ssl import sys import types @@ -227,10 +229,6 @@ def test_locate_app_suppress_raise(test_apps): def test_get_version(test_apps, capsys): - from flask import __version__ as flask_version - from werkzeug import __version__ as werkzeug_version - from platform import python_version - class MockCtx: resilient_parsing = False color = None @@ -241,9 +239,9 @@ def exit(self): ctx = MockCtx() get_version(ctx, None, "test") out, err = capsys.readouterr() - assert f"Python {python_version()}" in out - assert f"Flask {flask_version}" in out - assert f"Werkzeug {werkzeug_version}" in out + assert f"Python {platform.python_version()}" in out + assert f"Flask {importlib.metadata.version('flask')}" in out + assert f"Werkzeug {importlib.metadata.version('werkzeug')}" in out def test_scriptinfo(test_apps, monkeypatch): diff --git a/tests/test_testing.py b/tests/test_testing.py index 8703e047f9..6a90cc4504 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -1,6 +1,7 @@ +import importlib.metadata + import click import pytest -import werkzeug import flask from flask import appcontext_popped @@ -38,34 +39,35 @@ def index(): assert rv.data == b"http://localhost/" -def test_environ_base_default(app, client, app_ctx): +def test_environ_base_default(app, client): @app.route("/") def index(): - flask.g.user_agent = flask.request.headers["User-Agent"] - return flask.request.remote_addr + flask.g.remote_addr = flask.request.remote_addr + flask.g.user_agent = flask.request.user_agent.string + return "" - rv = client.get("/") - assert rv.data == b"127.0.0.1" - assert flask.g.user_agent == f"werkzeug/{werkzeug.__version__}" + with client: + client.get("/") + assert flask.g.remote_addr == "127.0.0.1" + assert flask.g.user_agent == ( + f"Werkzeug/{importlib.metadata.version('werkzeug')}" + ) -def test_environ_base_modified(app, client, app_ctx): +def test_environ_base_modified(app, client): @app.route("/") def index(): - flask.g.user_agent = flask.request.headers["User-Agent"] - return flask.request.remote_addr + flask.g.remote_addr = flask.request.remote_addr + flask.g.user_agent = flask.request.user_agent.string + return "" - client.environ_base["REMOTE_ADDR"] = "0.0.0.0" + client.environ_base["REMOTE_ADDR"] = "192.168.0.22" client.environ_base["HTTP_USER_AGENT"] = "Foo" - rv = client.get("/") - assert rv.data == b"0.0.0.0" - assert flask.g.user_agent == "Foo" - client.environ_base["REMOTE_ADDR"] = "0.0.0.1" - client.environ_base["HTTP_USER_AGENT"] = "Bar" - rv = client.get("/") - assert rv.data == b"0.0.0.1" - assert flask.g.user_agent == "Bar" + with client: + client.get("/") + assert flask.g.remote_addr == "192.168.0.22" + assert flask.g.user_agent == "Foo" def test_client_open_environ(app, client, request):