Skip to content

chore: wrap staking fields in StakingInfo wrapper class#1914

Open
Mounil2005 wants to merge 13 commits intohiero-ledger:mainfrom
Mounil2005:chore/Refactor-AccountInfo-class-to-use-the-staking_info
Open

chore: wrap staking fields in StakingInfo wrapper class#1914
Mounil2005 wants to merge 13 commits intohiero-ledger:mainfrom
Mounil2005:chore/Refactor-AccountInfo-class-to-use-the-staking_info

Conversation

@Mounil2005
Copy link
Contributor

Description:

Extract staking information fields from AccountInfo into a dedicated StakingInfo wrapper class following SDK patterns. This improves code organization and maintainability by encapsulating staking-related logic.

  • Create StakingInfo dataclass with _from_proto() and _to_proto() methods
  • Implement __str__() and __repr__() for staking info representation
  • Refactor AccountInfo to use single staking_info field instead of three flattened fields
  • Update all unit and integration tests to access staking fields through nested object

Related issue(s):

Fixes #1366

Notes for reviewer:

All 2224 unit tests passing. Integration tests updated to use nested field access pattern (info.staking_info.staked_account_id instead of info.staked_account_id). No breaking changes to public API beyond the AccountInfo field structure refactor.

Checklist

  • Documented (Code comments, README, etc.)
  • Tested (unit, integration, etc.)

Copilot AI review requested due to automatic review settings March 6, 2026 14:12
@Mounil2005 Mounil2005 requested review from a team as code owners March 6, 2026 14:12
@codecov
Copy link

codecov bot commented Mar 6, 2026

Codecov Report

❌ Patch coverage is 94.73684% with 3 lines in your changes missing coverage. Please review.

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1914   +/-   ##
=======================================
  Coverage   93.53%   93.54%           
=======================================
  Files         141      141           
  Lines        9146     9190   +44     
=======================================
+ Hits         8555     8597   +42     
- Misses        591      593    +2     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
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

Refactors AccountInfo to encapsulate staking-related data inside a dedicated staking_info object, aligning the Python SDK with the protobuf StakingInfo model and updating unit/integration tests to use nested field access.

Changes:

  • Add src/hiero_sdk_python/account/staking_info.py with _from_proto() / _to_proto() plus __str__ / __repr__.
  • Refactor AccountInfo to replace flattened staking fields with staking_info: Optional[StakingInfo].
  • Update unit + integration tests to access staking fields via info.staking_info.*.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/hiero_sdk_python/account/staking_info.py Introduces a new Account-scoped StakingInfo wrapper with proto conversion + formatting.
src/hiero_sdk_python/account/account_info.py Switches AccountInfo to a single staking_info field and updates proto conversions + formatting.
tests/unit/account_info_test.py Extends proto fixtures + assertions to cover nested staking info conversion.
tests/integration/account_update_transaction_e2e_test.py Updates staking assertions to use nested staking_info access.
tests/integration/account_create_transaction_e2e_test.py Updates staking assertions to use nested staking_info access.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 6, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

Replaces three flattened staking fields on AccountInfo with a single optional staking_info: StakingInfo field, updates proto (de)serialization, adjusts str/repr, and adds deprecated shim properties for backward compatibility. Unit and integration tests updated to use the nested staking_info structure.

Changes

