Skip to content

refactor: update example client setup to use Client.from_env()#2093

Merged
exploreriii merged 22 commits into
hiero-ledger:mainfrom
ParasSalonia:fix/examples-client-setup
May 4, 2026
Merged

refactor: update example client setup to use Client.from_env()#2093
exploreriii merged 22 commits into
hiero-ledger:mainfrom
ParasSalonia:fix/examples-client-setup

Conversation

@ParasSalonia

Copy link
Copy Markdown
Contributor

Changes

  • Refactored example files to replace manual client setup with the simplified Client.from_env() method.

  • Replaced manual client initialization (Client(network) + set_operator) with Client.from_env()

  • Removed redundant environment variable parsing and operator setup logic

  • Standardized client setup across example scripts for consistency and readability

Linked issue

@codacy-production

codacy-production Bot commented Apr 9, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@ParasSalonia
ParasSalonia marked this pull request as ready for review April 9, 2026 14:33
@ParasSalonia
ParasSalonia requested review from a team as code owners April 9, 2026 14:33
Comment thread examples/transaction/custom_fee_limit.py Outdated
@coderabbitai

coderabbitai Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

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

Refactored setup_client across many example scripts to use Client.from_env(), removed manual Network/OPERATOR parsing and try/except exit wrappers, added explicit return type annotations in many helpers, and updated startup logging to print client.network.network and client.operator_account_id.

Changes

Cohort / File(s) Summary
Account Examples
examples/account/account_allowance_approve_transaction_hbar.py, examples/account/account_allowance_approve_transaction_nft.py, examples/account/account_allowance_delete_transaction_hbar.py, examples/account/account_allowance_delete_transaction_nft.py, examples/account/account_records_query.py, examples/account/account_create_transaction_create_with_alias.py, examples/account/account_create_transaction_evm_alias.py, examples/account/account_create_transaction_with_fallback_alias.py
Replaced manual Network + env parsing + client.set_operator(...) with Client.from_env(); some functions gained explicit return type annotations; removed local env validation/try-exit logic; logging now prints client.network.network and client.operator_account_id.
Contract Examples
examples/contract/.../contract_bytecode_query.py, examples/contract/.../contract_call_query.py, examples/contract/.../contract_create_transaction_no_constructor_parameters.py, examples/contract/.../contract_create_transaction_with_bytecode.py, examples/contract/.../contract_create_transaction_with_constructor_parameters.py, examples/contract/.../contract_execute_transaction.py, examples/contract/.../contract_execute_transaction_with_value.py, examples/contract/.../contract_update_transaction.py, examples/contract/.../ethereum_transaction.py
Switched setup_client() to Client.from_env() and added -> Client annotations where applicable; removed explicit operator parsing and set_operator calls; updated startup prints to use client-resolved network/operator; cleaned up now-unused imports.
File Examples
examples/file/file_append_transaction.py, examples/file/file_contents_query.py, examples/file/file_delete_transaction.py, examples/file/file_info_query.py, examples/file/file_update_transaction.py
Replaced manual network/operator setup with Client.from_env(); added -> Client return annotations; removed env var parsing and legacy "Connecting…" messages in favor of client.network.network.
Schedule Examples
examples/schedule/schedule_create_transaction.py, examples/schedule/schedule_delete_transaction.py, examples/schedule/schedule_info_query.py, examples/schedule/schedule_sign_transaction.py
setup_client() now uses Client.from_env() and -> Client return annotations; dropped manual operator parsing and try/except exit behavior; logging uses client-resolved values.
Consensus Examples
examples/consensus/topic_message_submit_chunked_transaction.py, examples/consensus/topic_message_submit_transaction.py, examples/consensus/topic_update_transaction.py
Replaced manual client/operator setup with Client.from_env(); added return type annotations (Client or tuple where needed); removed custom try/except .env error handling; logging uses client properties.
Transaction Examples
examples/transaction/batch_transaction.py, examples/transaction/custom_fee_limit.py, examples/transaction/transaction_freeze_manually.py, examples/transaction/transaction_freeze_secondary_client.py, examples/transaction/transaction_freeze_without_operator.py, examples/transaction/transaction_to_bytes.py, examples/transaction/transfer_transaction_fungible.py, examples/transaction/transfer_transaction_gigabar.py, examples/transaction/transfer_transaction_hbar.py, examples/transaction/transfer_transaction_nft.py, examples/transaction/transfer_transaction_tinybar.py
Moved to Client.from_env() initialization; several setup_client() now return typed tuples (Client or tuple[Client, AccountId, PrivateKey]); removed manual env parsing, try/except rewraps, and sys.exit calls; updated network/operator logging and removed unused imports.
Other Examples
examples/client/client.py, examples/logger/logging_example.py, examples/prng_transaction.py, examples/tls_query_balance.py
Unified setup to Client.from_env(); added or updated return type annotations; removed per-example manual TLS/verification/network parsing logic (tls example simplified); startup logging standardized to client.network.network and client.operator_account_id.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: refactoring example client setup to use Client.from_env() instead of manual setup.
Description check ✅ Passed The description is related to the changeset, explaining the refactoring from manual client setup to Client.from_env() and mentioning standardization across examples.
Linked Issues check ✅ Passed The PR addresses the core requirements of issue #2055: replacing manual Client(network) + set_operator with Client.from_env() across all example files and removing redundant environment variable parsing.
Out of Scope Changes check ✅ Passed All changes are in-scope: modifications are limited to example files' setup_client() functions to use Client.from_env(), with removal of try/except blocks and redundant operator setup logic.
Docstring Coverage ✅ Passed Docstring coverage is 98.21% which is sufficient. The required threshold is 80.00%.

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

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

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 18

