Skip to content

Commit 438edcd

Browse files
committedSep 30, 2023
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.
1 parent b7c1290 commit 438edcd

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed
 

‎CHANGES.rst

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Unreleased
88
``importlib.metadata.version("flask")``, instead. :issue:`5230`
99
- Restructure the code such that the Flask (app) and Blueprint
1010
classes have Sans-IO bases. :pr:`5127`
11+
- Allow self as an argument to url_for. :pr:`5264`
1112

1213

1314
Version 2.3.3

‎src/flask/app.py

+1
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ def async_to_sync(
952952

953953
def url_for(
954954
self,
955+
/,
955956
endpoint: str,
956957
*,
957958
_anchor: str | None = None,

‎tests/test_helpers.py

+7
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ def post(self):
161161
assert flask.url_for("myview", id=42, _method="GET") == "/myview/42"
162162
assert flask.url_for("myview", _method="POST") == "/myview/create"
163163

164+
def test_url_for_with_self(self, app, req_ctx):
165+
@app.route("/<self>")
166+
def index(self):
167+
return "42"
168+
169+
assert flask.url_for("index", self="2") == "/2"
170+
164171

165172
def test_redirect_no_app():
166173
response = flask.redirect("https://localhost", 307)

0 commit comments

Comments
 (0)
Please sign in to comment.