From 1a6018e08a994c78f5c169b8535408259af0f249 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Sun, 29 Sep 2024 10:28:34 +0200 Subject: [PATCH] Support python-multipart 0.0.12 (#2708) --- pyproject.toml | 1 - starlette/applications.py | 2 +- starlette/formparsers.py | 13 ++++++++----- starlette/requests.py | 4 ++-- starlette/routing.py | 4 ++-- starlette/schemas.py | 2 +- starlette/templating.py | 4 ++-- tests/middleware/test_errors.py | 2 +- tests/test_authentication.py | 10 +++++----- tests/test_concurrency.py | 4 ++-- tests/test_exceptions.py | 2 +- tests/test_responses.py | 2 +- tests/test_routing.py | 10 +++++----- tests/test_websockets.py | 8 ++++---- 14 files changed, 35 insertions(+), 33 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b993c2a2d..a532e4628 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -100,7 +100,6 @@ source_pkgs = ["starlette", "tests"] [tool.coverage.report] exclude_lines = [ "pragma: no cover", - "pragma: nocover", "if typing.TYPE_CHECKING:", "@typing.overload", "raise NotImplementedError", diff --git a/starlette/applications.py b/starlette/applications.py index f34e80ead..0feae72e4 100644 --- a/starlette/applications.py +++ b/starlette/applications.py @@ -113,7 +113,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: await self.middleware_stack(scope, receive, send) def on_event(self, event_type: str) -> typing.Callable: # type: ignore[type-arg] - return self.router.on_event(event_type) # pragma: nocover + return self.router.on_event(event_type) # pragma: no cover def mount(self, path: str, app: ASGIApp, name: str | None = None) -> None: self.router.mount(path, app=app, name=name) # pragma: no cover diff --git a/starlette/formparsers.py b/starlette/formparsers.py index 56f63a8be..48b0f2aad 100644 --- a/starlette/formparsers.py +++ b/starlette/formparsers.py @@ -11,9 +11,12 @@ try: import multipart from multipart.multipart import parse_options_header -except ModuleNotFoundError: # pragma: nocover - parse_options_header = None - multipart = None +except ModuleNotFoundError: # pragma: no cover + parse_options_header = None # type: ignore + multipart = None # type: ignore + +if typing.TYPE_CHECKING: + from multipart.multipart import MultipartCallbacks, QuerystringCallbacks class FormMessage(Enum): @@ -74,7 +77,7 @@ def on_end(self) -> None: async def parse(self) -> FormData: # Callbacks dictionary. - callbacks = { + callbacks: QuerystringCallbacks = { "on_field_start": self.on_field_start, "on_field_name": self.on_field_name, "on_field_data": self.on_field_data, @@ -220,7 +223,7 @@ async def parse(self) -> FormData: raise MultiPartException("Missing boundary in multipart.") # Callbacks dictionary. - callbacks = { + callbacks: MultipartCallbacks = { "on_part_begin": self.on_part_begin, "on_part_data": self.on_part_data, "on_part_end": self.on_part_end, diff --git a/starlette/requests.py b/starlette/requests.py index 26c13d1ca..08dbd84d4 100644 --- a/starlette/requests.py +++ b/starlette/requests.py @@ -14,8 +14,8 @@ try: from multipart.multipart import parse_options_header -except ModuleNotFoundError: # pragma: nocover - parse_options_header = None +except ModuleNotFoundError: # pragma: no cover + parse_options_header = None # type: ignore if typing.TYPE_CHECKING: diff --git a/starlette/routing.py b/starlette/routing.py index cde771563..1504ef50a 100644 --- a/starlette/routing.py +++ b/starlette/routing.py @@ -767,7 +767,7 @@ async def app(self, scope: Scope, receive: Receive, send: Send) -> None: def __eq__(self, other: typing.Any) -> bool: return isinstance(other, Router) and self.routes == other.routes - def mount(self, path: str, app: ASGIApp, name: str | None = None) -> None: # pragma: nocover + def mount(self, path: str, app: ASGIApp, name: str | None = None) -> None: # pragma: no cover route = Mount(path, app=app, name=name) self.routes.append(route) @@ -782,7 +782,7 @@ def add_route( methods: list[str] | None = None, name: str | None = None, include_in_schema: bool = True, - ) -> None: # pragma: nocover + ) -> None: # pragma: no cover route = Route( path, endpoint=endpoint, diff --git a/starlette/schemas.py b/starlette/schemas.py index 94b9cca78..bfc40e2ae 100644 --- a/starlette/schemas.py +++ b/starlette/schemas.py @@ -10,7 +10,7 @@ try: import yaml -except ModuleNotFoundError: # pragma: nocover +except ModuleNotFoundError: # pragma: no cover yaml = None # type: ignore[assignment] diff --git a/starlette/templating.py b/starlette/templating.py index 48e54c0cc..17e7fa4dc 100644 --- a/starlette/templating.py +++ b/starlette/templating.py @@ -19,9 +19,9 @@ # adding a type ignore for mypy to let us access an attribute that may not exist if hasattr(jinja2, "pass_context"): pass_context = jinja2.pass_context - else: # pragma: nocover + else: # pragma: no cover pass_context = jinja2.contextfunction # type: ignore[attr-defined] -except ModuleNotFoundError: # pragma: nocover +except ModuleNotFoundError: # pragma: no cover jinja2 = None # type: ignore[assignment] diff --git a/tests/middleware/test_errors.py b/tests/middleware/test_errors.py index e32f406ae..0b0f7e51d 100644 --- a/tests/middleware/test_errors.py +++ b/tests/middleware/test_errors.py @@ -77,7 +77,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: with pytest.raises(RuntimeError): client = test_client_factory(app) with client.websocket_connect("/"): - pass # pragma: nocover + pass # pragma: no cover def test_background_task(test_client_factory: TestClientFactory) -> None: diff --git a/tests/test_authentication.py b/tests/test_authentication.py index a1bde67b9..3c013a3a7 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -211,7 +211,7 @@ def test_invalid_decorator_usage() -> None: @requires("authenticated") def foo() -> None: - pass # pragma: nocover + pass # pragma: no cover def test_user_interface(test_client_factory: TestClientFactory) -> None: @@ -281,11 +281,11 @@ def test_websocket_authentication_required( with test_client_factory(app) as client: with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/ws"): - pass # pragma: nocover + pass # pragma: no cover with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/ws", headers={"Authorization": "basic foobar"}): - pass # pragma: nocover + pass # pragma: no cover with client.websocket_connect("/ws", auth=("tomchristie", "example")) as websocket: data = websocket.receive_json() @@ -293,11 +293,11 @@ def test_websocket_authentication_required( with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/ws/decorated"): - pass # pragma: nocover + pass # pragma: no cover with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/ws/decorated", headers={"Authorization": "basic foobar"}): - pass # pragma: nocover + pass # pragma: no cover with client.websocket_connect("/ws/decorated", auth=("tomchristie", "example")) as websocket: data = websocket.receive_json() diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index bac6814e4..380eb9ef2 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -22,8 +22,8 @@ async def task1() -> None: async def task2() -> None: 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() diff --git a/tests/test_exceptions.py b/tests/test_exceptions.py index b3dc7843f..eebee3759 100644 --- a/tests/test_exceptions.py +++ b/tests/test_exceptions.py @@ -111,7 +111,7 @@ def test_with_headers(client: TestClient) -> None: def test_websockets_should_raise(client: TestClient) -> None: with pytest.raises(RuntimeError): with client.websocket_connect("/runtime_error"): - pass # pragma: nocover + pass # pragma: no cover def test_handled_exc_after_response( diff --git a/tests/test_responses.py b/tests/test_responses.py index 1fb8c6be0..be0701a5c 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -83,7 +83,7 @@ def test_redirect_response_content_length_header( ) -> None: async def app(scope: Scope, receive: Receive, send: Send) -> None: if scope["path"] == "/": - response = Response("hello", media_type="text/plain") # pragma: nocover + response = Response("hello", media_type="text/plain") # pragma: no cover else: response = RedirectResponse("/") await response(scope, receive, send) diff --git a/tests/test_routing.py b/tests/test_routing.py index 6bb398ba5..fb2bfeec1 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -365,7 +365,7 @@ def test_protocol_switch(test_client_factory: TestClientFactory) -> None: with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/404"): - pass # pragma: nocover + pass # pragma: no cover ok = PlainTextResponse("OK") @@ -598,7 +598,7 @@ def test_standalone_ws_route_does_not_match( client = test_client_factory(app) with pytest.raises(WebSocketDisconnect): with client.websocket_connect("/invalid"): - pass # pragma: nocover + pass # pragma: no cover def test_lifespan_async(test_client_factory: TestClientFactory) -> None: @@ -795,7 +795,7 @@ async def _send(message: Message) -> None: with pytest.raises(RuntimeError): with test_client_factory(app): - pass # pragma: nocover + pass # pragma: no cover assert startup_failed @@ -808,7 +808,7 @@ def run_shutdown() -> None: with pytest.raises(RuntimeError): with test_client_factory(app): - pass # pragma: nocover + pass # pragma: no cover def test_partial_async_endpoint(test_client_factory: TestClientFactory) -> None: @@ -1186,7 +1186,7 @@ def test_decorator_deprecations() -> None: with pytest.deprecated_call(): - async def startup() -> None: ... # pragma: nocover + async def startup() -> None: ... # pragma: no cover router.on_event("startup")(startup) diff --git a/tests/test_websockets.py b/tests/test_websockets.py index 7a9b9272a..8ecf6304c 100644 --- a/tests/test_websockets.py +++ b/tests/test_websockets.py @@ -529,7 +529,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: client = test_client_factory(app) with pytest.raises(RuntimeError): with client.websocket_connect("/"): - pass # pragma: nocover + pass # pragma: no cover def test_receive_text_before_accept(test_client_factory: TestClientFactory) -> None: @@ -540,7 +540,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: client = test_client_factory(app) with pytest.raises(RuntimeError): with client.websocket_connect("/"): - pass # pragma: nocover + pass # pragma: no cover def test_receive_bytes_before_accept(test_client_factory: TestClientFactory) -> None: @@ -551,7 +551,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: client = test_client_factory(app) with pytest.raises(RuntimeError): with client.websocket_connect("/"): - pass # pragma: nocover + pass # pragma: no cover def test_receive_json_before_accept(test_client_factory: TestClientFactory) -> None: @@ -573,7 +573,7 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: client = test_client_factory(app) with pytest.raises(RuntimeError): with client.websocket_connect("/"): - pass # pragma: nocover + pass # pragma: no cover def test_send_wrong_message_type(test_client_factory: TestClientFactory) -> None: