diff --git a/setup.cfg b/setup.cfg index b3f84dca1..20c2588a1 100644 --- 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: diff --git a/starlette/applications.py b/starlette/applications.py index ea52ee70e..c95fa0a5c 100644 --- a/starlette/applications.py +++ b/starlette/applications.py @@ -104,7 +104,7 @@ 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: 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: diff --git a/starlette/requests.py b/starlette/requests.py index 676f4e9aa..cf129702e 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -15,6 +15,10 @@ 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 @@ def state(self) -> State: 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) diff --git a/starlette/routing.py b/starlette/routing.py index f9474b4a1..3c11c1b0c 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -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 @@ 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: 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 @@ 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: 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 @@ 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: 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 "". path_params["path"] = path_params["path"].lstrip("/") @@ -441,7 +441,7 @@ 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: 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 "". path = path_params.pop("path") @@ -581,7 +581,7 @@ 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: 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)