From: Renan Heckert Leal <123672970+lealre@users.noreply.github.com> Date: Wed, 25 Dec 2024 08:45:38 +0000 (+0000) Subject: Remove deprecated `allow_redirects` argument from `TestClient` (#2808) X-Git-Tag: 0.43.0~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c82acf3a03a9b97ffc4332cd3970a1f679383aeb;p=thirdparty%2Fstarlette.git Remove deprecated `allow_redirects` argument from `TestClient` (#2808) --- diff --git a/pyproject.toml b/pyproject.toml index f2764608..18b954c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -87,7 +87,6 @@ filterwarnings = [ "ignore: Async generator 'starlette.requests.Request.stream' was garbage collected before it had been exhausted.*:ResourceWarning", "ignore: path is deprecated.*:DeprecationWarning:certifi", "ignore: Use 'content=<...>' to upload raw bytes/text content.:DeprecationWarning", - "ignore: The `allow_redirects` argument is deprecated. Use `follow_redirects` instead.:DeprecationWarning", "ignore: 'cgi' is deprecated and slated for removal in Python 3.13:DeprecationWarning", "ignore: You seem to already have a custom sys.excepthook handler installed. I'll skip installing Trio's custom handler, but this means MultiErrors will not show full tracebacks.:RuntimeWarning", # TODO: This warning appeared when we bumped anyio to 4.4.0. diff --git a/starlette/testclient.py b/starlette/testclient.py index 645ca109..2c096aa2 100644 --- a/starlette/testclient.py +++ b/starlette/testclient.py @@ -8,7 +8,6 @@ import math import queue import sys import typing -import warnings from concurrent.futures import Future from functools import cached_property from types import GeneratorType @@ -445,22 +444,6 @@ class TestClient(httpx.Client): with anyio.from_thread.start_blocking_portal(**self.async_backend) as portal: yield portal - def _choose_redirect_arg( - self, follow_redirects: bool | None, allow_redirects: bool | None - ) -> bool | httpx._client.UseClientDefault: - redirect: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT - if allow_redirects is not None: - message = "The `allow_redirects` argument is deprecated. Use `follow_redirects` instead." - warnings.warn(message, DeprecationWarning) - redirect = allow_redirects - if follow_redirects is not None: - redirect = follow_redirects - elif allow_redirects is not None and follow_redirects is not None: - raise RuntimeError( # pragma: no cover - "Cannot use both `allow_redirects` and `follow_redirects`." - ) - return redirect - def request( # type: ignore[override] self, method: str, @@ -474,13 +457,11 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: url = self._merge_url(url) - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().request( method, url, @@ -492,7 +473,7 @@ class TestClient(httpx.Client): headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -505,19 +486,17 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().get( url, params=params, headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -530,19 +509,17 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().options( url, params=params, headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -555,19 +532,17 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().head( url, params=params, headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -584,12 +559,10 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().post( url, content=content, @@ -600,7 +573,7 @@ class TestClient(httpx.Client): headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -617,12 +590,10 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().put( url, content=content, @@ -633,7 +604,7 @@ class TestClient(httpx.Client): headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -650,12 +621,10 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().patch( url, content=content, @@ -666,7 +635,7 @@ class TestClient(httpx.Client): headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) @@ -679,19 +648,17 @@ class TestClient(httpx.Client): headers: httpx._types.HeaderTypes | None = None, cookies: httpx._types.CookieTypes | None = None, auth: httpx._types.AuthTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, - follow_redirects: bool | None = None, - allow_redirects: bool | None = None, + follow_redirects: bool | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, timeout: httpx._types.TimeoutTypes | httpx._client.UseClientDefault = httpx._client.USE_CLIENT_DEFAULT, extensions: dict[str, typing.Any] | None = None, ) -> httpx.Response: - redirect = self._choose_redirect_arg(follow_redirects, allow_redirects) return super().delete( url, params=params, headers=headers, cookies=cookies, auth=auth, - follow_redirects=redirect, + follow_redirects=follow_redirects, timeout=timeout, extensions=extensions, ) diff --git a/tests/middleware/test_https_redirect.py b/tests/middleware/test_https_redirect.py index 22dfc14b..66014e7e 100644 --- a/tests/middleware/test_https_redirect.py +++ b/tests/middleware/test_https_redirect.py @@ -21,21 +21,21 @@ def test_https_redirect_middleware(test_client_factory: TestClientFactory) -> No assert response.status_code == 200 client = test_client_factory(app) - response = client.get("/", allow_redirects=False) + response = client.get("/", follow_redirects=False) assert response.status_code == 307 assert response.headers["location"] == "https://testserver/" client = test_client_factory(app, base_url="http://testserver:80") - response = client.get("/", allow_redirects=False) + response = client.get("/", follow_redirects=False) assert response.status_code == 307 assert response.headers["location"] == "https://testserver/" client = test_client_factory(app, base_url="http://testserver:443") - response = client.get("/", allow_redirects=False) + response = client.get("/", follow_redirects=False) assert response.status_code == 307 assert response.headers["location"] == "https://testserver/" client = test_client_factory(app, base_url="http://testserver:123") - response = client.get("/", allow_redirects=False) + response = client.get("/", follow_redirects=False) assert response.status_code == 307 assert response.headers["location"] == "https://testserver:123/" diff --git a/tests/test_responses.py b/tests/test_responses.py index 3c2d346d..9f801419 100644 --- a/tests/test_responses.py +++ b/tests/test_responses.py @@ -89,7 +89,7 @@ def test_redirect_response_content_length_header( await response(scope, receive, send) client: TestClient = test_client_factory(app) - response = client.request("GET", "/redirect", allow_redirects=False) + response = client.request("GET", "/redirect", follow_redirects=False) assert response.url == "http://testserver/redirect" assert response.headers["content-length"] == "0"