Skip to content

Commit

Permalink
change url_for signature to return URL
Browse files Browse the repository at this point in the history
  • Loading branch information
aminalaee committed Feb 13, 2023
1 parent 5771a78 commit 27eab5d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
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
4 changes: 2 additions & 2 deletions starlette/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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 @@ -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")),
}
)

Expand Down

0 comments on commit 27eab5d

Please sign in to comment.