Skip to content

Commit

Permalink
Allow self as an argument to url_for
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pgjones committed Sep 30, 2023
1 parent b7c1290 commit 438edcd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ def async_to_sync(

def url_for(
self,
/,
endpoint: str,
*,
_anchor: str | None = None,
Expand Down
7 changes: 7 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("/<self>")
def index(self):
return "42"

assert flask.url_for("index", self="2") == "/2"


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

0 comments on commit 438edcd

Please sign in to comment.