From 27eab5ddd5ef0c587f60c3d9b135a4ede9c858fc Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Mon, 13 Feb 2023 07:51:41 +0000 Subject: [PATCH 1/3] change url_for signature to return URL --- starlette/datastructures.py | 4 ++-- starlette/requests.py | 4 ++-- starlette/templating.py | 3 ++- tests/test_routing.py | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/starlette/datastructures.py b/starlette/datastructures.py index 48bd33fec..236f9fa43 100644 --- a/starlette/datastructures.py +++ b/starlette/datastructures.py @@ -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: @@ -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: diff --git a/starlette/requests.py b/starlette/requests.py index d924b501a..7194dd9da 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -173,10 +173,10 @@ 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) + return URL(url_path.make_absolute_url(base_url=self.base_url)) async def empty_receive() -> typing.NoReturn: diff --git a/starlette/templating.py b/starlette/templating.py index bccda36e4..ecea4f3a3 100644 --- a/starlette/templating.py +++ b/starlette/templating.py @@ -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 @@ -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) diff --git a/tests/test_routing.py b/tests/test_routing.py index 09beb8bb9..da4848b8d 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -521,8 +521,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")), } ) From b0119927f458ed8ec2adf36b3ec8234c1ae5ad2b Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Mon, 13 Feb 2023 07:56:01 +0000 Subject: [PATCH 2/3] fix lint --- starlette/requests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/starlette/requests.py b/starlette/requests.py index 7194dd9da..7652e3621 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -176,7 +176,7 @@ def state(self) -> State: 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(url_path.make_absolute_url(base_url=self.base_url)) + return url_path.make_absolute_url(base_url=self.base_url) async def empty_receive() -> typing.NoReturn: From ae7672186cc79a046c51d22d6779505caab917a8 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Mon, 13 Feb 2023 09:37:27 +0000 Subject: [PATCH 3/3] update docs --- docs/routing.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/routing.md b/docs/routing.md index 5a76d7bc4..fd1558793 100644 --- a/docs/routing.md +++ b/docs/routing.md @@ -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")