Cache failing callable defaults too in _get_default_cached#1965
Merged
dmontagu merged 1 commit intoMay 25, 2026
Conversation
Cubic r3296066209: the cache only memoised successful values, so a callable default that raises escapes the cache — first invocation raises before the assignment, subsequent invocations re-invoke. In the worst case a failing callable could fire three times in one `get()` (in `_get_serialized_default` → `_resolve_code_default` → outer-`except` fallback in `_resolve`). Switched the cache to record `(ok: bool, value_or_exception: Any)`. A successful invocation caches the value as before; a raising invocation caches the exception so re-entry re-raises without re-running the callable. New regression test `test_failing_callable_default_invoked_once_per_get` registers an `always_raises` callable default, calls `get()`, and asserts `call_count == 1`. (Without the fix the count was 3.)
f3e694c
into
feature/variable-composition-native-handlebars
15 checks passed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Addresses cubic r3296066209 on #1954.
_get_default_cachedonly memoised successful values, so a callable default that raises escapes the cache — first invocation raises before the assignment, subsequent invocations re-invoke. Worst case: three invocations in oneget()(in_get_serialized_default,_resolve_code_default, and the outer-exceptfallback in_resolve).Switched the cache to record
(ok: bool, value_or_exception: Any). Success caches the value as before; failure caches the exception so re-entry re-raises without re-invoking. Matches cubic's suggested patch.Test
New
test_failing_callable_default_invoked_once_per_getregisters analways_raisescallable default, callsget(), assertscall_count == 1. Without the fix the count was 3.513 tests pass; pyright + ruff format + check clean.