Skip to content
Myr

Dont perform implicit close/warning on `__del__` (#2026)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Version 0.21.3 * Don't perform implict close on __del__

authored by Tom Christie · committed by GitHub · parent 0f1ff50 · Browse files · 2 files+0 added−46 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+0 added−46 removed

  • tests:+0 added−8 removed
  • other:+0 added−38 removed

2 files+0 added−46 removed

modified:httpx/_client.py-38 removed
1271 unmodified lines
1272 for transport in self._mounts.values():1273 if transport is not None:1274 transport.__exit__(exc_type, exc_value, traceback)12751276 def __del__(self) -> None:1277 # We use 'getattr' here, to manage the case where '__del__()' is called1278 # on a partially initiallized instance that raised an exception during1279 # the call to '__init__()'.1280 if getattr(self, "_state", None) == ClientState.OPENED: # noqa: B0091281 self.close()127612831277class AsyncClient(BaseClient):1278 """1279 An asynchronous HTTP client, with connection pooling, HTTP/2, redirects,1280 cookie persistence, etc.
694 unmodified lines
1975 await self._transport.__aexit__(exc_type, exc_value, traceback)1976 for proxy in self._mounts.values():1977 if proxy is not None:1978 await proxy.__aexit__(exc_type, exc_value, traceback)19861987 def __del__(self) -> None:1988 # We use 'getattr' here, to manage the case where '__del__()' is called1989 # on a partially initiallized instance that raised an exception during1990 # the call to '__init__()'.1991 if getattr(self, "_state", None) == ClientState.OPENED: # noqa: B0091992 # Unlike the sync case, we cannot silently close the client when1993 # it is garbage collected, because `.aclose()` is an async operation,1994 # but `__del__` is not.1995 #1996 # For this reason we require explicit close management for1997 # `AsyncClient`, and issue a warning on unclosed clients.1998 #1999 # The context managed style is usually preferable, because it neatly2000 # ensures proper resource cleanup:2001 #2002 # async with httpx.AsyncClient() as client:2003 # ...2004 #2005 # However, an explicit call to `aclose()` is also sufficient:2006 #2007 # client = httpx.AsyncClient()2008 # try:2009 # ...2010 # finally:2011 # await client.aclose()2012 warnings.warn(2013 f"Unclosed {self!r}. "2014 "See https://www.python-httpx.org/async/#opening-and-closing-clients "2015 "for details."2016 )
modified:tests/client/test_async_client.py-8 removed
284 unmodified lines
285 with pytest.raises(RuntimeError):286 await client.get("http://example.com")287288289@pytest.mark.usefixtures("async_environment")290async def test_deleting_unclosed_async_client_causes_warning():291 client = httpx.AsyncClient(transport=httpx.MockTransport(hello_world))292 await client.get("http://example.com")293 with pytest.warns(UserWarning):294 del client295296289def unmounted(request: httpx.Request) -> httpx.Response:290 data = {"app": "unmounted"}291 return httpx.Response(200, json=data)292
41 unmodified lines