]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Rename PoolLimits to Limits (#1113)
authorFlorimond Manca <florimond.manca@gmail.com>
Sat, 1 Aug 2020 19:44:58 +0000 (21:44 +0200)
committerGitHub <noreply@github.com>
Sat, 1 Aug 2020 19:44:58 +0000 (20:44 +0100)
* Rename PoolLimits to Limits

* Lint

Co-authored-by: Tom Christie <tom@tomchristie.com>
docs/advanced.md
httpx/__init__.py
httpx/_client.py
httpx/_config.py
tests/client/test_client.py
tests/test_config.py
tests/test_timeouts.py

index 4fdafc1c1a10bae252b8a8683d60ca7880c1f803..4eac982afe28e9d4e0421dbd67866b1fe45b25eb 100644 (file)
@@ -401,8 +401,8 @@ response = client.get('http://example.com/')
 
 ## Pool limit configuration
 
-You can control the connection pool size using the `pool_limits` keyword
-argument on the client. It takes instances of `httpx.PoolLimits` which define:
+You can control the connection pool size using the `limits` keyword
+argument on the client. It takes instances of `httpx.Limits` which define:
 
 - `max_keepalive`, number of allowable keep-alive connections, or `None` to always
 allow. (Defaults 10)
@@ -411,8 +411,8 @@ allow. (Defaults 10)
 
 
 ```python
-limits = httpx.PoolLimits(max_keepalive=5, max_connections=10)
-client = httpx.Client(pool_limits=limits)
+limits = httpx.Limits(max_keepalive=5, max_connections=10)
+client = httpx.Client(limits=limits)
 ```
 
 ## Multipart file encoding
index 15f2223557924f0aadaaa80f040c4c3434778e20..25c70e4bad6c09425f868bd585e94b6057fcc2c9 100644 (file)
@@ -2,7 +2,7 @@ from .__version__ import __description__, __title__, __version__
 from ._api import delete, get, head, options, patch, post, put, request, stream
 from ._auth import Auth, BasicAuth, DigestAuth
 from ._client import AsyncClient, Client
-from ._config import PoolLimits, Proxy, Timeout, create_ssl_context
+from ._config import Limits, PoolLimits, Proxy, Timeout, create_ssl_context
 from ._exceptions import (
     CloseError,
     ConnectError,
@@ -59,6 +59,7 @@ __all__ = [
     "BasicAuth",
     "Client",
     "DigestAuth",
+    "Limits",
     "PoolLimits",
     "Proxy",
     "Timeout",
index 5a32f84e86882ce64954a006613706a7843d7c0b..bfc4e623b298b1f4129187fb11bd757362ac2d25 100644 (file)
@@ -7,11 +7,11 @@ import httpcore
 
 from ._auth import Auth, BasicAuth, FunctionAuth
 from ._config import (
+    DEFAULT_LIMITS,
     DEFAULT_MAX_REDIRECTS,
-    DEFAULT_POOL_LIMITS,
     DEFAULT_TIMEOUT_CONFIG,
     UNSET,
-    PoolLimits,
+    Limits,
     Proxy,
     Timeout,
     UnsetType,
@@ -50,6 +50,7 @@ from ._utils import (
     get_logger,
     same_origin,
     should_not_be_proxied,
+    warn_deprecated,
 )
 
 logger = get_logger(__name__)
@@ -422,8 +423,7 @@ class Client(BaseClient):
     URLs.
     * **timeout** - *(optional)* The timeout configuration to use when sending
     requests.
-    * **pool_limits** - *(optional)* The connection pool configuration to use
-    when determining the maximum number of concurrently open HTTP connections.
+    * **limits** - *(optional)* The limits configuration to use.
     * **max_redirects** - *(optional)* The maximum number of redirect responses
     that should be followed.
     * **base_url** - *(optional)* A URL to use as the base when building
@@ -448,7 +448,8 @@ class Client(BaseClient):
         http2: bool = False,
         proxies: ProxiesTypes = None,
         timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        limits: Limits = DEFAULT_LIMITS,
+        pool_limits: Limits = None,
         max_redirects: int = DEFAULT_MAX_REDIRECTS,
         base_url: URLTypes = None,
         transport: httpcore.SyncHTTPTransport = None,
@@ -466,13 +467,20 @@ class Client(BaseClient):
             trust_env=trust_env,
         )
 
+        if pool_limits is not None:
+            warn_deprecated(
+                "Client(..., pool_limits=...) is deprecated and will raise "
+                "errors in the future. Use Client(..., limits=...) instead."
+            )
+            limits = pool_limits
+
         proxy_map = self._get_proxy_map(proxies, trust_env)
 
         self._transport = self._init_transport(
             verify=verify,
             cert=cert,
             http2=http2,
-            pool_limits=pool_limits,
+            limits=limits,
             transport=transport,
             app=app,
             trust_env=trust_env,
@@ -487,7 +495,7 @@ class Client(BaseClient):
                 verify=verify,
                 cert=cert,
                 http2=http2,
-                pool_limits=pool_limits,
+                limits=limits,
                 trust_env=trust_env,
             )
             for key, proxy in proxy_map.items()
@@ -499,7 +507,7 @@ class Client(BaseClient):
         verify: VerifyTypes = True,
         cert: CertTypes = None,
         http2: bool = False,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        limits: Limits = DEFAULT_LIMITS,
         transport: httpcore.SyncHTTPTransport = None,
         app: typing.Callable = None,
         trust_env: bool = True,
@@ -514,8 +522,8 @@ class Client(BaseClient):
 
         return httpcore.SyncConnectionPool(
             ssl_context=ssl_context,
-            max_keepalive=pool_limits.max_keepalive,
-            max_connections=pool_limits.max_connections,
+            max_keepalive=limits.max_keepalive,
+            max_connections=limits.max_connections,
             keepalive_expiry=KEEPALIVE_EXPIRY,
             http2=http2,
         )
@@ -526,7 +534,7 @@ class Client(BaseClient):
         verify: VerifyTypes = True,
         cert: CertTypes = None,
         http2: bool = False,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        limits: Limits = DEFAULT_LIMITS,
         trust_env: bool = True,
     ) -> httpcore.SyncHTTPTransport:
         ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
@@ -536,8 +544,8 @@ class Client(BaseClient):
             proxy_headers=proxy.headers.raw,
             proxy_mode=proxy.mode,
             ssl_context=ssl_context,
-            max_keepalive=pool_limits.max_keepalive,
-            max_connections=pool_limits.max_connections,
+            max_keepalive=limits.max_keepalive,
+            max_connections=limits.max_connections,
             keepalive_expiry=KEEPALIVE_EXPIRY,
             http2=http2,
         )
@@ -946,8 +954,7 @@ class AsyncClient(BaseClient):
     URLs.
     * **timeout** - *(optional)* The timeout configuration to use when sending
     requests.
-    * **pool_limits** - *(optional)* The connection pool configuration to use
-    when determining the maximum number of concurrently open HTTP connections.
+    * **limits** - *(optional)* The limits configuration to use.
     * **max_redirects** - *(optional)* The maximum number of redirect responses
     that should be followed.
     * **base_url** - *(optional)* A URL to use as the base when building
@@ -972,7 +979,8 @@ class AsyncClient(BaseClient):
         http2: bool = False,
         proxies: ProxiesTypes = None,
         timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        limits: Limits = DEFAULT_LIMITS,
+        pool_limits: Limits = None,
         max_redirects: int = DEFAULT_MAX_REDIRECTS,
         base_url: URLTypes = None,
         transport: httpcore.AsyncHTTPTransport = None,
@@ -990,13 +998,20 @@ class AsyncClient(BaseClient):
             trust_env=trust_env,
         )
 