Caution

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

⚠️ Outside diff range comments (1)
examples/account/account_allowance_delete_transaction_nft.py (1)

44-55: ⚠️ Potential issue | 🟠 Major

Make setup_client() fail gracefully before main flow starts.

Because setup_client() is called before the main() try-block, any Client.from_env() failure currently exits via traceback. Handle this inside setup and terminate with sys.exit(1).

Suggested patch
 def setup_client() -> tuple[Client, AccountId, PrivateKey]:
     """Setup Client."""
-    client = Client.from_env()
+    try:
+        client = Client.from_env()
+    except Exception as e:
+        print(f"Failed to set up client: {e}")
+        sys.exit(1)
 
     operator_id = client.operator_account_id
     operator_key = client.operator_private_key

As per coding guidelines, examples should include try-except blocks with sys.exit(1) for critical failures.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 76833f10-638c-4c33-ac8d-5501e69f2c6e

📥 Commits

Reviewing files that changed from the base of the PR and between 7571b15 and bbd1d33.

📒 Files selected for processing (42)
  • CHANGELOG.md
  • examples/account/account_allowance_approve_transaction_hbar.py
  • examples/account/account_allowance_approve_transaction_nft.py
  • examples/account/account_allowance_delete_transaction_hbar.py
  • examples/account/account_allowance_delete_transaction_nft.py
  • examples/account/account_records_query.py
  • examples/client/client.py
  • examples/consensus/topic_message_submit_chunked_transaction.py
  • examples/consensus/topic_message_submit_transaction.py
  • examples/consensus/topic_update_transaction.py
  • examples/contract/contract_bytecode_query.py
  • examples/contract/contract_call_query.py
  • examples/contract/contract_create_transaction_no_constructor_parameters.py
  • examples/contract/contract_create_transaction_with_bytecode.py
  • examples/contract/contract_create_transaction_with_constructor_parameters.py
  • examples/contract/contract_execute_transaction.py
  • examples/contract/contract_execute_transaction_with_value.py
  • examples/contract/contract_update_transaction.py
  • examples/contract/ethereum_transaction.py
  • examples/file/file_append_transaction.py
  • examples/file/file_contents_query.py
  • examples/file/file_delete_transaction.py
  • examples/file/file_info_query.py
  • examples/file/file_update_transaction.py
  • examples/logger/logging_example.py
  • examples/prng_transaction.py
  • examples/schedule/schedule_create_transaction.py
  • examples/schedule/schedule_delete_transaction.py
  • examples/schedule/schedule_info_query.py
  • examples/schedule/schedule_sign_transaction.py
  • examples/tls_query_balance.py
  • examples/transaction/batch_transaction.py
  • examples/transaction/custom_fee_limit.py
  • examples/transaction/transaction_freeze_manually.py
  • examples/transaction/transaction_freeze_secondary_client.py
  • examples/transaction/transaction_freeze_without_operator.py
  • examples/transaction/transaction_to_bytes.py
  • examples/transaction/transfer_transaction_fungible.py
  • examples/transaction/transfer_transaction_gigabar.py
  • examples/transaction/transfer_transaction_hbar.py
  • examples/transaction/transfer_transaction_nft.py
  • examples/transaction/transfer_transaction_tinybar.py

