Skip to content

Commit 8640023

Browse files
rbarker-dev0xivanov
andcommitted
chore: Add Typehinting to tokens module
Signed-off-by: Roger Barker <[email protected]> additional changes to use "Any" and to update additional tokens module files Signed-off-by: Roger Barker <[email protected]> More updates Signed-off-by: Roger Barker <[email protected]> Through token_mint_transactions Signed-off-by: Roger Barker <[email protected]> finished tokens module Signed-off-by: Roger Barker <[email protected]> Finished transaction model Signed-off-by: Roger Barker <[email protected]> chore: Typing README (#151) * chore: starter mypy loose config Signed-off-by: exploreriii <[email protected]> * chore: rename of main README Signed-off-by: exploreriii <[email protected]> * chore: What are Types? Signed-off-by: exploreriii <[email protected]> * chore: What are Type hints?-rest Signed-off-by: exploreriii <[email protected]> * chore: fixing some small types and urls Signed-off-by: exploreriii <[email protected]> * chore: basic contents for typing Signed-off-by: exploreriii <[email protected]> * chore: markdown references for type hinting Signed-off-by: exploreriii <[email protected]> * revert: README core naming Signed-off-by: exploreriii <[email protected]> --------- Signed-off-by: exploreriii <[email protected]> fix: example scripts and README update (#154) * fixed example scripts and readme Signed-off-by: nadinedelia <[email protected]> * added commas Signed-off-by: nadinedelia <[email protected]> --------- Signed-off-by: nadinedelia <[email protected]> feat: add `TransactionRecordQuery` (#144) * feat: add transaction_id field to TransactionReceipt Signed-off-by: dosi <[email protected]> * refactor: store transaction_id in TransactionReceipt Signed-off-by: dosi <[email protected]> * feat: add _from_proto() to TokenNftTransfer Signed-off-by: dosi <[email protected]> * test: add unit test for TokenNftTransfer _from_proto() Signed-off-by: dosi <[email protected]> * test: add unit tests for TransactionRecord Signed-off-by: dosi <[email protected]> * feat: implement TransactionRecord class Signed-off-by: dosi <[email protected]> * sqash s transaction record Signed-off-by: dosi <[email protected]> * feat: implement TransactionRecordQuery Signed-off-by: dosi <[email protected]> * test: implement integration tests for TransactionRecordQuery Signed-off-by: dosi <[email protected]> * test: add unit tests Signed-off-by: dosi <[email protected]> * docs: add query record example Signed-off-by: dosi <[email protected]> * chore: add TransactionRecord and TransactionRecordQuery to __init__.py Signed-off-by: dosi <[email protected]> * docs: add transaction record query to README Signed-off-by: dosi <[email protected]> * test: reduce test_transaction_record_query_execute() lines in TransactionRecordQuery unit tests Signed-off-by: dosi <[email protected]> * feat: add __repr__ method Signed-off-by: Ivan Ivanov <[email protected]> --------- Signed-off-by: dosi <[email protected]> Signed-off-by: Ivan Ivanov <[email protected]> Co-authored-by: Ivan Ivanov <[email protected]>
1 parent 079e7e8 commit 8640023

34 files changed

+727
-403
lines changed

mypy.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# Here it is kept quite loose as type hinting is introduced
33

44
[mypy]
5-
65
# Do not error on imports that lack type stubs
76
ignore_missing_imports = True
87

@@ -19,4 +18,4 @@ allow_untyped_defs = True
1918
follow_imports = silent
2019

2120
# Turn off strict None‐checking—allows assigning X | None to X without error
22-
strict_optional = False
21+
strict_optional = False

src/hiero_sdk_python/client/network.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(
6666
network: str = 'testnet',
6767
nodes: list[_Node] = None,
6868
mirror_address: str = None,
69-
):
69+
) -> None:
7070
"""
7171
Initializes the Network with the specified network name or custom config.
7272

src/hiero_sdk_python/query/account_balance_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import traceback
12
from typing import Optional, Any
23
from hiero_sdk_python.query.query import Query
34
from hiero_sdk_python.hapi.services import crypto_get_account_balance_pb2, query_pb2
45
from hiero_sdk_python.account.account_id import AccountId
56
from hiero_sdk_python.account.account_balance import AccountBalance
67
from hiero_sdk_python.executable import _Method
78
from hiero_sdk_python.channels import _Channel
8-
import traceback
99

1010
class CryptoGetAccountBalanceQuery(Query):
1111
"""

src/hiero_sdk_python/query/query.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
import time
66
from typing import Any, List, Optional, Union
77

8+
from typing import Any, List, Optional
9+
10+
from hiero_sdk_python.exceptions import PrecheckError
11+
from hiero_sdk_python.executable import _Method
12+
from hiero_sdk_python.channels import _Channel
13+
from hiero_sdk_python.hapi.services import query_header_pb2, query_pb2
14+
from hiero_sdk_python.response_code import ResponseCode
15+
from hiero_sdk_python.hbar import Hbar
16+
from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction
17+
from hiero_sdk_python.transaction.transaction_id import TransactionId
18+
from hiero_sdk_python.executable import _Executable, _ExecutionState
819
from hiero_sdk_python.account.account_id import AccountId
920
from hiero_sdk_python.channels import _Channel
1021
from hiero_sdk_python.client.client import Client, Operator

