From abc18df3eeba443db78a41477860b947a0348a86 Mon Sep 17 00:00:00 2001 From: Pitchfork-and-Torch Date: Fri, 18 Sep 2026 02:36:45 +0000 Subject: [PATCH] Treat empty/non-str library overrides as unset thread_local.name / current_async_library_cvar previously returned any non-None value, so "" or an int leaked as a false library name. Only accept non-empty str overrides; otherwise keep sniffing. --- sniffio/_impl.py | 6 ++++-- sniffio/_tests/test_sniffio.py | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/sniffio/_impl.py b/sniffio/_impl.py index c1a7bbf..428695c 100644 --- a/sniffio/_impl.py +++ b/sniffio/_impl.py @@ -64,11 +64,13 @@ async def generic_sleep(seconds): """ value = thread_local.name - if value is not None: + # Empty / non-str overrides are programmer errors; treat as unset so we + # keep sniffing instead of returning a false library name. + if isinstance(value, str) and value: return value value = current_async_library_cvar.get() - if value is not None: + if isinstance(value, str) and value: return value # Need to sniff for asyncio diff --git a/sniffio/_tests/test_sniffio.py b/sniffio/_tests/test_sniffio.py index 02945a9..5d5cf65 100644 --- a/sniffio/_tests/test_sniffio.py +++ b/sniffio/_tests/test_sniffio.py @@ -82,3 +82,26 @@ async def this_is_curio(): with pytest.raises(AsyncLibraryNotFoundError): current_async_library() + + +def test_empty_name_ignored(): + with pytest.raises(AsyncLibraryNotFoundError): + current_async_library() + old_name, thread_local.name = thread_local.name, "" + try: + with pytest.raises(AsyncLibraryNotFoundError): + current_async_library() + finally: + thread_local.name = old_name + token = current_async_library_cvar.set("") + try: + with pytest.raises(AsyncLibraryNotFoundError): + current_async_library() + finally: + current_async_library_cvar.reset(token) + old_name, thread_local.name = thread_local.name, 123 # type: ignore[assignment] + try: + with pytest.raises(AsyncLibraryNotFoundError): + current_async_library() + finally: + thread_local.name = old_name