Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 191
Raise a neater RuntimeError when the correct async deps are not installed.#826
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lovelydinosaur
merged 5 commits into
master
from
neater-exception-when-async-deps-not-installedNov 2, 2023
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a944e2d
Raise a neater RuntimeError when the correct async deps are not insta…
lovelydinosaur 5d6209a
Run scripts/unasync
lovelydinosaur 17dd578
Update CHANGELOG
lovelydinosaur 8c1b716
Merge branch 'master' into neater-exception-when-async-deps-not-insta…
lovelydinosaur c46ffec
Merge branch 'master' into neater-exception-when-async-deps-not-insta…
lovelydinosaur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -24,13 +24,23 @@ def current_async_library() -> str: | ||
| try: | ||
| import sniffio | ||
| except ImportError: # pragma: nocover | ||
| return "asyncio" | ||
| environment = sniffio.current_async_library() | ||
| environment = "asyncio" | ||
| else: | ||
| environment = sniffio.current_async_library() | ||
| if environment not in ("asyncio", "trio"): # pragma: nocover | ||
| raise RuntimeError("Running under an unsupported async environment.") | ||
| if environment == "asyncio" and anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with asyncio requires installation of 'httpcore[asyncio]'." | ||
| ) | ||
| if environment == "trio" and trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with trio requires installation of 'httpcore[trio]'." | ||
| ) | ||
| return environment | ||
| @@ -45,16 +55,8 @@ def setup(self) -> None: | ||
| """ | ||
| self._backend = current_async_library() | ||
| if self._backend == "trio": | ||
| if trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with trio requires installation of 'httpcore[trio]'." | ||
| ) | ||
| self._trio_lock = trio.Lock() | ||
| else: | ||
| if anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with asyncio requires installation of 'httpcore[asyncio]'." | ||
| ) | ||
| elif self._backend == "asyncio": | ||
| self._anyio_lock = anyio.Lock() | ||
| async def __aenter__(self) -> "AsyncLock": | ||
| @@ -63,7 +65,7 @@ async def __aenter__(self) -> "AsyncLock": | ||
| if self._backend == "trio": | ||
| await self._trio_lock.acquire() | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| await self._anyio_lock.acquire() | ||
| return self | ||
| @@ -76,7 +78,7 @@ async def __aexit__( | ||
| ) -> None: | ||
| if self._backend == "trio": | ||
| self._trio_lock.release() | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| self._anyio_lock.release() | ||
| @@ -91,16 +93,8 @@ def setup(self) -> None: | ||
| """ | ||
| self._backend = current_async_library() | ||
| if self._backend == "trio": | ||
| if trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with trio requires installation of 'httpcore[trio]'." | ||
| ) | ||
| self._trio_event = trio.Event() | ||
| else: | ||
| if anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with asyncio requires installation of 'httpcore[asyncio]'." | ||
| ) | ||
| elif self._backend == "asyncio": | ||
| self._anyio_event = anyio.Event() | ||
| def set(self) -> None: | ||
| @@ -109,30 +103,20 @@ def set(self) -> None: | ||
| if self._backend == "trio": | ||
| self._trio_event.set() | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| self._anyio_event.set() | ||
| async def wait(self, timeout: Optional[float] = None) -> None: | ||
| if not self._backend: | ||
| self.setup() | ||
| if self._backend == "trio": | ||
| if trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with trio requires installation of 'httpcore[trio]'." | ||
| ) | ||
| trio_exc_map: ExceptionMapping = {trio.TooSlowError: PoolTimeout} | ||
| timeout_or_inf = float("inf") if timeout is None else timeout | ||
| with map_exceptions(trio_exc_map): | ||
| with trio.fail_after(timeout_or_inf): | ||
| await self._trio_event.wait() | ||
| else: | ||
| if anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with asyncio requires installation of 'httpcore[asyncio]'." | ||
| ) | ||
| elif self._backend == "asyncio": | ||
| anyio_exc_map: ExceptionMapping = {TimeoutError: PoolTimeout} | ||
| with map_exceptions(anyio_exc_map): | ||
| with anyio.fail_after(timeout): | ||
| @@ -151,20 +135,10 @@ def setup(self) -> None: | ||
| """ | ||
| self._backend = current_async_library() | ||
| if self._backend == "trio": | ||
| if trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with trio requires installation of 'httpcore[trio]'." | ||
| ) | ||
| self._trio_semaphore = trio.Semaphore( | ||
| initial_value=self._bound, max_value=self._bound | ||
| ) | ||
| else: | ||
| if anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with asyncio requires installation of 'httpcore[asyncio]'." | ||
| ) | ||
| elif self._backend == "asyncio": | ||
| self._anyio_semaphore = anyio.Semaphore( | ||
| initial_value=self._bound, max_value=self._bound | ||
| ) | ||
| @@ -175,13 +149,13 @@ async def acquire(self) -> None: | ||
| if self._backend == "trio": | ||
| await self._trio_semaphore.acquire() | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| await self._anyio_semaphore.acquire() | ||
| async def release(self) -> None: | ||
| if self._backend == "trio": | ||
| self._trio_semaphore.release() | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| self._anyio_semaphore.release() | ||
| @@ -201,24 +175,14 @@ def __init__(self) -> None: | ||
| self._backend = current_async_library() | ||
| if self._backend == "trio": | ||
| if trio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with trio requires installation of 'httpcore[trio]'." | ||
| ) | ||
| self._trio_shield = trio.CancelScope(shield=True) | ||
| else: | ||
| if anyio is None: # pragma: nocover | ||
| raise RuntimeError( | ||
| "Running with asyncio requires installation of 'httpcore[asyncio]'." | ||
| ) | ||
| elif self._backend == "asyncio": | ||
| self._anyio_shield = anyio.CancelScope(shield=True) | ||
| def __enter__(self) -> "AsyncShieldCancellation": | ||
| if self._backend == "trio": | ||
| self._trio_shield.__enter__() | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| self._anyio_shield.__enter__() | ||
lovelydinosaur marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return self | ||
| @@ -230,7 +194,7 @@ def __exit__( | ||
| ) -> None: | ||
| if self._backend == "trio": | ||
| self._trio_shield.__exit__(exc_type, exc_value, traceback) | ||
| else: | ||
| elif self._backend == "asyncio": | ||
| self._anyio_shield.__exit__(exc_type, exc_value, traceback) | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.