Skip to content

Commit f5435c0

Browse files
authored
fix: Improve type hinting in file_append_transaction.py (#633)
Signed-off-by: Adityarya11 <arya050411@gmail.com>
1 parent cc79fd3 commit f5435c0

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
2020
### Fixed
2121

2222
- Added explicit read permissions to examples.yml (#623)
23+
- Improved type hinting in `file_append_transaction.py` to resolve 'mypy --strict` errors. ([#495](https://github.com/hiero-ledger/hiero-sdk-python/issues/495))
2324

2425
### Breaking Changes
2526

src/hiero_sdk_python/file/file_append_transaction.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
"""
24
Represents a file append transaction on the network.
35
@@ -12,18 +14,25 @@
1214
"""
1315

1416
import math
15-
from typing import Optional
17+
from typing import TYPE_CHECKING, Any, List, Optional
1618
from hiero_sdk_python.file.file_id import FileId
1719
from hiero_sdk_python.hbar import Hbar
1820
from hiero_sdk_python.transaction.transaction import Transaction
19-
from hiero_sdk_python.channels import _Channel
20-
from hiero_sdk_python.executable import _Method
2121
from hiero_sdk_python.transaction.transaction_id import TransactionId
2222
from hiero_sdk_python.hapi.services import file_append_pb2, timestamp_pb2
2323
from hiero_sdk_python.hapi.services.schedulable_transaction_body_pb2 import (
2424
SchedulableTransactionBody,
2525
)
2626

27+
# Use TYPE_CHECKING to avoid circular import errors
28+
if TYPE_CHECKING:
29+
from hiero_sdk_python.client import Client
30+
from hiero_sdk_python.keys import PrivateKey
31+
from hiero_sdk_python.channels import _Channel
32+
from hiero_sdk_python.executable import _Method
33+
from hiero_sdk_python.transaction_receipt import TransactionReceipt
34+
35+
2736
# pylint: disable=too-many-instance-attributes
2837
class FileAppendTransaction(Transaction):
2938
"""
@@ -60,8 +69,8 @@ def __init__(self, file_id: Optional[FileId] = None, contents: Optional[str | by
6069
# Internal tracking for chunking
6170
self._current_chunk_index: int = 0
6271
self._total_chunks: int = self._calculate_total_chunks()
63-
self._transaction_ids: list[TransactionId] = []
64-
self._signing_keys: list = [] # Store all signing keys for multi-chunk transactions
72+
self._transaction_ids: List[TransactionId] = []
73+
self._signing_keys: List["PrivateKey"] = [] # Use string annotation to avoid import issues
6574

6675
def _encode_contents(self, contents: Optional[str | bytes]) -> Optional[bytes]:
6776
"""
@@ -99,7 +108,7 @@ def get_required_chunks(self) -> int:
99108
"""
100109
return self._calculate_total_chunks()
101110

102-
def set_file_id(self, file_id: FileId) -> 'FileAppendTransaction':
111+
def set_file_id(self, file_id: FileId) -> FileAppendTransaction:
103112
"""
104113
Sets the file ID for this file append transaction.
105114
@@ -113,7 +122,7 @@ def set_file_id(self, file_id: FileId) -> 'FileAppendTransaction':
113122
self.file_id = file_id
114123
return self
115124

116-
def set_contents(self, contents: Optional[str | bytes]) -> 'FileAppendTransaction':
125+
def set_contents(self, contents: Optional[str | bytes]) -> FileAppendTransaction:
117126
"""
118127
Sets the contents for this file append transaction.
119128
@@ -129,7 +138,7 @@ def set_contents(self, contents: Optional[str | bytes]) -> 'FileAppendTransactio
129138
self._total_chunks = self._calculate_total_chunks()
130139
return self
131140

132-
def set_max_chunks(self, max_chunks: int) -> 'FileAppendTransaction':
141+
def set_max_chunks(self, max_chunks: int) -> FileAppendTransaction:
133142
"""
134143
Sets the maximum number of chunks allowed for this transaction.
135144
@@ -143,7 +152,7 @@ def set_max_chunks(self, max_chunks: int) -> 'FileAppendTransaction':
143152
self.max_chunks = max_chunks
144153
return self
145154

146-
def set_chunk_size(self, chunk_size: int) -> 'FileAppendTransaction':
155+
def set_chunk_size(self, chunk_size: int) -> FileAppendTransaction:
147156
"""
148157
Sets the chunk size for this transaction.
149158
@@ -158,7 +167,7 @@ def set_chunk_size(self, chunk_size: int) -> 'FileAppendTransaction':
158167
self._total_chunks = self._calculate_total_chunks()
159168
return self
160169

161-
def _build_proto_body(self):
170+
def _build_proto_body(self) -> file_append_pb2.FileAppendTransactionBody:
162171
"""
163172
Returns the protobuf body for the file append transaction.
164173
@@ -184,7 +193,7 @@ def _build_proto_body(self):
184193
contents=chunk_contents
185194
)
186195

187-
def build_transaction_body(self):
196+
def build_transaction_body(self) -> Any:
188197
"""
189198
Builds the transaction body for this file append transaction.
190199
@@ -208,7 +217,7 @@ def build_scheduled_body(self) -> SchedulableTransactionBody:
208217
schedulable_body.fileAppend.CopyFrom(file_append_body)
209218
return schedulable_body
210219

211-
def _get_method(self, channel: _Channel) -> _Method:
220+
def _get_method(self, channel: "_Channel") -> "_Method":
212221
"""
213222
Gets the method to execute the file append transaction.
214223
@@ -221,12 +230,13 @@ def _get_method(self, channel: _Channel) -> _Method:
221230
Returns:
222231
_Method: An object containing the transaction function to append to a file.
223232
"""
233+
from hiero_sdk_python.executable import _Method
224234
return _Method(
225235
transaction_func=channel.file.appendContent,
226236
query_func=None
227237
)
228238

229-
def _from_proto(self, proto) -> 'FileAppendTransaction':
239+
def _from_proto(self, proto: file_append_pb2.FileAppendTransactionBody) -> FileAppendTransaction:
230240
"""
231241
Initializes a new FileAppendTransaction instance from a protobuf object.
232242
@@ -242,7 +252,7 @@ def _from_proto(self, proto) -> 'FileAppendTransaction':
242252
self._total_chunks = self._calculate_total_chunks()
243253
return self
244254

245-
def _validate_chunking(self):
255+
def _validate_chunking(self) -> None:
246256
"""
247257
Validates that the transaction doesn't exceed the maximum number of chunks.
248258
@@ -256,7 +266,7 @@ def _validate_chunking(self):
256266
)
257267

258268

259-
def freeze_with(self, client):
269+
def freeze_with(self, client: "Client") -> FileAppendTransaction:
260270
"""
261271
Freezes the transaction by building the transaction body and setting necessary IDs.
262272
@@ -272,6 +282,7 @@ def freeze_with(self, client):
272282
if self._transaction_body_bytes:
273283
return self
274284

285+
275286
if self.transaction_id is None:
276287
self.transaction_id = client.generate_transaction_id()
277288

@@ -310,7 +321,7 @@ def freeze_with(self, client):
310321
return self
311322

312323

313-
def execute(self, client):
324+
def execute(self, client: "Client") -> Any:
314325
"""
315326
Executes the file append transaction.
316327
@@ -349,15 +360,14 @@ def execute(self, client):
349360
# Call parent sign directly to avoid modifying _signing_keys
350361
super().sign(signing_key)
351362

352-
353363
# Execute the chunk
354364
response = super().execute(client)
355365
responses.append(response)
356366

357367
# Return the first response (as per JavaScript implementation)
358368
return responses[0] if responses else None
359369

360-
def sign(self, private_key):
370+
def sign(self, private_key: "PrivateKey") -> FileAppendTransaction:
361371
"""
362372
Signs the transaction using the provided private key.
363373
@@ -374,4 +384,5 @@ def sign(self, private_key):
374384
self._signing_keys.append(private_key)
375385

376386
# Call the parent sign method for the current transaction
377-
return super().sign(private_key)
387+
super().sign(private_key)
388+
return self

0 commit comments

Comments
 (0)