You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
\ cache_disabled()\ directly mutated the global _caching_enabled\ boolean. In asynchronous environments, calling \with cache_disabled():\ in one task inadvertently disabled caching globally for concurrent tasks running in the same process loop.
Changes
Added a \ContextVar[Optional[bool]]\ (_caching_enabled_override) defaulting to \None.
Implemented _is_caching_enabled()\ to check the context-local override first, falling back to global _caching_enabled\ if \None.
Updated \cache_disabled()\ context manager to set and reset the \ContextVar\ token.
Retained global _caching_enabled\ so process-level switches like \disable_cache()\ continue to apply across worker threads.
Added unit tests in \ ests/test_cache.py\ covering async task isolation and thread behavior.
The ContextVar approach is exactly right for this, and the two-level design (_caching_enabled_override + fallback to global _caching_enabled) cleanly preserves the intended semantics: disable_cache() remains process-wide, cache_disabled() becomes context-local. Token-based set/reset handles nesting correctly too.
One thing worth documenting: asyncio.create_task() called inside a cache_disabled() block will spawn a task that inherits _caching_enabled_override = False - standard ContextVar propagation means the child sees the parent's context at spawn time. That may be the desired behavior (subtasks honor the same override), but it is potentially surprising. A sentence in the docstring would help users reason about it.
The async decorator path still carries # pragma: no cover. The new async tests hit _is_caching_enabled() correctly, but the if not _is_caching_enabled() guard inside the async wrapper itself stays excluded from the coverage report. Not a blocker, but you may want to remove the pragma or add a direct test through @cache on an async def function.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1983
Context
\ cache_disabled()\ directly mutated the global _caching_enabled\ boolean. In asynchronous environments, calling \with cache_disabled():\ in one task inadvertently disabled caching globally for concurrent tasks running in the same process loop.
Changes