_util: make DictMixin unhashable to fix hash/equality contract violation - #716
Merged
Merged
Conversation
DictMixin.__eq__ compares objects by their content (tag key-value pairs), but __hash__ was set to object.__hash__ (identity-based). This violates Python's requirement that equal objects have equal hash values: two tag objects loaded from the same file compare equal yet produce different hashes, silently breaking set membership, dict keys, and any code that relies on the hash/equality invariant. Fix by setting __hash__ = None, making DictMixin instances explicitly unhashable - consistent with Python's built-in mutable dict. Fixes quodlibet#603
phw
approved these changes
Jun 27, 2026
Collaborator
There was a problem hiding this comment.
Right. the DictMixin must not be hashable. The docs say explicitly:
If a class defines mutable objects and implements an eq() method, it should not implement hash(), since the implementation of hashable collections requires that a key’s hash value is immutable (if the object’s hash value changes, it will be in the wrong hash bucket).
There is some small potential of this breaking some code, though, if code relied on the hasability of the instances. But regardless, I think this should be fixed and we mention this change prominently in the changelog.
Member
|
risky, but makes sense. There are more places with the same pattern |
lazka
approved these changes
Jun 27, 2026
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 #603.
Problem
DictMixin.__eq__compares objects by their tag content:But
__hash__was set toobject.__hash__, which is identity-based. This violates Python's fundamental requirement that objects which compare equal must have the same hash value:Any code that puts FLAC (or other tag) objects into sets, uses them as dict keys, or relies on the hash/equality contract is silently broken.
Fix
Set
__hash__ = NoneinDictMixin, making instances explicitly unhashable — consistent with Python's own mutabledict. AllDictMixinsubclasses (tag types, file types) are mutable mappings, so unhashability is the correct behavior.Testing
Added a regression test in
TDictMixin.test_unhashable. Full suite (3993 tests) passes.This pull request was prepared with the assistance of AI, under my direction and review.