Skip to content

feat: add arithmetic operators to Hbar#2286

Draft
De-real-iManuel wants to merge 2 commits into
hiero-ledger:mainfrom
De-real-iManuel:feat/hbar-arithmetic-operators
Draft

feat: add arithmetic operators to Hbar#2286
De-real-iManuel wants to merge 2 commits into
hiero-ledger:mainfrom
De-real-iManuel:feat/hbar-arithmetic-operators

Conversation

@De-real-iManuel
Copy link
Copy Markdown
Contributor

Summary

Adds __add__, __sub__, and __abs__ dunder methods to the Hbar class, enabling native Python arithmetic syntax for HBAR amounts.

Changes

  • Added __add__, __sub__, __abs__ to src/hiero_sdk_python/hbar.py
  • All methods follow the existing comparison operator pattern (type guard, NotImplemented for non-Hbar operands)
  • Added 7 unit tests to tests/unit/hbar_test.py covering basic operations, type errors, and edge cases
  • All 58 tests pass, ruff linting passes

Fixes #2274

Signed-off-by: Emmanuel Nwajari <De_real_iManuel@hotmail.com>
@De-real-iManuel De-real-iManuel requested review from a team as code owners May 14, 2026 16:42
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 14, 2026

Review Change Stack

Warning

Rate limit exceeded

@De-real-iManuel has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 44 minutes and 27 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 27fcb84b-ce93-4248-9c2a-f31a986778cf

📥 Commits

Reviewing files that changed from the base of the PR and between eb5a870 and 696368d.

📒 Files selected for processing (1)
  • tests/unit/hbar_test.py

Walkthrough

The Hbar class now supports basic arithmetic operations via three dunder methods: __add__ and __sub__ compute sums and differences of tinybar amounts, returning new Hbar instances, and __abs__ returns the absolute tinybar value. Non-Hbar operands trigger NotImplemented. Unit tests validate all cases including negative results, type errors, and zero handling.

Changes

Hbar Arithmetic Operators

Layer / File(s) Summary
Arithmetic operators implementation
src/hiero_sdk_python/hbar.py
Three dunder methods (__add__, __sub__, __abs__) enable native Python arithmetic syntax; each operates on the internal _amount_in_tinybar value and returns a new Hbar instance via Hbar.from_tinybars(), or NotImplemented for non-Hbar operands.
Arithmetic operator unit tests
tests/unit/hbar_test.py
Unit tests validate Hbar addition, subtraction (including negative outcomes), absolute value, correct TypeError raising when non-Hbar operands are used with + or -, and edge cases such as Hbar.ZERO in addition.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main change: adding arithmetic operators to the Hbar class.
Description check ✅ Passed The description clearly explains the changes made, lists the methods added, mentions tests added, and references the linked issue #2274.
Linked Issues check ✅ Passed All core objectives from #2274 are met: add, sub, abs implemented with type guards returning NotImplemented for non-Hbar operands, unit tests cover basic operations and edge cases, code style and linting verified.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing arithmetic operators for Hbar as specified in #2274; no unrelated modifications found.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📋 Issue Planner

Built with CodeRabbit's Coding Plans for faster development and fewer bugs.

View plan used: #2274

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: bb7eac1d-43bd-4b44-a6fa-9bf51abb74ec

📥 Commits

Reviewing files that changed from the base of the PR and between 7d3d212 and eb5a870.

📒 Files selected for processing (2)
  • src/hiero_sdk_python/hbar.py
  • tests/unit/hbar_test.py

Comment thread tests/unit/hbar_test.py
Comment thread tests/unit/hbar_test.py
Comment thread tests/unit/hbar_test.py
Comment thread tests/unit/hbar_test.py
…ests

Signed-off-by: Emmanuel Nwajari <De_real_iManuel@hotmail.com>
@De-real-iManuel
Copy link
Copy Markdown
Contributor Author

Addressed, I added isinstance checks and additional edge case tests in the follow-up commit.

@github-actions github-actions Bot added open to community review PR is open for community review and feedback queue:junior-committer PR awaiting initial quality review labels May 14, 2026
@De-real-iManuel
Copy link
Copy Markdown
Contributor Author

@ryjones

@exploreriii exploreriii added the status: discussion a new proposed feature that should be discussed before proceeding label May 17, 2026

def __add__(self, other: object) -> Hbar:
"""
Return a new Hbar representing the sum of this and another Hbar.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I am not certain if the doc strings are clear here. Should we update @De-real-iManuel ?


def __sub__(self, other: object) -> Hbar:
"""
Return a new Hbar representing the difference of this and another Hbar.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same here

@github-actions
Copy link
Copy Markdown

Hello, this is the OfficeHourBot.

This is a reminder that the Hiero Python SDK Office Hours will begin in approximately 3 hours (14:00 UTC).

This session provides an opportunity to ask questions regarding this Pull Request.

Details:

Disclaimer: This is an automated reminder. Please verify the schedule here for any changes.

From,
The Python SDK Team

@manishdait
Copy link
Copy Markdown
Contributor

@De-real-iManuel, thanks for the PR and the work on this!

At the moment, adding arithmetic operators to Hbar feels a bit out of scope for the SDK, mainly because the other SDKs in the Hiero ecosystem currently do not expose similar functionality. Since this could introduce cross SDK API consistency differences, I think it would be better to first discuss it in the SDK-Collaboration Hub and get feedback on it

Copy link
Copy Markdown
Contributor

@exploreriii exploreriii left a comment

Choose a reason for hiding this comment

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

Hi @De-real-iManuel
When you have some time, would you mind creating an issue here https://github.com/hiero-ledger/sdk-collaboration-hub/issues outlining your proposal.
This way it can be rolled out across the SDKs and we support similar services
Meanwhile as this is in discussion, I am converting this to draft

@exploreriii exploreriii marked this pull request as draft May 21, 2026 09:11
@exploreriii exploreriii added dev: blocked developer unable to proceed and removed queue:junior-committer PR awaiting initial quality review open to community review PR is open for community review and feedback labels May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dev: blocked developer unable to proceed status: discussion a new proposed feature that should be discussed before proceeding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add arithmetic operators to Hbar (+, -, abs)

4 participants