Skip to content

Commit

Permalink
Drop sniffio requirement. (#3323)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomchristie authored Sep 27, 2024
1 parent 5766bf5 commit 7a04661
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* The deprecated `proxies` argument has now been removed.
* The deprecated `app` argument has now been removed.
* The `URL.raw` property has now been removed.
* The `sniffio` project dependency has now been removed.

## 0.27.2 (27th August, 2024)

Expand Down
25 changes: 19 additions & 6 deletions httpx/_transports/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import typing

import sniffio

from .._models import Request, Response
from .._types import AsyncByteStream
from .base import AsyncBaseTransport
Expand All @@ -28,15 +26,30 @@
__all__ = ["ASGITransport"]


def is_running_trio() -> bool:
try:
# sniffio is a dependency of trio.

# See https://github.com/python-trio/trio/issues/2802
import sniffio

if sniffio.current_async_library() == "trio":
return True
except ImportError: # pragma: nocover
pass

return False


def create_event() -> Event:
if sniffio.current_async_library() == "trio":
if is_running_trio():
import trio

return trio.Event()
else:
import asyncio

return asyncio.Event()
import asyncio

return asyncio.Event()


class ASGIResponseStream(AsyncByteStream):
Expand Down
17 changes: 2 additions & 15 deletions httpx/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from pathlib import Path
from urllib.request import getproxies

import sniffio

from ._types import PrimitiveData

if typing.TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -289,29 +287,18 @@ def peek_filelike_length(stream: typing.Any) -> int | None:


class Timer:
async def _get_time(self) -> float:
library = sniffio.current_async_library()
if library == "trio":
import trio

return trio.current_time()
else:
import asyncio

return asyncio.get_event_loop().time()

def sync_start(self) -> None:
self.started = time.perf_counter()

async def async_start(self) -> None:
self.started = await self._get_time()
self.started = time.perf_counter()

def sync_elapsed(self) -> float:
now = time.perf_counter()
return now - self.started

async def async_elapsed(self) -> float:
now = await self._get_time()
now = time.perf_counter()
return now - self.started


Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ dependencies = [
"httpcore==1.*",
"anyio",
"idna",
"sniffio",
]
dynamic = ["readme", "version"]

Expand Down

0 comments on commit 7a04661

Please sign in to comment.