Cohort / File(s) Summary
Core implementation
src/hiero_sdk_python/account/account_info.py
Replaces staked_account_id, staked_node_id, decline_staking_reward with staking_info: Optional[StakingInfo]; updates _from_proto/_to_proto to use StakingInfo (HasField/CopyFrom), updates __str__/__repr__, and adds deprecated properties that proxy to staking_info with DeprecationWarning. Also adds warnings and StakingInfo import.
Unit tests
tests/unit/account_info_test.py
Adds extensive tests for StakingInfo and its integration with AccountInfo: proto round-trips, presence/absence handling, deprecated property behavior, string/repr checks, and timestamp/timezone handling for stake_period_start.
Integration tests
tests/integration/account_create_transaction_e2e_test.py, tests/integration/account_update_transaction_e2e_test.py
Updates assertions to read staking values via info.staking_info.<field> (e.g., staked_account_id, staked_node_id, decline_reward) instead of top-level info.<field>.
Changelog
CHANGELOG.md
Documents the refactor: new StakingInfo wrapper, staking_info field on AccountInfo, and deprecation of the old flat accessors.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 78.72% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'chore: wrap staking fields in StakingInfo wrapper class' accurately summarizes the main change of refactoring AccountInfo to use a StakingInfo wrapper instead of flattened fields.
Description check ✅ Passed The description clearly relates to the changeset, explaining the extraction of staking fields into StakingInfo, implementation details, and test updates with reference to issue #1366.
Linked Issues check ✅ Passed The PR fully meets all coding requirements from issue #1366: removes flattened staking fields, adds staking_info field, implements from_proto/to_proto methods, updates str/repr, and updates all tests to use nested access pattern.
Out of Scope Changes check ✅ Passed All changes are directly related to the objectives: AccountInfo refactoring, StakingInfo wrapper implementation, and corresponding test updates. No out-of-scope modifications detected.

✏️ 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: #1366

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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
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: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unit/account_info_test.py (1)

179-196: 🧹 Nitpick | 🔵 Trivial

Consider adding explicit assertion for absent staking_info.

The test_to_proto_with_none_values test doesn't assert that staking_info is absent when AccountInfo has no staking_info set. Adding this would strengthen the test.

💡 Suggested addition
     assert proto.ownedNfts == 0  # 0 is default for protobuf int
+    assert not proto.HasField('staking_info')  # staking_info should not be set when None

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: f3fd4e52-2887-4c9a-b73b-39f48c243132

📥 Commits

Reviewing files that changed from the base of the PR and between f620a85 and 4506b44.

📒 Files selected for processing (5)
  • src/hiero_sdk_python/account/account_info.py
  • src/hiero_sdk_python/account/staking_info.py
  • tests/integration/account_create_transaction_e2e_test.py
  • tests/integration/account_update_transaction_e2e_test.py
  • tests/unit/account_info_test.py

@Mounil2005 Mounil2005 marked this pull request as draft March 6, 2026 14:32
Copy link
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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d7864f4b-b3b7-4b86-8184-2b53843b07e5

📥 Commits

Reviewing files that changed from the base of the PR and between 4506b44 and f00e8ea.

📒 Files selected for processing (1)
  • CHANGELOG.md

@exploreriii
Copy link
Contributor

Note @Mounil2005 watch out for any breaking changes please, and if you must, ensure there is backwards compatibility

@Mounil2005
Copy link
Contributor Author

Note @Mounil2005 watch out for any breaking changes please, and if you must, ensure there is backwards compatibility

Yes ma'am! Still working on it!

@Mounil2005 Mounil2005 marked this pull request as ready for review March 6, 2026 18:04
Copy link
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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
tests/unit/account_info_test.py (1)

179-196: 🧹 Nitpick | 🔵 Trivial

Consider adding assertion for staking_info absence.

This test verifies _to_proto behavior with None values but doesn't assert that proto.staking_info is unset when AccountInfo.staking_info is None.

Suggested addition
     assert proto.ownedNfts == 0  # 0 is default for protobuf int
+    # staking_info should not be set when AccountInfo.staking_info is None
+    assert not proto.HasField('staking_info')

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e85bcc4c-4b03-4a31-9c12-c29fe698439f

📥 Commits

Reviewing files that changed from the base of the PR and between f00e8ea and 8acc5cd.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • src/hiero_sdk_python/account/account_info.py
  • src/hiero_sdk_python/account/staking_info.py
  • tests/unit/account_info_test.py

Copy link
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.

@Mounil2005
have you seen src/hiero_sdk_python/staking_info.py?
They are both to do with account staking information, i assume you'll be able to use that one - perhaps you can add sections to it if you see anything missing in there.
We do have some content duplicted in the source code as we have missed possible instances like this :D

@exploreriii exploreriii added the status: needs developer revision PR has requested changes that the developer needs to implement label Mar 6, 2026
@exploreriii exploreriii marked this pull request as draft March 6, 2026 22:01
@Mounil2005
Copy link
Contributor Author

