Skip to content

Tighten client closed-state behaviour - #1346

Merged
lovelydinosaur merged 2 commits into
masterfrom
tighter-closed-state-behaviour
Oct 6, 2020
Merged

Tighten client closed-state behaviour#1346
lovelydinosaur merged 2 commits into
masterfrom
tighter-closed-state-behaviour

Conversation

@lovelydinosaur

Copy link
Copy Markdown
Contributor

Closes #1317
Closes #1332

Tighens up the client open/closed state by making it tri-state under the hood: UNOPENED/OPENED/CLOSED.

End result of the changes here, are that only the following styles are valid...

client=httpx.Client()
client.request(...)
client.close() # If omitted, this will be called on `__del__` with sync client, or raise a warning on `__del__` with async client.# Cannot send requests anymore.

Or...

withhttpx.Client() asclient:
client.request(...)
# Cannot send requests anymore.

Because we're tri-state now we can make sure that simply importing but not using a client won't raise errors.
(Because under the hood, the client is initially in an UNOPENED state.)

# If we don't actually use this client, then `__del__` won't raise a warning here.client=httpx.AsyncClient()
  • Clients may no longer used once closed. Calling client.get(...) and pals will raise a runtime error if attempted.
  • Clients that have not sent a request should still call .close() on the underlying transports if closed.
  • AsyncClients that have not been opened should not raise a warning if deleted.

@lovelydinosaurlovelydinosaur added the user-experience Ensuring that users have a good experience using the library label Oct 6, 2020
@lovelydinosaurlovelydinosaur added this to the v0.16 milestone Oct 6, 2020
@lovelydinosaurlovelydinosaur mentioned this pull request Oct 6, 2020

@florimondmancaflorimondmanca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍👍

Fab!

@lovelydinosaur
lovelydinosaur merged commit 2a2bbe5 into masterOct 6, 2020
@lovelydinosaur
lovelydinosaur deleted the tighter-closed-state-behaviour branch October 6, 2020 12:38
@lovelydinosaurlovelydinosaur mentioned this pull request Oct 7, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

user-experienceEnsuring that users have a good experience using the library

Development

Successfully merging this pull request may close these issues.

Unintuitive behavior with using an AsyncClient after it's been aclosed Client.close() does not close transports if unused

2 participants

@lovelydinosaur@florimondmanca