Skip to content

Treat empty/non-str library overrides as unset - #60

Closed
Pitchfork-and-Torch wants to merge 1 commit into
python-trio:masterfrom
Pitchfork-and-Torch:cook/ignore-empty-non-str-library-name
Closed

Pitchfork-and-Torch wants to merge 1 commit into
python-trio:masterfrom
Pitchfork-and-Torch:cook/ignore-empty-non-str-library-name

Conversation

@Pitchfork-and-Torch

Copy link
Copy Markdown

Summary

Only treat non-empty str values from thread_local.name / current_async_library_cvar as library overrides.

Previously any non-None value was returned, so "" or an int leaked as a false library name. Invalid overrides are ignored and sniffing continues.

Test plan

  • sniffio/_tests/test_sniffio.py (incl. test_empty_name_ignored) passes

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.
@x42005e1f

Copy link
Copy Markdown

For type checking, there are already type hints. In this regard, runtime checks are redundant.

As for empty strings, that makes even less sense. Your PR treats them as equivalent to None, and that violates PEP 20 – The Zen of Python ("There should be one-- and preferably only one --obvious way to do it"; though it is hard to imagine a situation where someone would want to explicitly set None instead of using a set-reset pattern). And even if you took a different approach and raised an exception for "", it is just one of countless invalid values (arbitrary sequences of spaces, typos, etc.), and they would be detected at runtime anyway.

@A5rocks

A5rocks commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

I think setting this to an empty string is just like, sure whatever? Why should we assume that library code that integrates with sniffio, which is probably like 2 lines in a heavily tested async framework, can mess up this badly?

@A5rocks A5rocks closed this Sep 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants