Fix for Inconsistent equality and hashing#50
Conversation
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
|
@copilot fix the pre-commit failure. |
Agent-Logs-Url: https://github.com/GalacticDynamics/xmmutablemap/sessions/34262293-9387-470d-8439-ed11f3ebb34f Co-authored-by: nstarman <8949649+nstarman@users.noreply.github.com>
Fixed in commit |
There was a problem hiding this comment.
Pull request overview
Implements explicit equality semantics for ImmutableMap to compare contained key/value pairs and interoperate with other Mapping implementations, aligning with the PR goal of fixing equality/hashing consistency for this immutable, hashable mapping type.
Changes:
- Add
ImmutableMap.__eq__that compares underlying dicts forImmutableMapinstances. - Add cross-
Mappingequality by comparing againstdict(other.items()). - Return
NotImplementedfor non-mapping comparisons.
Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com> Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
Agent-Logs-Url: https://github.com/GalacticDynamics/xmmutablemap/sessions/f39253f1-d8ea-46eb-8a8e-f004866a473d Co-authored-by: nstarman <8949649+nstarman@users.noreply.github.com>
Agent-Logs-Url: https://github.com/GalacticDynamics/xmmutablemap/sessions/f39253f1-d8ea-46eb-8a8e-f004866a473d Co-authored-by: nstarman <8949649+nstarman@users.noreply.github.com>
Agent-Logs-Url: https://github.com/GalacticDynamics/xmmutablemap/sessions/f39253f1-d8ea-46eb-8a8e-f004866a473d Co-authored-by: nstarman <8949649+nstarman@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes ImmutableMap’s equality/hashing contract by defining mapping-based equality and making hashing independent of insertion order, improving correctness and interoperability across mapping types.
Changes:
- Add
ImmutableMap.__eq__to compare by contained key/value pairs and returnNotImplementedfor non-mappings. - Update
ImmutableMap.__hash__to be order-insensitive by hashing afrozensetof items. - Update/add tests to validate order-insensitive hashing and equality against common mapping types.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/xmmutablemap/_core.py |
Implements __eq__ for mapping-based equality and updates __hash__ to be insertion-order independent. |
tests/test_immutablemap.py |
Updates hash expectation and adds tests for mapping equality interop and order-insensitive equality/hash. |
Pre-commit is now passing on the current PR head ( |
There was a problem hiding this comment.
Pull request overview
This PR aims to fix the equality/hashing contract of ImmutableMap by making equality based on contained key/value pairs (including interoperability with other Mapping implementations) and by making hashing order-insensitive.
Changes:
- Added
ImmutableMap.__eq__with optimized paths for comparing against anotherImmutableMapand against genericMappingtypes. - Updated
ImmutableMap.__hash__to be insertion-order-insensitive (via hashing afrozensetof items). - Expanded tests to cover cross-mapping equality (including reverse comparisons) and to assert equal hashes across different insertion orders.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/xmmutablemap/_core.py |
Implements __eq__ and changes __hash__ to align equality/hash behavior and mapping interoperability. |
tests/test_immutablemap.py |
Updates hash expectations and adds equality/hash contract tests across mapping types and insertion orders. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes the ImmutableMap equality/hash consistency by making equality explicitly item-based (including interoperability with other Mapping types) and updating hashing to be order-insensitive, aligning with mapping equality semantics.
Changes:
- Added
ImmutableMap.__eq__to compare by key/value pairs and interoperate with otherMappingimplementations. - Updated
ImmutableMap.__hash__to hash based on an order-insensitive representation of items. - Added unit tests covering cross-mapping equality (including reverse comparisons) and hash/equality invariance across insertion order.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/xmmutablemap/_core.py |
Implements mapping-based __eq__ and updates __hash__ to be insertion-order independent. |
tests/test_immutablemap.py |
Adds tests validating mapping interoperability for equality and hash/equality consistency across insertion orders. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes the ImmutableMap equality/hash contract by defining equality in terms of contained key/value pairs (including interoperability with other Mapping types) and by making hashing independent of insertion order, aligning with standard mapping equality semantics.
Changes:
- Added
ImmutableMap.__eq__that compares items againstImmutableMapand otherMappinginstances (otherwise returnsNotImplemented). - Updated
ImmutableMap.__hash__to use an order-insensitive item representation (frozenset(items)). - Expanded tests to cover mapping interoperability (including reverse comparisons) and the equality/hash invariant across different insertion orders.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/xmmutablemap/_core.py |
Adds item-based __eq__ and updates __hash__ to be insertion-order-insensitive. |
tests/test_immutablemap.py |
Updates hash expectation and adds equality/hash contract tests (including symmetry vs. other mappings). |
Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
To fix this safely, implement
__eq__onImmutableMapso equality is based on contained key/value pairs, and ensure it interoperates with other mappings. The best approach is to compare the underlying data dict whenotheris anImmutableMap, and compare againstdict(other.items())whenotheris anyMapping; otherwise returnNotImplemented.In
src/xmmutablemap/_core.py, add a new__eq__method insideImmutableMap(near other protocol methods, e.g., after__len__). No new imports are required sinceAnyandMappingare already imported. This preserves existing functionality while making hashing/equality contract explicit and correct.Suggested fixes powered by Copilot Autofix. Review carefully before merging.