Skip to content

Commit eae34cb

Browse files
committed
chore: added type var to chunked transaction
Signed-off-by: Manish Dait <daitmanish88@gmail.com>
1 parent 4c5ec1d commit eae34cb

3 files changed

Lines changed: 22 additions & 16 deletions

File tree

src/hiero_sdk_python/transaction/chunked_transaction.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
from abc import ABC, abstractmethod
4-
from typing import Literal, overload
4+
from typing import Literal, TypeVar, overload
55

66
from hiero_sdk_python.client.client import Client
77
from hiero_sdk_python.transaction.transaction import Transaction
@@ -10,6 +10,9 @@
1010
from hiero_sdk_python.transaction.transaction_response import TransactionResponse
1111

1212

13+
T = TypeVar("T", bound="ChunkedTransaction")
14+
15+
1316
class ChunkedTransaction(Transaction, ABC):
1417
"""
1518
Abstract base class for transactions that support chunking.
@@ -36,7 +39,7 @@ def __init__(self) -> None:
3639
self.max_chunks: int = 20
3740

3841
@abstractmethod
39-
def _build_proto_body(self):
42+
def _build_proto_body(self: T):
4043
"""
4144
Builds the protobuf body for the current chunk.
4245
@@ -52,7 +55,7 @@ def _build_proto_body(self):
5255
"""
5356
pass
5457

55-
def set_chunk_size(self, chunk_size: int) -> ChunkedTransaction:
58+
def set_chunk_size(self: T, chunk_size: int) -> T:
5659
"""
5760
Sets the chunk size for this transaction.
5861
@@ -73,7 +76,7 @@ def set_chunk_size(self, chunk_size: int) -> ChunkedTransaction:
7376
self._total_chunks = self.get_required_chunks()
7477
return self
7578

76-
def set_max_chunks(self, max_chunks: int) -> ChunkedTransaction:
79+
def set_max_chunks(self: T, max_chunks: int) -> T:
7780
"""
7881
Sets the maximum number of chunks allowed.
7982
@@ -93,7 +96,7 @@ def set_max_chunks(self, max_chunks: int) -> ChunkedTransaction:
9396
self.max_chunks = max_chunks
9497
return self
9598

96-
def _validate_chunking(self) -> None:
99+
def _validate_chunking(self: T) -> None:
97100
"""
98101
Validates that the transaction doesn't exceed the maximum number of chunks.
99102
@@ -106,7 +109,7 @@ def _validate_chunking(self) -> None:
106109
f"Required: {self.get_required_chunks()} Increase limit with set_max_chunks()."
107110
)
108111

109-
def freeze_with(self, client: Client) -> ChunkedTransaction:
112+
def freeze_with(self: T, client: Client) -> T:
110113
"""
111114
Freezes the transaction by building transaction bodies for all chunks.
112115
@@ -149,14 +152,14 @@ def freeze_with(self, client: Client) -> ChunkedTransaction:
149152

150153
self._transaction_body_bytes[self._transaction_ids[chunk]] = node_bytes
151154

152-
self._current_chunk_index = 0
155+
self._current_chunk_index = None
153156
self._current_transaction_id_index = 0
154157

155158
return super().freeze_with(client)
156159

157160
@overload
158161
def execute(
159-
self,
162+
self: T,
160163
client: Client,
161164
timeout: int | float | None = None,
162165
wait_for_receipt: Literal[True] = True,
@@ -165,15 +168,15 @@ def execute(
165168

166169
@overload
167170
def execute(
168-
self,
171+
self: T,
169172
client: Client,
170173
timeout: int | float | None = None,
171174
wait_for_receipt: Literal[False] = False,
172175
validate_status: bool = False,
173176
) -> TransactionResponse: ...
174177

175178
def execute(
176-
self,
179+
self: T,
177180
client: Client,
178181
timeout: int | float | None = None,
179182
wait_for_receipt: bool = True,
@@ -200,7 +203,7 @@ def execute(
200203

201204
@overload
202205
def execute_all(
203-
self,
206+
self: T,
204207
client: Client,
205208
timeout: int | float | None = None,
206209
wait_for_receipt: Literal[True] = True,
@@ -209,15 +212,15 @@ def execute_all(
209212

210213
@overload
211214
def execute_all(
212-
self,
215+
self: T,
213216
client: Client,
214217
timeout: int | float | None = None,
215218
wait_for_receipt: Literal[False] = False,
216219
validate_status: bool = False,
217220
) -> list[TransactionResponse]: ...
218221

219222
def execute_all(
220-
self,
223+
self: T,
221224
client: Client,
222225
timeout: int | float | None = None,
223226
wait_for_receipt: bool = True,
@@ -258,7 +261,7 @@ def execute_all(
258261
return responses
259262

260263
@property
261-
def body_size_all_chunks(self) -> list[int]:
264+
def body_size_all_chunks(self: T) -> list[int]:
262265
"""
263266
Returns an array of body sizes for each chunk in the transaction.
264267
@@ -273,6 +276,7 @@ def body_size_all_chunks(self) -> list[int]:
273276
self._require_frozen()
274277
sizes = []
275278

279+
original_chunk_index = self._current_chunk_index
276280
original_transaction_index = self._current_transaction_id_index
277281

278282
try:
@@ -281,6 +285,7 @@ def body_size_all_chunks(self) -> list[int]:
281285
self._current_chunk_index = i
282286
sizes.append(self.body_size)
283287
finally:
288+
self._current_chunk_index = original_chunk_index
284289
self._current_transaction_id_index = original_transaction_index
285290

286291
return sizes

tests/unit/chunked_transaction_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ def test_execute_all_multi_chunk_replays_each_chunk(mock_client, private_key):
148148

149149
assert responses == ["chunk-1", "chunk-2", "chunk-3"]
150150
assert mock_execute.call_count == 3
151-
assert tx._current_chunk_index == 2
151+
# ensure reset to none once transaction_body_bytes created
152+
assert tx._current_chunk_index is None
152153

153154

154155
def test_validate_chunking_allows_required_equal_to_max_chunks():

tests/unit/transaction_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ def test_chunked_tx_return_proper_sizes(file_id, account_id, transaction_id):
410410
assert large_size > 1024
411411
# The larger chunked transaction should be bigger than the single-chunk transaction
412412
assert large_size > small_size
413-
assert large_tx._current_chunk_index == 0
413+
assert large_tx._current_chunk_index is None
414414

415415

416416
def test_chunked_tx_differ_size_if_chunk_are_not_equal(topic_id, account_id, transaction_id):

0 commit comments

Comments
 (0)