Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change url_for signature to return a URL instance #1385

Merged
merged 4 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ app = Starlette(routes=routes)
You'll often want to be able to generate the URL for a particular route,
such as in cases where you need to return a redirect response.

* Signature: `url_for(name, **path_params) -> URL`

```python
routes = [
Route("/", homepage, name="homepage")
Expand Down
4 changes: 2 additions & 2 deletions starlette/datastructures.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __init__(self, path: str, protocol: str = "", host: str = "") -> None:
self.protocol = protocol
self.host = host

def make_absolute_url(self, base_url: typing.Union[str, URL]) -> str:
def make_absolute_url(self, base_url: typing.Union[str, URL]) -> URL:
if isinstance(base_url, str):
base_url = URL(base_url)
if self.protocol:
Expand All @@ -200,7 +200,7 @@ def make_absolute_url(self, base_url: typing.Union[str, URL]) -> str:

netloc = self.host or base_url.netloc
path = base_url.path.rstrip("/") + str(self)
return str(URL(scheme=scheme, netloc=netloc, path=path))
return URL(scheme=scheme, netloc=netloc, path=path)


class Secret:
Expand Down
2 changes: 1 addition & 1 deletion starlette/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def state(self) -> State:
self._state = State(self.scope["state"])
return self._state

def url_for(self, name: str, **path_params: typing.Any) -> str:
def url_for(self, name: str, **path_params: typing.Any) -> URL:
router: Router = self.scope["router"]
url_path = router.url_path_for(name, **path_params)
return url_path.make_absolute_url(base_url=self.base_url)
Expand Down
3 changes: 2 additions & 1 deletion starlette/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from os import PathLike

from starlette.background import BackgroundTask
from starlette.datastructures import URL
from starlette.requests import Request
from starlette.responses import Response
from starlette.types import Receive, Scope, Send
Expand Down Expand Up @@ -77,7 +78,7 @@ def _create_env(
self, directory: typing.Union[str, PathLike], **env_options: typing.Any
) -> "jinja2.Environment":
@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
def url_for(context: dict, name: str, **path_params: typing.Any) -> URL:
request = context["request"]
return request.url_for(name, **path_params)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ def test_subdomain_reverse_urls():
async def echo_urls(request):
return JSONResponse(
{
"index": request.url_for("index"),
"submount": request.url_for("mount:submount"),
"index": str(request.url_for("index")),
"submount": str(request.url_for("mount:submount")),
}
)

Expand Down