]> git.ipfire.org Git - thirdparty/starlette.git/commitdiff
Add deprecated warnings to `TestClient` on use of `timeout` argument (#2840)
authorIrfanuddin Shafi Ahmed <irfanudeen08@gmail.com>
Sat, 22 Feb 2025 11:45:06 +0000 (15:45 +0400)
committerGitHub <noreply@github.com>
Sat, 22 Feb 2025 11:45:06 +0000 (11:45 +0000)
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
starlette/testclient.py
tests/test_testclient.py

index fb2ad6e3555b62433c27c48fd9760dabdad52801..d54025e52d7ec2996d3a6fbdea9c390291067063 100644 (file)
@@ -7,6 +7,7 @@ import json
 import math
 import sys
 import typing
+import warnings
 from concurrent.futures import Future
 from types import GeneratorType
 from urllib.parse import unquote, urljoin
@@ -426,6 +427,12 @@ class TestClient(httpx.Client):
         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(
+                "You should not use the 'timeout' argument with the TestClient. "
+                "See https://github.com/encode/starlette/issues/1108 for more information.",
+                DeprecationWarning,
+            )
         url = self._merge_url(url)
         return super().request(
             method,
index 478dbca4692cfb97f7757d746bd81ed0581a747c..c6a3edafc14e33da5203808a18ca4519da06ceb0 100644 (file)
@@ -422,3 +422,9 @@ def test_websocket_raw_path_without_params(test_client_factory: TestClientFactor
     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(match="You should not use the 'timeout' argument with the TestClient."):
+        client = TestClient(mock_service)
+        client.get("/", timeout=1)