src/hiero_sdk_python/tokens/nft_id.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def __init__(
4646
)
4747
token_id = tokenId
4848

49+
<<<<<<< HEAD
4950
# Map legacy serialNumber -> serial_number
5051
if serial_number is None and serialNumber is not None:
5152
warnings.warn(
@@ -78,6 +79,18 @@ def __post_init__(self) -> None:
7879
raise TypeError(f"serial_number must be an integer, got {type(self.serial_number)}")
7980
if self.serial_number < 0:
8081
raise ValueError("serial_number must be non-negative")
82+
=======
83+
def __post_init__(self) -> None:
84+
"""Validate the NftId after initialization."""
85+
if self.tokenId is None:
86+
raise TypeError("token_id is required")
87+
if not isinstance(self.tokenId, TokenId):
88+
raise TypeError(f"token_id must be of type TokenId, got {type(self.tokenId)}")
89+
if not isinstance(self.serialNumber, int):
90+
raise TypeError(f"Expected an integer for serial_number, got {type(self.serialNumber)}")
91+
if self.serialNumber < 0:
92+
raise ValueError("serial_number must be positive")
93+
>>>>>>> 1c940df (chore: Add Typehinting to tokens module)
8194

8295
@classmethod
8396
def _from_proto(cls, nft_id_proto: Optional[basic_types_pb2.NftID] = None) -> "NftId":
@@ -96,16 +109,21 @@ def _to_proto(self) -> basic_types_pb2.NftID:
96109
"""
97110
:return: a protobuf NftID object representation of this NftId object
98111
"""
112+
<<<<<<< HEAD
99113
nft_id_proto = basic_types_pb2.NftID(
100114
token_ID=self.token_id._to_proto(),
101115
serial_number=self.serial_number,
102116
)
117+
=======
118+
nft_id_proto:basic_types_pb2.NftID = basic_types_pb2.NftID(token_ID=self.tokenId._to_proto(), serial_number=self.serialNumber)
119+
120+
>>>>>>> 1c940df (chore: Add Typehinting to tokens module)
103121
return nft_id_proto
104122

105123
@classmethod
106124
def from_string(cls, nft_id_str: str) -> "NftId":
107125
"""
108-
:param nft_id_str: a string NftId representation
126+
:param nft_id_str (str): a string NftId representation
109127
:return: returns the NftId parsed from the string input
110128
"""
111129
parts = re.split(r"/", nft_id_str)

src/hiero_sdk_python/tokens/token_associate_transaction.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
Provides TokenAssociateTransaction, a subclass of Transaction for associating
66
tokens with accounts on the Hedera network using the Hedera Token Service (HTS) API.
77
"""
8+
from typing import Optional, List
9+
from hiero_sdk_python.account.account_id import AccountId
810
from hiero_sdk_python.channels import _Channel
911
from hiero_sdk_python.executable import _Method
1012
from hiero_sdk_python.hapi.services import token_associate_pb2
13+
from hiero_sdk_python.hapi.services import transaction_body_pb2
14+
from hiero_sdk_python.tokens.token_id import TokenId
1115
from hiero_sdk_python.transaction.transaction import Transaction
1216

1317

@@ -22,7 +26,7 @@ class TokenAssociateTransaction(Transaction):
2226
to build and execute a token association transaction.
2327
"""
2428

25-
def __init__(self, account_id=None, token_ids=None):
29+
def __init__(self, account_id: Optional[AccountId] = None, token_ids:Optional[List[TokenId]] = None) -> None:
2630
"""
2731
Initializes a new TokenAssociateTransaction instance with optional keyword arguments.
2832
@@ -31,24 +35,29 @@ def __init__(self, account_id=None, token_ids=None):
3135
token_ids (list of TokenId, optional): The tokens to associate with the account.
3236
"""
3337
super().__init__()
34-
self.account_id = account_id
35-
self.token_ids = token_ids or []
38+
self.account_id: Optional[AccountId] = account_id
39+
self.token_ids: Optional[List[TokenId]] = token_ids or []
3640

37-
self._default_transaction_fee = 500_000_000
41+
self._default_transaction_fee: int = 500_000_000
3842

39-
def set_account_id(self, account_id):
40-
"""Set the account ID for token association."""
43+
def set_account_id(self, account_id: AccountId) -> "TokenAssociateTransaction":
44+
"""
45+
Sets the account ID for the token association transaction.
46+
Args:
47+
account_id (AccountId): The account ID to associate tokens with.
48+
Returns:
49+
TokenAssociateTransaction: The current instance for method chaining.
50+
"""
4151
self._require_not_frozen()
4252
self.account_id = account_id
4353
return self
4454

45-
def add_token_id(self, token_id):
46-
"""Add a token ID to the association list."""
55+
def add_token_id(self, token_id: TokenId) -> "TokenAssociateTransaction":
4756
self._require_not_frozen()
4857
self.token_ids.append(token_id)
4958
return self
5059

51-
def build_transaction_body(self):
60+
def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
5261
"""
5362
Builds and returns the protobuf transaction body for token association.
5463
@@ -66,7 +75,7 @@ def build_transaction_body(self):
6675
tokens=[token_id._to_proto() for token_id in self.token_ids]
6776
)
6877

69-
transaction_body = self.build_base_transaction_body()
78+
transaction_body: transaction_body_pb2.TransactionBody = self.build_base_transaction_body()
7079
transaction_body.tokenAssociate.CopyFrom(token_associate_body)
7180

7281
return transaction_body

src/hiero_sdk_python/tokens/token_burn_transaction.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
Provides TokenBurnTransaction, a subclass of Transaction for burning fungible and
66
non-fungible tokens on the Hedera network using the Hedera Token Service (HTS) API.
77
"""
8-
from typing import Optional
8+
from typing import List, Optional
99

1010
from hiero_sdk_python.hapi.services.token_burn_pb2 import TokenBurnTransactionBody
11+
from hiero_sdk_python.hapi.services import transaction_body_pb2
1112
from hiero_sdk_python.transaction.transaction import Transaction
1213
from hiero_sdk_python.channels import _Channel
1314
from hiero_sdk_python.executable import _Method
@@ -24,11 +25,11 @@ class TokenBurnTransaction(Transaction):
2425
to build and execute a token burn transaction.
2526
"""
2627
def __init__(
27-
self,
28-
token_id: TokenId | None = None,
29-
amount: Optional[int] = None,
30-
serials: list[int] | None = None,
31-
):
28+
self,
29+
token_id: Optional[TokenId] = None,
30+
amount: Optional[int] = None,
31+
serials: Optional[List[int]] = None
32+
) -> None:
3233
"""
3334
Initializes a new TokenBurnTransaction instance with optional token_id, amount, and serials.
3435
@@ -38,11 +39,11 @@ def __init__(
3839
serials (list[int], optional): The serial numbers of non-fungible tokens to burn.
3940
"""
4041
super().__init__()
41-
self.token_id: TokenId = token_id
42+
self.token_id: Optional[TokenId] = token_id
4243
self.amount: Optional[int] = amount
43-
self.serials: list[int] = serials if serials else []
44-
45-
def set_token_id(self, token_id: TokenId):
44+
self.serials: Optional[List[int]] = serials if serials else []
45+
46+
def set_token_id(self, token_id: TokenId) -> "TokenBurnTransaction":
4647
"""
4748
Sets the token ID for this burn transaction.
4849
@@ -55,8 +56,8 @@ def set_token_id(self, token_id: TokenId):
5556
self._require_not_frozen()
5657
self.token_id = token_id
5758
return self
58-
59-
def set_amount(self, amount: int):
59+
60+
def set_amount(self, amount: int) -> "TokenBurnTransaction":
6061
"""
6162
Sets the amount of fungible tokens to burn.
6263
@@ -69,8 +70,8 @@ def set_amount(self, amount: int):
6970
self._require_not_frozen()
7071
self.amount = amount
7172
return self
72-
73-
def set_serials(self, serials: list[int]):
73+
74+
def set_serials(self, serials: List[int]) -> "TokenBurnTransaction":
7475
"""
7576
Sets the list of serial numbers of non-fungible tokens to burn.
7677
@@ -83,8 +84,8 @@ def set_serials(self, serials: list[int]):
8384
self._require_not_frozen()
8485
self.serials = serials
8586
return self
86-
87-
def add_serial(self, serial: int):
87+
88+
def add_serial(self, serial: int) -> "TokenBurnTransaction":
8889
"""
8990
Adds a single serial number to the list of non-fungible tokens to burn.
9091
@@ -98,8 +99,8 @@ def add_serial(self, serial: int):
9899
self._require_not_frozen()
99100
self.serials.append(serial)
100101
return self
101-
102-
def build_transaction_body(self):
102+
103+
def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
103104
"""
104105
Builds the transaction body for this token burn transaction.
105106
@@ -120,7 +121,7 @@ def build_transaction_body(self):
120121
amount=self.amount,
121122
serialNumbers=self.serials
122123
)
123-
transaction_body = self.build_base_transaction_body()
124+
transaction_body: transaction_body_pb2.TransactionBody = self.build_base_transaction_body()
124125
transaction_body.tokenBurn.CopyFrom(token_burn_body)
125126
return transaction_body
126127

@@ -141,8 +142,8 @@ def _get_method(self, channel: _Channel) -> _Method:
141142
transaction_func=channel.token.burnToken,
142143
query_func=None
143144
)
144-
145-
def _from_proto(self, proto: TokenBurnTransactionBody):
145+
146+
def _from_proto(self, proto: TokenBurnTransactionBody) -> "TokenBurnTransaction":
146147
"""
147148
Deserializes a TokenBurnTransactionBody from a protobuf object.
148149

0 commit comments

Comments
 (0)