fix: freeze unhashable args in Query.test to prevent TypeError#618
Open
gaoflow wants to merge 1 commit into
Open
fix: freeze unhashable args in Query.test to prevent TypeError#618gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
Passing list or dict values as extra arguments to Query().field.test() raised TypeError: unhashable type when the resulting QueryInstance was hashed (e.g. on first use with a caching Table). The other query methods (any, all, one_of, fragment) already call freeze() on their arguments; apply the same treatment to each element of the *args tuple in Query.test(). Fixes msiemens#517
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.
Problem
Passing a
listordictas an extra argument toQuery().field.test()raisesTypeError: unhashable type: 'list'(or'dict') the first time TinyDB triesto hash the resulting
QueryInstancefor the query cache.Reported in #514 (issue #517).
Root cause
Query.testbuilds its cache key tuple as:argsis a raw Python tuple that can contain unhashable objects (list,dict). Every other query method that accepts caller-supplied values(
any,all,one_of,fragment) already passes them through theexisting
freeze()helper before building the hash tuple.testwasthe only method that did not.
Fix
Apply
freeze()to each element ofargsbefore composing the hashtuple, matching the pattern used by the sibling methods:
freeze()recursively convertslist→tuple,dict→FrozenDict,and
set→frozenset, so nested unhashable structures are handled too.The test function still receives the original (unfrozen)
argsatcall-time; only the hash key is affected.
Tests
Added
test_custom_with_unhashable_paramsintests/test_queries.pycovering list args, dict args, same-args hash stability, and different-args
hash distinctness.