From 77117ab9f123e7b68eb7fbbeb052c43aa0199046 Mon Sep 17 00:00:00 2001 From: Irfanuddin Shafi Ahmed Date: Sat, 4 Jan 2025 15:20:34 +0400 Subject: [PATCH 1/4] fix: add depcrecated warnings to testclient on use of timeout arg --- starlette/testclient.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/starlette/testclient.py b/starlette/testclient.py index fb2ad6e35..767df6c91 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -7,6 +7,7 @@ import math import sys import typing +import warnings from concurrent.futures import Future from types import GeneratorType from urllib.parse import unquote, urljoin @@ -455,6 +456,8 @@ def get( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().get( url, params=params, @@ -478,6 +481,8 @@ def options( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().options( url, params=params, @@ -501,6 +506,8 @@ def head( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().head( url, params=params, @@ -528,6 +535,8 @@ def post( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().post( url, content=content, @@ -559,6 +568,8 @@ def put( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().put( url, content=content, @@ -590,6 +601,8 @@ def patch( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().patch( url, content=content, @@ -617,6 +630,8 @@ def delete( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().delete( url, params=params, From 8d211e74704376a4785b710683dd623d68cead6b Mon Sep 17 00:00:00 2001 From: Irfanuddin Shafi Ahmed Date: Sat, 4 Jan 2025 15:34:43 +0400 Subject: [PATCH 2/4] fix: change to request function --- starlette/testclient.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/starlette/testclient.py b/starlette/testclient.py index 767df6c91..5b0b855c7 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -427,6 +427,8 @@ def request( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: + if timeout is not httpx.USE_CLIENT_DEFAULT: + warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) url = self._merge_url(url) return super().request( method, @@ -456,8 +458,6 @@ def get( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().get( url, params=params, @@ -481,8 +481,6 @@ def options( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().options( url, params=params, @@ -506,8 +504,6 @@ def head( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().head( url, params=params, @@ -535,8 +531,6 @@ def post( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().post( url, content=content, @@ -568,8 +562,6 @@ def put( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().put( url, content=content, @@ -601,8 +593,6 @@ def patch( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().patch( url, content=content, @@ -630,8 +620,6 @@ def delete( # type: ignore[override] timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - if timeout is not httpx.USE_CLIENT_DEFAULT: - warnings.warn("The 'timeout' argument is not supported by 'TestClient'.", DeprecationWarning) return super().delete( url, params=params, From 4e439578157ebe03fb58a0c79f9e0c75c695104d Mon Sep 17 00:00:00 2001 From: Irfanuddin Shafi Ahmed Date: Sat, 4 Jan 2025 15:34:52 +0400 Subject: [PATCH 3/4] test: add test --- tests/test_testclient.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/test_testclient.py b/tests/test_testclient.py index 478dbca46..a68e07927 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -422,3 +422,9 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: with client.websocket_connect("/hello-world", params={"foo": "bar"}) as websocket: data = websocket.receive_bytes() assert data == b"/hello-world" + + +def test_timeout_deprecation() -> None: + with pytest.deprecated_call(): + client = TestClient(mock_service) + client.request("GET", "/", timeout=1) From 7fbe6885f4ec61380bcf9be41c3fd3fe4d22c267 Mon Sep 17 00:00:00 2001 From: Irfanuddin Shafi Ahmed Date: Sat, 4 Jan 2025 16:01:13 +0400 Subject: [PATCH 4/4] test: improve test --- tests/test_testclient.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_testclient.py b/tests/test_testclient.py index a68e07927..5d8a4fbf2 100644 --- a/tests/test_testclient.py +++ b/tests/test_testclient.py @@ -424,7 +424,10 @@ async def app(scope: Scope, receive: Receive, send: Send) -> None: assert data == b"/hello-world" -def test_timeout_deprecation() -> None: +@pytest.mark.parametrize("method", ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]) +def test_timeout_deprecation(method: str) -> None: with pytest.deprecated_call(): client = TestClient(mock_service) - client.request("GET", "/", timeout=1) + client.request(method, "/", timeout=1) + method_call = getattr(client, method.lower()) + method_call("/", timeout=1)