]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Fix url_path_for typehints (#1341)
authorAmin Alaee <mohammadamin.alaee@gmail.com>
Tue, 30 Nov 2021 18:13:08 +0000 (19:13 +0100)
committerGitHub <noreply@github.com>
Tue, 30 Nov 2021 18:13:08 +0000 (19:13 +0100)
setup.cfg
starlette/applications.py
starlette/requests.py
starlette/routing.py

index b3f84dca1e1a8838cd0d11a0741adfffa0d24b3c..20c2588a17600f3538393a2ed4e03aa969021a0b 100644 (file)
--- a/setup.cfg
+++ b/setup.cfg
@@ -32,3 +32,9 @@ filterwarnings=
 
 [coverage:run]
 source_pkgs = starlette, tests
+
+[coverage:report]
+exclude_lines =
+    pragma: no cover
+    pragma: nocover
+    if typing.TYPE_CHECKING:
index ea52ee70eea462a27df46eb9f15652a4037a919e..c95fa0a5cc5f6721e7a36254cd45aa49af5cbc2a 100644 (file)
@@ -104,7 +104,7 @@ class Starlette:
         self._debug = value
         self.middleware_stack = self.build_middleware_stack()
 
-    def url_path_for(self, name: str, **path_params: str) -> URLPath:
+    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:
index 676f4e9aa3fec50df942c46053e991b78074cf26..cf129702e06295c39add77db49a4ce6b9218b94b 100644 (file)
@@ -15,6 +15,10 @@ except ImportError:  # pragma: nocover
     parse_options_header = None
 
 
+if typing.TYPE_CHECKING:
+    from starlette.routing import Router
+
+
 SERVER_PUSH_HEADERS_TO_COPY = {
     "accept",
     "accept-encoding",
@@ -166,7 +170,7 @@ class HTTPConnection(Mapping):
         return self._state
 
     def url_for(self, name: str, **path_params: typing.Any) -> str:
-        router = self.scope["router"]
+        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)
 
index f9474b4a13c00e95728658c540d28ea94374e076..3c11c1b0cc0152f2955680f2461fe331c0932119 100644 (file)
@@ -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: str) -> 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:
@@ -235,7 +235,7 @@ class Route(BaseRoute):
                     return Match.FULL, child_scope
         return Match.NONE, {}
 
-    def url_path_for(self, name: str, **path_params: str) -> 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())
 
@@ -298,7 +298,7 @@ class WebSocketRoute(BaseRoute):
                 return Match.FULL, child_scope
         return Match.NONE, {}
 
-    def url_path_for(self, name: str, **path_params: str) -> 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())
 
@@ -371,7 +371,7 @@ class Mount(BaseRoute):
                 return Match.FULL, child_scope
         return Match.NONE, {}
 
-    def url_path_for(self, name: str, **path_params: str) -> URLPath:
+    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("/")
@@ -441,7 +441,7 @@ class Host(BaseRoute):
                 return Match.FULL, child_scope
         return Match.NONE, {}
 
-    def url_path_for(self, name: str, **path_params: str) -> URLPath:
+    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")
@@ -581,7 +581,7 @@ class Router:
             response = PlainTextResponse("Not Found", status_code=404)
         await response(scope, receive, send)
 
-    def url_path_for(self, name: str, **path_params: str) -> 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)