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

Allow 'name' in keyworded args for url_for() and url_path_for() #1364

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions starlette/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def debug(self, value: bool) -> None:
self._debug = value
self.middleware_stack = self.build_middleware_stack()

def url_path_for(self, name: str, **path_params: typing.Any) -> URLPath:
return self.router.url_path_for(name, **path_params)
def url_path_for(self, *args: str, **path_params: typing.Any) -> URLPath:
return self.router.url_path_for(*args, **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 @@ -169,9 +169,9 @@ 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, *args: str, **path_params: typing.Any) -> str:
router: Router = self.scope["router"]
url_path = router.url_path_for(name, **path_params)
url_path = router.url_path_for(*args, **path_params)
return url_path.make_absolute_url(base_url=self.base_url)


Expand Down
22 changes: 15 additions & 7 deletions starlette/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,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, *args: 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 @@ -235,7 +235,9 @@ 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, *args: str, **path_params: typing.Any) -> URLPath:
assert len(args) == 1, "url_path_for() takes exactly one positional argument"
name = args[0]
seen_params = set(path_params.keys())
expected_params = set(self.param_convertors.keys())

Expand Down Expand Up @@ -301,7 +303,9 @@ 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, *args: str, **path_params: typing.Any) -> URLPath:
assert len(args) == 1, "url_path_for() takes exactly one positional argument"
name = args[0]
seen_params = set(path_params.keys())
expected_params = set(self.param_convertors.keys())

Expand Down Expand Up @@ -374,7 +378,9 @@ 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, *args: str, **path_params: typing.Any) -> URLPath:
assert len(args) == 1, "url_path_for() takes exactly one positional argument"
name = args[0]
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("/")
Expand Down Expand Up @@ -444,7 +450,9 @@ 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, *args: str, **path_params: typing.Any) -> URLPath:
assert len(args) == 1, "url_path_for() takes exactly one positional argument"
name = args[0]
if self.name is not None and name == self.name and "path" in path_params:
# 'name' matches "<mount_name>".
path = path_params.pop("path")
Expand Down Expand Up @@ -584,10 +592,10 @@ 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, *args: 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(*args, **path_params)
except NoMatchFound:
pass
raise NoMatchFound()
Expand Down
4 changes: 2 additions & 2 deletions starlette/templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def _create_env(
self, directory: typing.Union[str, PathLike]
) -> "jinja2.Environment":
@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
def url_for(context: dict, *args: str, **path_params: typing.Any) -> str:
request = context["request"]
return request.url_for(name, **path_params)
return request.url_for(*args, **path_params)

loader = jinja2.FileSystemLoader(directory)
env = jinja2.Environment(loader=loader, autoescape=True)
Expand Down