Repository: encode/httpx
Pull request #1795 (2021-08-11 15:24:52)

Drop mode argument, 'httpx.Proxy(..., mode=...)'
Discussed in https://github.com/encode/httpx/discussions/1783


tomchristie: Let's crack on with this.

Patch:
diff --git a/httpx/_config.py b/httpx/_config.py
index 9d29f9f..927a67c 100644
--- a/httpx/_config.py
+++ b/httpx/_config.py
@@ -316,16 +316,12 @@ class Limits:
 
 
 class Proxy:
-    def __init__(
-        self, url: URLTypes, *, headers: HeaderTypes = None, mode: str = "DEFAULT"
-    ):
+    def __init__(self, url: URLTypes, *, headers: HeaderTypes = None):
         url = URL(url)
         headers = Headers(headers)
 
         if url.scheme not in ("http", "https"):
             raise ValueError(f"Unknown scheme for proxy URL {url!r}")
-        if mode not in ("DEFAULT", "FORWARD_ONLY", "TUNNEL_ONLY"):
-            raise ValueError(f"Unknown proxy mode {mode!r}")
 
         if url.username or url.password:
             headers.setdefault(
@@ -338,7 +334,6 @@ class Proxy:
 
         self.url = url
         self.headers = headers
-        self.mode = mode
 
     def _build_auth_header(self, username: str, password: str) -> str:
         userpass = (username.encode("utf-8"), password.encode("utf-8"))
@@ -346,11 +341,7 @@ class Proxy:
         return f"Basic {token}"
 
     def __repr__(self) -> str:
-        return (
-            f"Proxy(url={str(self.url)!r}, "
-            f"headers={dict(self.headers)!r}, "
-            f"mode={self.mode!r})"
-        )
+        return f"Proxy(url={str(self.url)!r}, headers={dict(self.headers)!r})"
 
 
 DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)
diff --git a/httpx/_transports/default.py b/httpx/_transports/default.py
index ae6c2d1..73401fc 100644
--- a/httpx/_transports/default.py
+++ b/httpx/_transports/default.py
@@ -145,7 +145,6 @@ class HTTPTransport(BaseTransport):
             self._pool = httpcore.SyncHTTPProxy(
                 proxy_url=proxy.url.raw,
                 proxy_headers=proxy.headers.raw,
-                proxy_mode=proxy.mode,
                 ssl_context=ssl_context,
                 max_connections=limits.max_connections,
                 max_keepalive_connections=limits.max_keepalive_connections,
@@ -242,7 +241,6 @@ class AsyncHTTPTransport(AsyncBaseTransport):
             self._pool = httpcore.AsyncHTTPProxy(
                 proxy_url=proxy.url.raw,
                 proxy_headers=proxy.headers.raw,
-                proxy_mode=proxy.mode,
                 ssl_context=ssl_context,
                 max_connections=limits.max_connections,
                 max_keepalive_connections=limits.max_keepalive_connections,


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
index f218f8f..3a17cf5 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -200,33 +200,26 @@ def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch):  # pragma: noc
 
 
 @pytest.mark.parametrize(
-    "url,expected_url,expected_headers,expected_mode",
+    "url,expected_url,expected_headers",
     [
-        ("https://example.com", "https://example.com", {}, "DEFAULT"),
+        ("https://example.com", "https://example.com", {}),
         (
             "https://user:pass@example.com",
             "https://example.com",
             {"proxy-authorization": "Basic dXNlcjpwYXNz"},
-            "DEFAULT",
         ),
     ],
 )
-def test_proxy_from_url(url, expected_url, expected_headers, expected_mode):
+def test_proxy_from_url(url, expected_url, expected_headers):
     proxy = httpx.Proxy(url)
 
     assert str(proxy.url) == expected_url
     assert dict(proxy.headers) == expected_headers
-    assert proxy.mode == expected_mode
-    assert repr(proxy) == "Proxy(url='{}', headers={}, mode='{}')".format(
-        expected_url, str(expected_headers), expected_mode
+    assert repr(proxy) == "Proxy(url='{}', headers={})".format(
+        expected_url, str(expected_headers)
     )
 
 
 def test_invalid_proxy_scheme():
     with pytest.raises(ValueError):
         httpx.Proxy("invalid://example.com")
-
-
-def test_invalid_proxy_mode():
-    with pytest.raises(ValueError):
-        httpx.Proxy("https://example.com", mode="INVALID")