Skip to content

Fix for Inconsistent equality and hashing#50

Merged
nstarman merged 9 commits intomainfrom
finding-autofix-93e102d0
Apr 14, 2026
Merged

Fix for Inconsistent equality and hashing#50
nstarman merged 9 commits intomainfrom
finding-autofix-93e102d0

Conversation

@nstarman
Copy link
Copy Markdown
Contributor

To fix this safely, implement __eq__ on ImmutableMap so 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 when other is an ImmutableMap, and compare against dict(other.items()) when other is any Mapping; otherwise return NotImplemented.

In src/xmmutablemap/_core.py, add a new __eq__ method inside ImmutableMap (near other protocol methods, e.g., after __len__). No new imports are required since Any and Mapping are already imported. This preserves existing functionality while making hashing/equality contract explicit and correct.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

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>
@nstarman nstarman added this to the v0.2.x milestone Apr 14, 2026
@nstarman
Copy link
Copy Markdown
Contributor Author

@copilot fix the pre-commit failure.

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

@copilot fix the pre-commit failure.

Fixed in commit 3fd3eda. I reproduced the CI pre-commit failure locally, applied the minimal Ruff-required fix (__eq__(self, other: object)), and re-ran pre-commit plus targeted tests successfully.

@nstarman nstarman marked this pull request as ready for review April 14, 2026 04:04
Copilot AI review requested due to automatic review settings April 14, 2026 04:04
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 for ImmutableMap instances.
  • Add cross-Mapping equality by comparing against dict(other.items()).
  • Return NotImplemented for non-mapping comparisons.

Comment thread src/xmmutablemap/_core.py
Comment thread src/xmmutablemap/_core.py
Comment thread src/xmmutablemap/_core.py Outdated
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>
Comment thread src/xmmutablemap/_core.py Fixed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 return NotImplemented for non-mappings.
  • Update ImmutableMap.__hash__ to be order-insensitive by hashing a frozenset of 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.

Comment thread tests/test_immutablemap.py
Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 14, 2026

@copilot fix the pre-commit failure.

Pre-commit is now passing on the current PR head (2eabfa7). I re-ran the full pre-commit session locally and it completed successfully.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 another ImmutableMap and against generic Mapping types.
  • Updated ImmutableMap.__hash__ to be insertion-order-insensitive (via hashing a frozenset of 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.

Comment thread src/xmmutablemap/_core.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 other Mapping implementations.
  • 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.

Comment thread src/xmmutablemap/_core.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 against ImmutableMap and other Mapping instances (otherwise returns NotImplemented).
  • 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).

@nstarman nstarman merged commit d7d0478 into main Apr 14, 2026
20 checks passed
@nstarman nstarman deleted the finding-autofix-93e102d0 branch April 14, 2026 05:40
meeseeksmachine pushed a commit to meeseeksmachine/xmmutablemap that referenced this pull request Apr 14, 2026
nstarman added a commit that referenced this pull request Apr 14, 2026
Co-authored-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants