Repository navigation
Conversation
added 3 commits
September 1, 2026 00:08
Python 3.14 raises TypeError when deque.remove() calls __hash__ on a key containing an unhashable type (e.g. dict inside tuple). Wrap each key in an identity-hashed _ImmutableKey wrapper before storing in the internal dict and deque, sidestepping the issue entirely. Keys retrieved from the cache are unwrapped before being returned. Also update test_pickleable and test_copy to use the public cache API (.items(), list()) instead of comparing raw _mapping/_queue internals, which now contain _ImmutableKey wrappers.
test_escaped used hasattr(value, '__html__') which always returns True for ChainableUndefined since __getattr__ returns self, and self has __html__. Fix by checking isinstance(value, Markup) instead.
The _ImmutableKey wrapper used identity comparison (is) and always used object.__hash__, breaking equality for string keys created fresh on each get_template() call. Fix by using value equality (==) and try hash(key) first, falling back to identity hash only for unhashable types.
Member
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.
Fixes #2180
Python 3.14 raises a TypeError when an unhashable type (e.g. a dict) appears inside a tuple used as a dict key. The LRUCache in
jinja2/utils.pyuses a plain dict internally, and when a cache key contains an unhashable element,deque.remove()internally calls__hash__on the key during the linear search, causing a crash.This fix wraps each key in an
_ImmutableKeyclass that usesobject.__hash__(identity-based hashing), sidestepping the issue entirely. The wrapper is transparent -- keys retrieved from the cache are unwrapped before being returned.Before:
TypeError: cannot use 'tuple' as a dict key (unhashable type: 'dict')After: Cache works correctly with any key type