@Mounil2005 have you seen src/hiero_sdk_python/staking_info.py? They are both to do with account staking information, i assume you'll be able to use that one - perhaps you can add sections to it if you see anything missing in there. We do have some content duplicted in the source code as we have missed possible instances like this :D

Noted, looking into it.

@Mounil2005 Mounil2005 force-pushed the chore/Refactor-AccountInfo-class-to-use-the-staking_info branch from 56c9719 to 1094ae5 Compare March 7, 2026 21:28
@exploreriii exploreriii marked this pull request as ready for review March 7, 2026 23:31
@exploreriii exploreriii added status: needs triage review PR needs a review from the triage team status: needs committer review PR needs a review from the committer team help needed: step security team and removed status: needs developer revision PR has requested changes that the developer needs to implement labels Mar 7, 2026
Copy link
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: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 95d48dea-b1e4-460d-a0b0-f3d995f21eec

📥 Commits

Reviewing files that changed from the base of the PR and between 8acc5cd and 1094ae5.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • src/hiero_sdk_python/account/account_info.py
  • tests/integration/account_create_transaction_e2e_test.py
  • tests/integration/account_update_transaction_e2e_test.py
  • tests/unit/account_info_test.py

@Mounil2005 Mounil2005 force-pushed the chore/Refactor-AccountInfo-class-to-use-the-staking_info branch from 1094ae5 to d3321db Compare March 8, 2026 21:20
Copy link
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: 1

♻️ Duplicate comments (2)
src/hiero_sdk_python/account/account_info.py (2)

8-8: 🧹 Nitpick | 🔵 Trivial

Use X | None union syntax instead of Optional[X].

The new code at line 58 uses Optional[StakingInfo] which is inconsistent with the codebase style that prefers X | None. While the existing fields in this file also use Optional[X], new code should follow the modern style.

As per coding guidelines: "The codebase uses X | None union syntax (Python 3.10+). Flag any use of Optional[X] from typing in newly added or modified code."


191-222: ⚠️ Potential issue | 🟡 Minor

Deprecation shims do not support legacy constructor kwargs.

The deprecated properties provide backward-compatible attribute access (info.staked_account_id), but constructor usage like AccountInfo(staked_account_id=...) will raise TypeError because these are no longer dataclass fields.

If full backward compatibility is required, consider adding a custom __init__ that accepts legacy kwargs and translates them to staking_info. Otherwise, document this constructor breaking change explicitly.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8b133902-282b-4e05-a242-7750c4f39010

📥 Commits

Reviewing files that changed from the base of the PR and between 1094ae5 and d3321db.

📒 Files selected for processing (5)
  • CHANGELOG.md
  • src/hiero_sdk_python/account/account_info.py
  • tests/integration/account_create_transaction_e2e_test.py
  • tests/integration/account_update_transaction_e2e_test.py
  • tests/unit/account_info_test.py

@Mounil2005 Mounil2005 force-pushed the chore/Refactor-AccountInfo-class-to-use-the-staking_info branch from 9d14e9d to 6e988b3 Compare March 9, 2026 10:49
@Mounil2005 Mounil2005 force-pushed the chore/Refactor-AccountInfo-class-to-use-the-staking_info branch from f66aa3d to 9bf4e81 Compare March 9, 2026 17:16
Copy link
Contributor

@Dosik13 Dosik13 left a comment

Choose a reason for hiding this comment

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

good work, left some comments

Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
…on AccountInfo

Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
…mpat

Signed-off-by: Mounil <mounilkankhara@gmail.com>
…roperties

Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
Add @property.setter implementations for staked_account_id, staked_node_id, and decline_staking_reward. This ensures full backwards compatibility for code that both reads AND writes these deprecated fields.

When setting a field, if the oneof constraint would be violated (both staked_account_id and staked_node_id set), the conflicting field is cleared automatically.

Add 4 regression tests for the setter behavior.

Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
Signed-off-by: Mounil <mounilkankhara@gmail.com>
@Mounil2005 Mounil2005 force-pushed the chore/Refactor-AccountInfo-class-to-use-the-staking_info branch from 5dd9449 to 0729769 Compare March 10, 2026 19:22
@github-actions
Copy link

