✨ Make blocks and fields hashable#563
Merged
Merged
Conversation
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.
Summary
Block.__eq__andField.__eq__were defined without__hash__, making all blocks and fields unhashable (set(library.entries)raisedTypeError). This PR decides and implements the hash/equality semantics:__dict__.(type, start_line, raw)for blocks,(type, start_line, key)for fields — so equal objects always hash equal (hash contract holds). Mutable/unhashable attributes (fields,parser_metadata, field values that may be lists/NameParts) are excluded, so a block doesn't lose its hash when middleware mutates values. As with any hashable-mutable design, mutating the hashed attributes while an object sits in a set/dict is unsupported.Library.remove/replaceare now identity-first: with two equal blocks in a library (e.g. identical comments), the exact instance passed is removed/replaced; equality matching remains as fallback, so existing code keeps working.Tests
remove/replacewith two equal comments operate on the passed instance, verified viais.Fixes #537
🤖 Generated with Claude Code