From 8b61de3f72a9898db69cddd2d6011987fda1d754 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Wed, 24 Nov 2021 22:48:36 +0100 Subject: [PATCH 1/5] fix-request-url-for-type-hint --- starlette/applications.py | 2 +- starlette/requests.py | 6 +++++- starlette/routing.py | 12 ++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) 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..baf0a2773 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -15,6 +15,10 @@ parse_options_header = None +if typing.TYPE_CHECKING: # pragma: nocover + 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 9a1a5e12d..c5c97bf9c 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) From fb3fc639e0e7a1e7eac0e035d8bc6bc4835050c9 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 28 Nov 2021 10:50:08 +0100 Subject: [PATCH 2/5] add no cover to setup.cfg --- setup.cfg | 4 ++++ starlette/requests.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/setup.cfg b/setup.cfg index b3f84dca1..17d709fec 100644 --- a/setup.cfg +++ b/setup.cfg @@ -32,3 +32,7 @@ filterwarnings= [coverage:run] source_pkgs = starlette, tests + +[coverage:report] +exclude_lines = + if typing.TYPE_CHECKING: diff --git a/starlette/requests.py b/starlette/requests.py index baf0a2773..cf129702e 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -15,7 +15,7 @@ parse_options_header = None -if typing.TYPE_CHECKING: # pragma: nocover +if typing.TYPE_CHECKING: from starlette.routing import Router From db46dc8a15b3b87dd47a06d9f0425626c9bed74d Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 28 Nov 2021 10:57:00 +0100 Subject: [PATCH 3/5] fix setup.cfg --- setup.cfg | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.cfg b/setup.cfg index 17d709fec..6b094e754 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,3 +36,4 @@ source_pkgs = starlette, tests [coverage:report] exclude_lines = if typing.TYPE_CHECKING: + ... From 86c2a218dbe38f2c0964570476c6114464b85ff1 Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 28 Nov 2021 11:19:04 +0100 Subject: [PATCH 4/5] fix coverage --- setup.cfg | 2 +- starlette/formparsers.py | 2 +- starlette/requests.py | 2 +- starlette/schemas.py | 2 +- starlette/templating.py | 4 ++-- tests/test_authentication.py | 2 +- tests/test_concurrency.py | 4 ++-- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/setup.cfg b/setup.cfg index 6b094e754..763d1d445 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,5 +35,5 @@ source_pkgs = starlette, tests [coverage:report] exclude_lines = + pragma: no cover if typing.TYPE_CHECKING: - ... diff --git a/starlette/formparsers.py b/starlette/formparsers.py index 1614a9d69..e1cb65911 100644 --- a/starlette/formparsers.py +++ b/starlette/formparsers.py @@ -7,7 +7,7 @@ try: import multipart from multipart.multipart import parse_options_header -except ImportError: # pragma: nocover +except ImportError: # pragma: no cover parse_options_header = None multipart = None diff --git a/starlette/requests.py b/starlette/requests.py index cf129702e..98a339aa2 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -11,7 +11,7 @@ try: from multipart.multipart import parse_options_header -except ImportError: # pragma: nocover +except ImportError: # pragma: no cover parse_options_header = None diff --git a/starlette/schemas.py b/starlette/schemas.py index 6ca764fdc..432300779 100644 --- a/starlette/schemas.py +++ b/starlette/schemas.py @@ -7,7 +7,7 @@ try: import yaml -except ImportError: # pragma: nocover +except ImportError: # pragma: no cover yaml = None # type: ignore diff --git a/starlette/templating.py b/starlette/templating.py index 18d5eb40c..1bc25510b 100644 --- a/starlette/templating.py +++ b/starlette/templating.py @@ -11,9 +11,9 @@ # @contextfunction renamed to @pass_context in Jinja 3.0, to be removed in 3.1 if hasattr(jinja2, "pass_context"): pass_context = jinja2.pass_context - else: # pragma: nocover + else: # pragma: no cover pass_context = jinja2.contextfunction -except ImportError: # pragma: nocover +except ImportError: # pragma: no cover jinja2 = None # type: ignore diff --git a/tests/test_authentication.py b/tests/test_authentication.py index 43c7ab96d..a77701ea7 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -191,7 +191,7 @@ def test_invalid_decorator_usage(): @requires("authenticated") def foo(): - pass # pragma: nocover + pass # pragma: no cover def test_user_interface(test_client_factory): diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index cc5eba974..a20696ab6 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -14,8 +14,8 @@ async def task1(): async def task2(): await task1_finished.wait() - await anyio.sleep(0) # pragma: nocover - task2_finished.set() # pragma: nocover + await anyio.sleep(0) # pragma: no cover + task2_finished.set() # pragma: no cover await run_until_first_complete((task1, {}), (task2, {})) assert task1_finished.is_set() From 4e5fb7b63f650884471c97ef9a1589777042dc1b Mon Sep 17 00:00:00 2001 From: Amin Alaee Date: Sun, 28 Nov 2021 11:26:15 +0100 Subject: [PATCH 5/5] revert --- setup.cfg | 1 + starlette/formparsers.py | 2 +- starlette/requests.py | 2 +- starlette/schemas.py | 2 +- starlette/templating.py | 4 ++-- tests/test_authentication.py | 2 +- tests/test_concurrency.py | 4 ++-- 7 files changed, 9 insertions(+), 8 deletions(-) diff --git a/setup.cfg b/setup.cfg index 763d1d445..20c2588a1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -36,4 +36,5 @@ source_pkgs = starlette, tests [coverage:report] exclude_lines = pragma: no cover + pragma: nocover if typing.TYPE_CHECKING: diff --git a/starlette/formparsers.py b/starlette/formparsers.py index e1cb65911..1614a9d69 100644 --- a/starlette/formparsers.py +++ b/starlette/formparsers.py @@ -7,7 +7,7 @@ try: import multipart from multipart.multipart import parse_options_header -except ImportError: # pragma: no cover +except ImportError: # pragma: nocover parse_options_header = None multipart = None diff --git a/starlette/requests.py b/starlette/requests.py index 98a339aa2..cf129702e 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -11,7 +11,7 @@ try: from multipart.multipart import parse_options_header -except ImportError: # pragma: no cover +except ImportError: # pragma: nocover parse_options_header = None diff --git a/starlette/schemas.py b/starlette/schemas.py index 432300779..6ca764fdc 100644 --- a/starlette/schemas.py +++ b/starlette/schemas.py @@ -7,7 +7,7 @@ try: import yaml -except ImportError: # pragma: no cover +except ImportError: # pragma: nocover yaml = None # type: ignore diff --git a/starlette/templating.py b/starlette/templating.py index 1bc25510b..18d5eb40c 100644 --- a/starlette/templating.py +++ b/starlette/templating.py @@ -11,9 +11,9 @@ # @contextfunction renamed to @pass_context in Jinja 3.0, to be removed in 3.1 if hasattr(jinja2, "pass_context"): pass_context = jinja2.pass_context - else: # pragma: no cover + else: # pragma: nocover pass_context = jinja2.contextfunction -except ImportError: # pragma: no cover +except ImportError: # pragma: nocover jinja2 = None # type: ignore diff --git a/tests/test_authentication.py b/tests/test_authentication.py index a77701ea7..43c7ab96d 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -191,7 +191,7 @@ def test_invalid_decorator_usage(): @requires("authenticated") def foo(): - pass # pragma: no cover + pass # pragma: nocover def test_user_interface(test_client_factory): diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index a20696ab6..cc5eba974 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -14,8 +14,8 @@ async def task1(): async def task2(): await task1_finished.wait() - await anyio.sleep(0) # pragma: no cover - task2_finished.set() # pragma: no cover + await anyio.sleep(0) # pragma: nocover + task2_finished.set() # pragma: nocover await run_until_first_complete((task1, {}), (task2, {})) assert task1_finished.is_set()