pybind: pos validation and class attribute constants for S2CellId bindings#625
Merged
Merged
Conversation
- Add S2CellId::kMaxPosition constant (2^61 - 1) to s2cell_id.h and add C++ unit test verifying valid/overflow behavior - Validate pos argument in from_face_pos_level; expose MAX_POSITION as a Python class attribute; add Python test for pos out of range - Switch constants to cls.attr() pattern; update README to document this style - Fix BUILD: add @abseil-cpp//absl/strings dep to s2cell_id_bindings
Contributor
Author
|
@jmr, couple of fast follow improvements here to be able to validate the position attribute, and I also found a way to use python class attributes for constants as you had suggested previously. |
jmr
reviewed
May 20, 2026
| static constexpr int kMaxLevel = | ||
| S2::kMaxCellLevel; // Valid levels: 0..kMaxLevel | ||
| static constexpr int kPosBits = 2 * kMaxLevel + 1; | ||
| static constexpr uint64_t kMaxPosition = (~uint64_t{0}) >> kFaceBits; |
Member
There was a problem hiding this comment.
this line is confusing swig. use static_cast or #ifndef SWIG
Contributor
Author
There was a problem hiding this comment.
Done, switched to static_cast<uint64_t>.
jmr
reviewed
May 20, 2026
| constexpr int face = 5; | ||
| constexpr uint64_t kOverflowPos = S2CellId::kMaxPosition + 1; | ||
| S2CellId overflow_id( | ||
| (static_cast<uint64_t>(face) << S2CellId::kPosBits) + (kOverflowPos | 1)); |
jmr
reviewed
May 20, 2026
| } | ||
| } | ||
|
|
||
| TEST(S2CellId, MaxPosition) { |
Member
There was a problem hiding this comment.
Split into two test cases: max position is valid, larger than max position is invalid
Contributor
Author
There was a problem hiding this comment.
Done, split into MaxPositionIsValid and PositionAboveMaxIsInvalid.
- Use static_cast<uint64_t> instead of uint64_t{0} brace-init in the
kMaxPosition definition to avoid confusing SWIG
- Split MaxPosition test into MaxPositionIsValid and
PositionAboveMaxIsInvalid
- Use uint64_t{face} in the overflow test per style suggestion
Contributor
Author
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
S2CellId::kMaxPositionconstant (2^61 - 1) tos2cell_id.hwith a C++ unit test verifying valid and overflow behaviorposargument inS2CellId.from_face_pos_levelPython binding; exposeMAX_POSITIONas a Python class attributecls.attr()pattern for uniform style regardless of whether constants come fromstatic constexprmembers or enum values; document in README@abseil-cpp//absl/stringsdep tos2cell_id_bindings