+        if pool_limits is not None:
+            warn_deprecated(
+                "AsyncClient(..., pool_limits=...) is deprecated and will raise "
+                "errors in the future. Use AsyncClient(..., limits=...) instead."
+            )
+            limits = pool_limits
+
         proxy_map = self._get_proxy_map(proxies, trust_env)
 
         self._transport = self._init_transport(
             verify=verify,
             cert=cert,
             http2=http2,
-            pool_limits=pool_limits,
+            limits=limits,
             transport=transport,
             app=app,
             trust_env=trust_env,
@@ -1011,7 +1026,7 @@ class AsyncClient(BaseClient):
                 verify=verify,
                 cert=cert,
                 http2=http2,
-                pool_limits=pool_limits,
+                limits=limits,
                 trust_env=trust_env,
             )
             for key, proxy in proxy_map.items()
@@ -1023,7 +1038,7 @@ class AsyncClient(BaseClient):
         verify: VerifyTypes = True,
         cert: CertTypes = None,
         http2: bool = False,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        limits: Limits = DEFAULT_LIMITS,
         transport: httpcore.AsyncHTTPTransport = None,
         app: typing.Callable = None,
         trust_env: bool = True,
@@ -1038,8 +1053,8 @@ class AsyncClient(BaseClient):
 
         return httpcore.AsyncConnectionPool(
             ssl_context=ssl_context,
-            max_keepalive=pool_limits.max_keepalive,
-            max_connections=pool_limits.max_connections,
+            max_keepalive=limits.max_keepalive,
+            max_connections=limits.max_connections,
             keepalive_expiry=KEEPALIVE_EXPIRY,
             http2=http2,
         )
