Skip to content

pybind: pos validation and class attribute constants for S2CellId bindings#625

Merged
jmr merged 3 commits into
google:masterfrom
deustis:deustis/s2cell_id_binding_fixes
May 22, 2026
Merged

pybind: pos validation and class attribute constants for S2CellId bindings#625
jmr merged 3 commits into
google:masterfrom
deustis:deustis/s2cell_id_binding_fixes

Conversation

@deustis
Copy link
Copy Markdown
Contributor

@deustis deustis commented May 12, 2026

Summary

  • Add S2CellId::kMaxPosition constant (2^61 - 1) to s2cell_id.h with a C++ unit test verifying valid and overflow behavior
  • Validate pos argument in S2CellId.from_face_pos_level Python binding; expose MAX_POSITION as a Python class attribute
  • Switch Python binding constants to cls.attr() pattern for uniform style regardless of whether constants come from static constexpr members or enum values; document in README
  • Fix BUILD: add missing @abseil-cpp//absl/strings dep to s2cell_id_bindings

- 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
@deustis
Copy link
Copy Markdown
Contributor Author

deustis commented May 12, 2026

@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.

@deustis deustis changed the title pybind: Address review comments on S2CellId bindings pybind: pos validation and class attribute constants for S2CellId bindings May 12, 2026
Comment thread src/s2/s2cell_id.h Outdated
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;
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.

this line is confusing swig. use static_cast or #ifndef SWIG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, switched to static_cast<uint64_t>.

Comment thread src/s2/s2cell_id_test.cc Outdated
constexpr int face = 5;
constexpr uint64_t kOverflowPos = S2CellId::kMaxPosition + 1;
S2CellId overflow_id(
(static_cast<uint64_t>(face) << S2CellId::kPosBits) + (kOverflowPos | 1));
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.

here you can use uint64_t{face}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment thread src/s2/s2cell_id_test.cc Outdated
}
}

TEST(S2CellId, MaxPosition) {
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.

Split into two test cases: max position is valid, larger than max position is invalid

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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
@deustis deustis requested a review from jmr May 20, 2026 23:50
@deustis
Copy link
Copy Markdown
Contributor Author

deustis commented May 20, 2026

@jmr, ready for another look here (also #626 and #627)

@jmr jmr merged commit 9864ff7 into google:master May 22, 2026
40 of 41 checks passed
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