From 7a046618f748c24ab05bd2adf683b8c24a72887b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 27 Sep 2024 10:55:41 +0100 Subject: [PATCH] Drop `sniffio` requirement. (#3323) --- CHANGELOG.md | 1 + httpx/_transports/asgi.py | 25 +++++++++++++++++++------ httpx/_utils.py | 17 ++--------------- pyproject.toml | 1 - 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25669250b5..50c5a5de83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/httpx/_transports/asgi.py b/httpx/_transports/asgi.py index 8578d4aeff..2bc4efae0e 100644 --- a/httpx/_transports/asgi.py +++ b/httpx/_transports/asgi.py @@ -2,8 +2,6 @@ import typing -import sniffio - from .._models import Request, Response from .._types import AsyncByteStream from .base import AsyncBaseTransport @@ -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): diff --git a/httpx/_utils.py b/httpx/_utils.py index a9ece19438..cf7f5bcad5 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index c4c188052e..2b83de5f12 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,6 @@ dependencies = [ "httpcore==1.*", "anyio", "idna", - "sniffio", ] dynamic = ["readme", "version"]