]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Dont perform implicit close/warning on `__del__` (#2026)
authorTom Christie <tom@tomchristie.com>
Thu, 13 Jan 2022 10:37:37 +0000 (10:37 +0000)
committerGitHub <noreply@github.com>
Thu, 13 Jan 2022 10:37:37 +0000 (10:37 +0000)
* Version 0.21.3

* Don't perform implict close on __del__

httpx/_client.py
tests/client/test_async_client.py

index 56215203771a561d399a5f2e8ed51a4498a5eea7..2b513b0d357f998022745623192ec6f7384ca9f2 100644 (file)
@@ -1273,13 +1273,6 @@ class Client(BaseClient):
             if transport is not None:
                 transport.__exit__(exc_type, exc_value, traceback)
 
-    def __del__(self) -> None:
-        # We use 'getattr' here, to manage the case where '__del__()' is called
-        # on a partially initiallized instance that raised an exception during
-        # the call to '__init__()'.
-        if getattr(self, "_state", None) == ClientState.OPENED:  # noqa: B009
-            self.close()
-
 
 class AsyncClient(BaseClient):
     """
@@ -1983,34 +1976,3 @@ class AsyncClient(BaseClient):
         for proxy in self._mounts.values():
             if proxy is not None:
                 await proxy.__aexit__(exc_type, exc_value, traceback)
-
-    def __del__(self) -> None:
-        # We use 'getattr' here, to manage the case where '__del__()' is called
-        # on a partially initiallized instance that raised an exception during
-        # the call to '__init__()'.
-        if getattr(self, "_state", None) == ClientState.OPENED:  # noqa: B009
-            # Unlike the sync case, we cannot silently close the client when
-            # it is garbage collected, because `.aclose()` is an async operation,
-            # but `__del__` is not.
-            #
-            # For this reason we require explicit close management for
-            # `AsyncClient`, and issue a warning on unclosed clients.
-            #
-            # The context managed style is usually preferable, because it neatly
-            # ensures proper resource cleanup:
-            #
-            #     async with httpx.AsyncClient() as client:
-            #         ...
-            #
-            # However, an explicit call to `aclose()` is also sufficient:
-            #
-            #    client = httpx.AsyncClient()
-            #    try:
-            #        ...
-            #    finally:
-            #        await client.aclose()
-            warnings.warn(
-                f"Unclosed {self!r}. "
-                "See https://www.python-httpx.org/async/#opening-and-closing-clients "
-                "for details."
-            )
index 1761287454eda71da388f4cc7512e2ee874c33ad..219d612f79ba5257e6b65e5cb2cac732d94eed74 100644 (file)
@@ -286,14 +286,6 @@ async def test_client_closed_state_using_with_block():
         await client.get("http://example.com")
 
 
-@pytest.mark.usefixtures("async_environment")
-async def test_deleting_unclosed_async_client_causes_warning():
-    client = httpx.AsyncClient(transport=httpx.MockTransport(hello_world))
-    await client.get("http://example.com")
-    with pytest.warns(UserWarning):
-        del client
-
-
 def unmounted(request: httpx.Request) -> httpx.Response:
     data = {"app": "unmounted"}
     return httpx.Response(200, json=data)