Hi, this is WorkflowBot.
Your pull request cannot be merged as it is not passing all our workflow checks.
Please click on each check to review the logs and resolve issues so all checks pass.
To help you:

Comment on lines +270 to +362

# ---------------------------------------------------------------------------
# StakingInfo (staking_info) unit tests
# ---------------------------------------------------------------------------

class TestStakingInfo:
"""Unit tests for the staking_info.StakingInfo wrapper."""

def test_from_proto_none_raises(self):
"""_from_proto(None) should raise ValueError."""
with pytest.raises(ValueError, match="Staking info proto is None"):
StakingInfo._from_proto(None)

def test_from_proto_staked_node_id(self):
"""_from_proto should populate staked_node_id from the oneof field."""
from hiero_sdk_python.hapi.services.basic_types_pb2 import StakingInfo as StakingInfoProto
proto = StakingInfoProto(decline_reward=False, pending_reward=0, staked_to_me=0)
proto.staked_node_id = 5
si = StakingInfo._from_proto(proto)
assert si.staked_node_id == 5
assert si.staked_account_id is None

def test_from_proto_staked_node_id_zero(self):
"""staked_node_id=0 should be preserved (valid node ID)."""
from hiero_sdk_python.hapi.services.basic_types_pb2 import StakingInfo as StakingInfoProto
proto = StakingInfoProto(decline_reward=False)
proto.staked_node_id = 0 # 0 is a valid node ID
si = StakingInfo._from_proto(proto)
assert si.staked_node_id == 0
assert si.staked_account_id is None

def test_to_proto_staked_node_id(self):
"""_to_proto should set staked_node_id on the proto."""
si = StakingInfo(staked_node_id=7, decline_reward=False)
proto = si._to_proto()
assert proto.staked_node_id == 7
assert not proto.HasField("staked_account_id")

def test_to_proto_oneof_raises(self):
"""StakingInfo construction should raise ValueError if both staked fields are set."""
with pytest.raises(ValueError, match="Only one of"):
StakingInfo(
staked_account_id=AccountId(0, 0, 1),
staked_node_id=2,
)

def test_to_proto_all_fields(self):
"""_to_proto should serialize all optional fields correctly."""
si = StakingInfo(
pending_reward=Hbar.from_tinybars(100),
staked_to_me=Hbar.from_tinybars(200),
stake_period_start=Timestamp(1625097600, 0),
staked_account_id=AccountId(0, 0, 50),
decline_reward=True,
)
proto = si._to_proto()
assert proto.pending_reward == 100
assert proto.staked_to_me == 200
assert proto.decline_reward is True
assert proto.HasField("stake_period_start")
assert proto.stake_period_start.seconds == 1625097600
assert proto.HasField("staked_account_id")

def test_to_proto_none_fields(self):
"""_to_proto with all-None fields should return empty proto."""
si = StakingInfo()
proto = si._to_proto()
assert proto.pending_reward == 0
assert proto.staked_to_me == 0
assert proto.decline_reward is False

def test_str_contains_set_fields(self):
"""__str__ should include staked fields."""
si = StakingInfo(staked_node_id=3, decline_reward=True)
s = str(si)
assert "staked_node_id=3" in s
assert "decline_reward=True" in s

def test_str_with_account_id(self):
"""__str__ should include staked_account_id when set."""
si = StakingInfo(staked_account_id=AccountId(0, 0, 99))
s = str(si)
assert "staked_account_id=" in s

def test_repr(self):
"""__repr__ should include all field names."""
si = StakingInfo(staked_node_id=1)
r = repr(si)
assert r.startswith("StakingInfo(")
assert "staked_node_id=1" in r
assert "pending_reward=" in r
assert "decline_reward=" in r

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this tests shouldn't be in this PR as they belong to the staking_info_test.py. Also, I believe most of the tests are in the staking info test file but if you want you can add more in future PR

@github-actions
Copy link

Hello, this is the OfficeHourBot.

This is a reminder that the Hiero Python SDK Office Hours are scheduled in approximately 4 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: needs committer review PR needs a review from the committer team status: needs triage review PR needs a review from the triage team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor AccountInfo class to use the staking_info

4 participants