Skip to content

Commit

Permalink
Allow "name" argument on url_for() and url_path_for() (#2050)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel <709115+dansan@users.noreply.github.com>
  • Loading branch information
Kludex and dansan committed Mar 6, 2023
1 parent 5472c44 commit b3add7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
5 changes: 3 additions & 2 deletions starlette/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ def build_middleware_stack(self) -> ASGIApp:
def routes(self) -> typing.List[BaseRoute]:
return self.router.routes

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
return self.router.url_path_for(name, **path_params)
# TODO: Make `__name` a positional-only argument when we drop Python 3.7 support.
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
return self.router.url_path_for(__name, **path_params)

async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
scope["app"] = self
Expand Down
4 changes: 2 additions & 2 deletions starlette/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ def state(self) -> State:
self._state = State(self.scope["state"])
return self._state

def url_for(self, name: str, **path_params: typing.Any) -> URL:
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)
url_path = router.url_path_for(__name, **path_params)
return url_path.make_absolute_url(base_url=self.base_url)


Expand Down
44 changes: 22 additions & 22 deletions starlette/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class BaseRoute:
def matches(self, scope: Scope) -> typing.Tuple[Match, Scope]:
raise NotImplementedError() # pragma: no cover

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
raise NotImplementedError() # pragma: no cover

async def handle(self, scope: Scope, receive: Receive, send: Send) -> None:
Expand Down Expand Up @@ -249,12 +249,12 @@ def matches(self, scope: Scope) -> typing.Tuple[Match, Scope]:
return Match.FULL, child_scope
return Match.NONE, {}

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
seen_params = set(path_params.keys())
expected_params = set(self.param_convertors.keys())

if name != self.name or seen_params != expected_params:
raise NoMatchFound(name, path_params)
if __name != self.name or seen_params != expected_params:
raise NoMatchFound(__name, path_params)

path, remaining_params = replace_params(
self.path_format, self.param_convertors, path_params
Expand Down Expand Up @@ -324,12 +324,12 @@ def matches(self, scope: Scope) -> typing.Tuple[Match, Scope]:
return Match.FULL, child_scope
return Match.NONE, {}

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
seen_params = set(path_params.keys())
expected_params = set(self.param_convertors.keys())

if name != self.name or seen_params != expected_params:
raise NoMatchFound(name, path_params)
if __name != self.name or seen_params != expected_params:
raise NoMatchFound(__name, path_params)

path, remaining_params = replace_params(
self.path_format, self.param_convertors, path_params
Expand Down Expand Up @@ -406,22 +406,22 @@ def matches(self, scope: Scope) -> typing.Tuple[Match, Scope]:
return Match.FULL, child_scope
return Match.NONE, {}

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
if self.name is not None and name == self.name and "path" in path_params:
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
if self.name is not None and __name == self.name and "path" in path_params:
# 'name' matches "<mount_name>".
path_params["path"] = path_params["path"].lstrip("/")
path, remaining_params = replace_params(
self.path_format, self.param_convertors, path_params
)
if not remaining_params:
return URLPath(path=path)
elif self.name is None or name.startswith(self.name + ":"):
elif self.name is None or __name.startswith(self.name + ":"):
if self.name is None:
# No mount name.
remaining_name = name
remaining_name = __name
else:
# 'name' matches "<mount_name>:<child_name>".
remaining_name = name[len(self.name) + 1 :]
remaining_name = __name[len(self.name) + 1 :]
path_kwarg = path_params.get("path")
path_params["path"] = ""
path_prefix, remaining_params = replace_params(
Expand All @@ -437,7 +437,7 @@ def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
)
except NoMatchFound:
pass
raise NoMatchFound(name, path_params)
raise NoMatchFound(__name, path_params)

async def handle(self, scope: Scope, receive: Receive, send: Send) -> None:
await self.app(scope, receive, send)
Expand Down Expand Up @@ -484,22 +484,22 @@ def matches(self, scope: Scope) -> typing.Tuple[Match, Scope]:
return Match.FULL, child_scope
return Match.NONE, {}

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
if self.name is not None and name == self.name and "path" in path_params:
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
if self.name is not None and __name == self.name and "path" in path_params:
# 'name' matches "<mount_name>".
path = path_params.pop("path")
host, remaining_params = replace_params(
self.host_format, self.param_convertors, path_params
)
if not remaining_params:
return URLPath(path=path, host=host)
elif self.name is None or name.startswith(self.name + ":"):
elif self.name is None or __name.startswith(self.name + ":"):
if self.name is None:
# No mount name.
remaining_name = name
remaining_name = __name
else:
# 'name' matches "<mount_name>:<child_name>".
remaining_name = name[len(self.name) + 1 :]
remaining_name = __name[len(self.name) + 1 :]
host, remaining_params = replace_params(
self.host_format, self.param_convertors, path_params
)
Expand All @@ -509,7 +509,7 @@ def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
return URLPath(path=str(url), protocol=url.protocol, host=host)
except NoMatchFound:
pass
raise NoMatchFound(name, path_params)
raise NoMatchFound(__name, path_params)

async def handle(self, scope: Scope, receive: Receive, send: Send) -> None:
await self.app(scope, receive, send)
Expand Down Expand Up @@ -634,13 +634,13 @@ async def not_found(self, scope: Scope, receive: Receive, send: Send) -> None:
response = PlainTextResponse("Not Found", status_code=404)
await response(scope, receive, send)

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
def url_path_for(self, __name: str, **path_params: typing.Any) -> URLPath:
for route in self.routes:
try:
return route.url_path_for(name, **path_params)
return route.url_path_for(__name, **path_params)
except NoMatchFound:
pass
raise NoMatchFound(name, path_params)
raise NoMatchFound(__name, path_params)

async def startup(
self, state: typing.Optional[typing.Dict[str, typing.Any]]
Expand Down

0 comments on commit b3add7e

Please sign in to comment.