Block.__eq__ and Field.__eq__ were defined without __hash__, making all blocks and fields unhashable (set(library.entries) raised TypeError). This PR decides and implements the hash/equality semantics:
Equality stays value-based (unchanged): same type + same __dict__.
Hashes use a stable subset of the eq-attributes — (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/replace are 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
Equal entries/strings/fields hash equal; set deduplication and dict lookup work (including fields with unhashable list values).
remove/replace with two equal comments operate on the passed instance, verified via is.
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
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__ and Field.__eq__ were defined without __hash__, making all blocks and fields unhashable (set(library.entries) raised TypeError). This PR decides and implements the hash/equality semantics:
Tests
Fixes #537
🤖 Generated with Claude Code