@@ -1050,7 +1065,7 @@ class AsyncClient(BaseClient):
         verify: VerifyTypes = True,
         cert: CertTypes = None,
         http2: bool = False,
-        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,
+        limits: Limits = DEFAULT_LIMITS,
         trust_env: bool = True,
     ) -> httpcore.AsyncHTTPTransport:
         ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
@@ -1060,8 +1075,8 @@ class AsyncClient(BaseClient):
             proxy_headers=proxy.headers.raw,
             proxy_mode=proxy.mode,
             ssl_context=ssl_context,
-            max_keepalive=pool_limits.max_keepalive,
-            max_connections=pool_limits.max_connections,
+            max_keepalive=limits.max_keepalive,
+            max_connections=limits.max_connections,
             keepalive_expiry=KEEPALIVE_EXPIRY,
             http2=http2,
         )
index 4bd633aa8c410c5d87f48c0839dc508c5f83f710..c140e12d3662b8cd1f60e4c0bac1e0e560304e81 100644 (file)
@@ -317,9 +317,9 @@ class Timeout:
         )
 
 
-class PoolLimits:
+class Limits:
     """
-    Limits on the number of connections in a connection pool.
+    Configuration for limits to various client behaviors.
 
     **Parameters:**
 
@@ -350,6 +350,17 @@ class PoolLimits:
         )
 
 
+class PoolLimits(Limits):
+    def __init__(
+        self, *, max_keepalive: int = None, max_connections: int = None,
+    ) -> None:
+        warn_deprecated(
+            "httpx.PoolLimits(...) is deprecated and will raise errors in the future. "
+            "Use httpx.Limits(...) instead."
+        )
+        super().__init__(max_keepalive=max_keepalive, max_connections=max_connections)
+
+
 class Proxy:
     def __init__(
         self, url: URLTypes, *, headers: HeaderTypes = None, mode: str = "DEFAULT",
@@ -389,5 +400,5 @@ class Proxy:
 
 
 DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)
-DEFAULT_POOL_LIMITS = PoolLimits(max_keepalive=10, max_connections=100)
+DEFAULT_LIMITS = Limits(max_keepalive=10, max_connections=100)
 DEFAULT_MAX_REDIRECTS = 20
index 073cebf98d2321f41a31a58dd793663bd4c42027..4f196be9549c2a215be7189983ec3ce5e3b42dae 100644 (file)
@@ -194,3 +194,13 @@ def test_merge_url_hsts(url: str, scheme: str, is_ssl: bool):
     request = client.build_request("GET", url)
     assert request.url.scheme == scheme
     assert request.url.is_ssl == is_ssl
+
+
+def test_pool_limits_deprecated():
+    limits = httpx.Limits()
+
+    with pytest.warns(DeprecationWarning):
+        httpx.Client(pool_limits=limits)
+
+    with pytest.warns(DeprecationWarning):
+        httpx.AsyncClient(pool_limits=limits)
index ee3932c769545ab26824210f1498a70827110ed3..de9f4bc85b2e3dd93ca4324b28cbc9c337ca0c45 100644 (file)
@@ -101,13 +101,18 @@ def test_create_ssl_context_with_get_request(server, cert_pem_file):
 
 
 def test_limits_repr():
-    limits = httpx.PoolLimits(max_connections=100)
-    assert repr(limits) == "PoolLimits(max_keepalive=None, max_connections=100)"
+    limits = httpx.Limits(max_connections=100)
+    assert repr(limits) == "Limits(max_keepalive=None, max_connections=100)"
 
 
 def test_limits_eq():
-    limits = httpx.PoolLimits(max_connections=100)
-    assert limits == httpx.PoolLimits(max_connections=100)
+    limits = httpx.Limits(max_connections=100)
+    assert limits == httpx.Limits(max_connections=100)
+
+
+def test_pool_limits_deprecated():
+    with pytest.warns(DeprecationWarning):
+        httpx.PoolLimits()
 
 
 def test_timeout_eq():
index 1c61ba5ee4fa0138d8e41364e23f562ded0636c3..3e261389853a4c624e72e6e557c3dee95f36b9e0 100644 (file)
@@ -34,10 +34,10 @@ async def test_connect_timeout(server):
 
 @pytest.mark.usefixtures("async_environment")
 async def test_pool_timeout(server):
-    pool_limits = httpx.PoolLimits(max_connections=1)
+    limits = httpx.Limits(max_connections=1)
     timeout = httpx.Timeout(None, pool=1e-4)
 
-    async with httpx.AsyncClient(pool_limits=pool_limits, timeout=timeout) as client:
+    async with httpx.AsyncClient(limits=limits, timeout=timeout) as client:
         async with client.stream("GET", server.url):
             with pytest.raises(httpx.PoolTimeout):
                 await client.get("http://localhost:8000/")