Skip to content

pybind: Add __hash__ to all binding types - #627

Merged
jmr merged 5 commits into
google:masterfrom
deustis:deustis/fix_hash_bindings
May 29, 2026
Merged

jmr merged 5 commits into
google:masterfrom
deustis:deustis/fix_hash_bindings

Conversation

@deustis

@deustis deustis commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

In Python 3, defining __eq__ without __hash__ implicitly sets __hash__ = None, making the type unhashable. All seven affected types raised:

TypeError: unhashable type: 's2geometry_bindings.S1Angle'

Adds __hash__ to S1Angle, S1Interval, R1Interval, R2Point, R2Rect, S2LatLng, and S2Point using absl::Hash on their underlying values.

@deustis deustis changed the title pybind: Add __hash__ to all binding types that define __eq__ pybind: Add __hash__ to all binding types May 13, 2026
@deustis
deustis marked this pull request as ready for review May 13, 2026 04:49

@jmr jmr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Similar for the rest of the classes.

Comment thread src/python/r2point_bindings.cc Outdated
@deustis

deustis commented May 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Updated R2Point, S2Point, and S2LatLng to use absl::HashOf(self) — those three already have AbslHashValue defined (BasicVector and s2latlng.h). That change is pushed.

The remaining four — S1Angle, S1Interval, R1Interval, R2Rect — don't have AbslHashValue. For those, should we add AbslHashValue to their C++ headers so we can use the same pattern, or drop __hash__ from their Python bindings entirely?

@deustis
deustis requested a review from jmr May 13, 2026 15:54
@deustis

deustis commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

@jmr, just checking on this PR? (Also #625 and #626)

Comment thread src/python/r1interval_bindings.cc Outdated
Comment thread src/python/s1angle_bindings.cc Outdated
@deustis
deustis force-pushed the deustis/fix_hash_bindings branch from a2e57e6 to 040dd76 Compare May 22, 2026 15:46
@deustis
deustis requested a review from jmr May 22, 2026 15:53
@deustis
deustis force-pushed the deustis/fix_hash_bindings branch from 040dd76 to 1b627a2 Compare May 22, 2026 21:15
@jmr

jmr commented May 26, 2026

Copy link
Copy Markdown
Member

This has merge conflicts, otherwise LGTM.

deustis added 5 commits May 26, 2026 15:13
In Python 3, defining __eq__ without __hash__ sets __hash__ = None,
making the type unhashable. All seven affected types raised:

  TypeError: unhashable type: 's2geometry_bindings.S1Angle'

Adds __hash__ to S1Angle, S1Interval, R1Interval, R2Point, R2Rect,
S2LatLng, and S2Point using absl::Hash on their underlying values.
These types already define AbslHashValue, so absl::HashOf(self) is
more idiomatic than manually extracting and hashing fields.
Use AbslHashValue free functions in the respective headers so that the
Python bindings can call absl::HashOf(self) uniformly instead of
manually assembling a std::pair/tuple over the component fields.
…2Rect

Uses absl::VerifyTypeImplementsAbslHashCorrectly to confirm the newly
added AbslHashValue functions satisfy the hash contract.
@deustis
deustis force-pushed the deustis/fix_hash_bindings branch from 1b627a2 to 918290e Compare May 26, 2026 15:14
@deustis

deustis commented May 26, 2026

Copy link
Copy Markdown
Contributor Author

This has merge conflicts, otherwise LGTM.

Rebased

@jmr
jmr merged commit 282b35b into google:master May 29, 2026
40 of 41 checks passed
@jmr jmr mentioned this pull request May 29, 2026
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.

2 participants