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