From 17179082c241abe8e4884bf04ff59277765b8578 Mon Sep 17 00:00:00 2001 From: pgjones Date: Sun, 24 Sep 2023 16:16:55 +0100 Subject: [PATCH] Allow self as an argument to url_for This makes the Flask.url_for self argument positional only (Flask supports Python 3.8+) thereby restoring the ability to pass self as a value argument to url_for. --- CHANGES.rst | 1 + src/flask/app.py | 1 + tests/test_helpers.py | 7 +++++++ 3 files changed, 9 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index e5ca2fb174..3e50fe0fa8 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -8,6 +8,7 @@ Unreleased ``importlib.metadata.version("flask")``, instead. :issue:`5230` - Restructure the code such that the Flask (app) and Blueprint classes have Sans-IO bases. :pr:`5127` +- Allow self as an argument to url_for. :pr:`5264` Version 2.3.3 diff --git a/src/flask/app.py b/src/flask/app.py index ebb5a20254..d710cb96a4 100644 --- a/src/flask/app.py +++ b/src/flask/app.py @@ -952,6 +952,7 @@ def async_to_sync( def url_for( self, + /, endpoint: str, *, _anchor: str | None = None, diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 077cb43298..3566385cf1 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -161,6 +161,13 @@ def post(self): assert flask.url_for("myview", id=42, _method="GET") == "/myview/42" assert flask.url_for("myview", _method="POST") == "/myview/create" + def test_url_for_with_self(self, app, req_ctx): + @app.route("/") + def index(self): + return "42" + + assert flask.url_for("index", self="2") == "/2" + def test_redirect_no_app(): response = flask.redirect("https://localhost", 307)