Comment thread CHANGELOG.md Outdated
Comment thread examples/account/account_allowance_approve_transaction_nft.py
Comment thread examples/account/account_records_query.py
Comment thread examples/client/client.py
Comment thread examples/contract/contract_create_transaction_with_bytecode.py
Comment thread examples/schedule/schedule_info_query.py
Comment thread examples/schedule/schedule_sign_transaction.py
Comment thread examples/tls_query_balance.py
Comment thread examples/transaction/custom_fee_limit.py Outdated
Comment thread examples/transaction/transfer_transaction_nft.py

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

HI @ParasSalonia thanks for your contribution! :D
have you tried to run the examples locally? do they works fine with the new client setup?

@ParasSalonia
ParasSalonia marked this pull request as draft April 9, 2026 19:11
@ParasSalonia

Copy link
Copy Markdown
Contributor Author

Hi @manishdait, @AntonioCeppellini,

I wanted to clarify a couple of things regarding this issue.

  • Based on the discussion, should we remove the try/except block in all setup_client() implementations to keep things consistent, since Client.from_env() already provides descriptive errors?
  • I also tried to test the example scripts locally using uv, but I’m currently unable to run them due to a missing module (hiero_sdk_python.hapi). It seems like some generated files or setup steps may be required.

@exploreriii

Copy link
Copy Markdown
Contributor

@exploreriii

Copy link
Copy Markdown
Contributor

Hi @ParasSalonia please also rebase and delete your changelog entry, we are not using that anymore

@exploreriii

Copy link
Copy Markdown
Contributor

Signed-off-by: ParasSalonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
Added error handling for client setup to check environment variables.

Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
@exploreriii exploreriii added reviewer: committer request review help from a committer reviewer: junior committer request review from junior committers with triage permissions step: 1st 1st stage of the review approval process labels Apr 28, 2026

@exploreriii exploreriii left a comment

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.

Hi @ParasSalonia please triple check the example run, check all is as expected and note the failed run
https://github.com/hiero-ledger/hiero-sdk-python/actions/runs/25064955743/job/73436923567?pr=2093

@exploreriii exploreriii removed step: 1st 1st stage of the review approval process reviewer: junior committer request review from junior committers with triage permissions reviewer: committer request review help from a committer labels Apr 28, 2026
@exploreriii
exploreriii marked this pull request as draft April 28, 2026 17:42
Signed-off-by: ParasSalonia <parassalonia22@gmail.com>
@ParasSalonia
ParasSalonia force-pushed the fix/examples-client-setup branch from 63b114c to 4d3676a Compare April 28, 2026 18:02
@ParasSalonia
ParasSalonia marked this pull request as ready for review May 1, 2026 04:55
@exploreriii exploreriii added step: 1st 1st stage of the review approval process reviewer: community pull requests looking for community reviews labels May 1, 2026

@exploreriii exploreriii left a comment

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.

@manishdait what do you think about this?
the client does not specify the network if blank, but network defaults to testnet.
should we make a change later in another PR to be more explicit?

@exploreriii exploreriii added step: 2nd second stage of the review approval process reviewer: write requires a review with write permissions and removed step: 1st 1st stage of the review approval process reviewer: community pull requests looking for community reviews labels May 1, 2026
@exploreriii

Copy link
Copy Markdown
Contributor

Please click update branch @ParasSalonia

@codacy-production

codacy-production Bot commented May 3, 2026

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@exploreriii
exploreriii merged commit c9c2d27 into hiero-ledger:main May 4, 2026
13 checks passed
NssGourav pushed a commit to NssGourav/hiero-sdk-python that referenced this pull request May 14, 2026
…-ledger#2093)

Signed-off-by: ParasSalonia <parassalonia22@gmail.com>
Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
@exploreriii exploreriii added this to the v0.2.7 milestone May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

reviewer: write requires a review with write permissions skill: beginner Achievable by a fairly new comer that has already completed a couple of good first issues step: 2nd second stage of the review approval process

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Beginner]: Tidy set up client in examples

4 participants