Skip to content

Commit 47b94b9

Browse files
Fix mypy type checking errors and add stricter type annotations (#165)
Signed-off-by: Anirudh Sengar <[email protected]>
1 parent ece454b commit 47b94b9

File tree

9 files changed

+38
-35
lines changed

9 files changed

+38
-35
lines changed

src/hiero_sdk_python/executable.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from os import error
22
import time
3-
import typing
3+
from typing import Callable, Optional, Any, TYPE_CHECKING
44
import grpc
55
from abc import ABC, abstractmethod
66
from enum import IntEnum
77

88
from hiero_sdk_python.channels import _Channel
99
from hiero_sdk_python.exceptions import MaxAttemptsError
10-
if typing.TYPE_CHECKING:
10+
if TYPE_CHECKING:
1111
from hiero_sdk_python.client.client import Client
1212

1313
# Default values for retry and backoff configuration in miliseconds
@@ -28,8 +28,8 @@ class _Method:
2828

2929
def __init__(
3030
self,
31-
query_func: typing.Callable = None,
32-
transaction_func: typing.Callable = None,
31+
query_func: Callable = None,
32+
transaction_func: Optional[Callable[..., Any]] = None,
3333
):
3434
"""
3535
Initialize a Method instance with the appropriate callable functions.

src/hiero_sdk_python/query/account_balance_query.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional, Any
12
from hiero_sdk_python.query.query import Query
23
from hiero_sdk_python.hapi.services import crypto_get_account_balance_pb2, query_pb2
34
from hiero_sdk_python.account.account_id import AccountId
@@ -14,15 +15,15 @@ class CryptoGetAccountBalanceQuery(Query):
1415
including hbars and tokens.
1516
"""
1617

17-
def __init__(self, account_id: AccountId = None) -> None:
18+
def __init__(self, account_id: Optional[AccountId] = None) -> None:
1819
"""
1920
Initializes a new instance of the CryptoGetAccountBalanceQuery class.
2021
2122
Args:
2223
account_id (AccountId, optional): The ID of the account to retrieve the balance for.
2324
"""
2425
super().__init__()
25-
self.account_id = account_id
26+
self.account_id: Optional[AccountId] = account_id
2627

2728
def set_account_id(self, account_id: AccountId) -> "CryptoGetAccountBalanceQuery":
2829
"""
@@ -112,7 +113,7 @@ def execute(self, client) -> AccountBalance:
112113

113114
return AccountBalance._from_proto(response.cryptogetAccountBalance)
114115

115-
def _get_query_response(self, response: any) -> crypto_get_account_balance_pb2.CryptoGetAccountBalanceResponse:
116+
def _get_query_response(self, response: Any) -> crypto_get_account_balance_pb2.CryptoGetAccountBalanceResponse:
116117
"""
117118
Extracts the account balance response from the full response.
118119

src/hiero_sdk_python/query/query.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import time
22

3-
from typing import List, Optional
3+
from typing import List, Optional, Any, Union
44

5-
from hiero_sdk_python.exceptions import PrecheckError
5+
from hiero_sdk_python.exceptions import PrecheckError, ReceiptStatusError
66
from hiero_sdk_python.executable import _Method
77
from hiero_sdk_python.channels import _Channel
88
from hiero_sdk_python.hapi.services import query_header_pb2, query_pb2
@@ -49,15 +49,15 @@ def __init__(self) -> None:
4949
self.node_index: int = 0
5050
self.payment_amount: Optional[Hbar] = None
5151

52-
def _get_query_response(self, response: any) -> query_pb2.Query:
52+
def _get_query_response(self, response: Any) -> query_pb2.Query:
5353
"""
5454
Extracts the query-specific response object from the full response.
5555
5656
Subclasses must implement this method to properly extract their
5757
specific response object.
5858
5959
Args:
60-
response (any): The full response from the network
60+
response (Any): The full response from the network
6161
6262
Returns:
6363
The query-specific response object
@@ -257,7 +257,7 @@ def _make_request(self) -> query_pb2.Query:
257257
"""
258258
raise NotImplementedError("_make_request must be implemented by subclasses.")
259259

260-
def _map_response(self, response: any, node_id: int, proto_request: query_pb2.Query) -> query_pb2.Query:
260+
def _map_response(self, response: Any, node_id: int, proto_request: query_pb2.Query) -> query_pb2.Query:
261261
"""
262262
Maps the network response to the appropriate response object.
263263
@@ -271,7 +271,7 @@ def _map_response(self, response: any, node_id: int, proto_request: query_pb2.Qu
271271
"""
272272
return response
273273

274-
def _should_retry(self, response: any) -> _ExecutionState:
274+
def _should_retry(self, response: Any) -> _ExecutionState:
275275
"""
276276
Determines whether the query should be retried based on the response.
277277
@@ -300,7 +300,7 @@ def _should_retry(self, response: any) -> _ExecutionState:
300300
else:
301301
return _ExecutionState.ERROR
302302

303-
def _map_status_error(self, response: any) -> PrecheckError:
303+
def _map_status_error(self, response: Any) -> Union[PrecheckError,ReceiptStatusError]:
304304
"""
305305
Maps a response status code to an appropriate error object.
306306

src/hiero_sdk_python/query/token_info_query.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional
12
from hiero_sdk_python.query.query import Query
23
from hiero_sdk_python.hapi.services import query_pb2, token_get_info_pb2, response_pb2
34
from hiero_sdk_python.executable import _Method
@@ -16,15 +17,15 @@ class TokenInfoQuery(Query):
1617
including the token's properties and settings.
1718
1819
"""
19-
def __init__(self, token_id: TokenId = None) -> None:
20+
def __init__(self, token_id: Optional[TokenId] = None) -> None:
2021
"""
2122
Initializes a new TokenInfoQuery instance with an optional token_id.
2223
2324
Args:
2425
token_id (TokenId, optional): The ID of the token to query.
2526
"""
2627
super().__init__()
27-
self.token_id: TokenId = token_id
28+
self.token_id: Optional[TokenId] = token_id
2829

2930
def set_token_id(self, token_id: TokenId) -> "TokenInfoQuery":
3031
"""

src/hiero_sdk_python/query/token_nft_info_query.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Any
22
from hiero_sdk_python.query.query import Query
33
from hiero_sdk_python.hapi.services import query_pb2, response_pb2, token_get_nft_info_pb2
44
from hiero_sdk_python.executable import _Method

src/hiero_sdk_python/query/topic_info_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Optional, Any
22
from hiero_sdk_python.query.query import Query
33
from hiero_sdk_python.hapi.services import query_pb2, consensus_get_topic_info_pb2, response_pb2
44
from hiero_sdk_python.client.client import Client
@@ -121,7 +121,7 @@ def _get_method(self, channel: _Channel) -> _Method:
121121
query_func=channel.topic.getTopicInfo
122122
)
123123

124-
def _should_retry(self, response: any) -> _ExecutionState:
124+
def _should_retry(self, response: Any) -> _ExecutionState:
125125
"""
126126
Determines whether the query should be retried based on the response.
127127
@@ -174,7 +174,7 @@ def execute(self, client: Client) -> TopicInfo:
174174

175175
return TopicInfo._from_proto(response.consensusGetTopicInfo.topicInfo)
176176

177-
def _get_query_response(self, response: any) -> consensus_get_topic_info_pb2.ConsensusGetTopicInfoResponse:
177+
def _get_query_response(self, response: Any) -> consensus_get_topic_info_pb2.ConsensusGetTopicInfoResponse:
178178
"""
179179
Extracts the topic info response from the full response.
180180

src/hiero_sdk_python/query/topic_message_query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22
import threading
33
from datetime import datetime
4-
from typing import Optional, Callable, Union, Dict, List
4+
from typing import Optional, Callable, Union, Dict, List, Any
55

66
from hiero_sdk_python.hapi.mirror import consensus_service_pb2 as mirror_proto
77
from hiero_sdk_python.hapi.services import basic_types_pb2, timestamp_pb2
@@ -30,15 +30,15 @@ def __init__(
3030
"""
3131
Initializes a TopicMessageQuery.
3232
"""
33-
self._topic_id: TopicId = self._parse_topic_id(topic_id) if topic_id else None
34-
self._start_time: timestamp_pb2.Timestamp = self._parse_timestamp(start_time) if start_time else None
35-
self._end_time: timestamp_pb2.Timestamp = self._parse_timestamp(end_time) if end_time else None
36-
self._limit: int = limit
33+
self._topic_id: Optional[TopicId] = self._parse_topic_id(topic_id) if topic_id else None
34+
self._start_time: Optional[timestamp_pb2.Timestamp] = self._parse_timestamp(start_time) if start_time else None
35+
self._end_time: Optional[timestamp_pb2.Timestamp] = self._parse_timestamp(end_time) if end_time else None
36+
self._limit: Optional[int] = limit
3737
self._chunking_enabled: bool = chunking_enabled
3838
self._completion_handler: Optional[Callable[[], None]] = None
3939

4040
self._max_attempts: int = 10
41-
self._max_backoff: int = 8.0
41+
self._max_backoff: float = 8.0
4242

4343
def set_max_attempts(self, attempts: int) -> "TopicMessageQuery":
4444
"""Sets the maximum number of attempts to reconnect on failure."""

src/hiero_sdk_python/query/transaction_get_receipt_query.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional,Union
1+
from typing import Optional,Union,Any
22

33
from hiero_sdk_python.client.client import Client
44
from hiero_sdk_python.exceptions import PrecheckError, ReceiptStatusError
@@ -129,7 +129,7 @@ def _get_method(self, channel: _Channel) -> _Method:
129129
query_func=channel.crypto.getTransactionReceipts
130130
)
131131

132-
def _should_retry(self, response: any) -> _ExecutionState:
132+
def _should_retry(self, response: Any) -> _ExecutionState:
133133
"""
134134
Determines whether the query should be retried based on the response.
135135
@@ -167,7 +167,7 @@ def _should_retry(self, response: any) -> _ExecutionState:
167167
else:
168168
return _ExecutionState.FINISHED
169169

170-
def _map_status_error(self, response: any) -> Union[PrecheckError,ReceiptStatusError]:
170+
def _map_status_error(self, response: Any) -> Union[PrecheckError,ReceiptStatusError]:
171171
"""
172172
Maps a response status code to an appropriate error object.
173173

src/hiero_sdk_python/query/transaction_record_query.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from typing import Optional, Any, Union
12
from hiero_sdk_python.hapi.services import query_header_pb2, transaction_get_record_pb2, query_pb2
23
from hiero_sdk_python.query.query import Query
34
from hiero_sdk_python.response_code import ResponseCode
@@ -15,12 +16,12 @@ class TransactionRecordQuery(Query):
1516
Represents a query for a transaction record on the Hedera network.
1617
"""
1718

18-
def __init__(self, transaction_id: TransactionId = None):
19+
def __init__(self, transaction_id: Optional[TransactionId] = None):
1920
"""
2021
Initializes the TransactionRecordQuery with the provided transaction ID.
2122
"""
2223
super().__init__()
23-
self.transaction_id : TransactionId = transaction_id
24+
self.transaction_id : Optional[TransactionId] = transaction_id
2425

2526
def set_transaction_id(self, transaction_id: TransactionId):
2627
"""
@@ -86,7 +87,7 @@ def _get_method(self, channel: _Channel) -> _Method:
8687
query_func=channel.crypto.getTxRecordByTxID
8788
)
8889

89-
def _should_retry(self, response):
90+
def _should_retry(self, response: Any) -> _ExecutionState:
9091
"""
9192
Determines whether the query should be retried based on the response.
9293
@@ -128,7 +129,7 @@ def _should_retry(self, response):
128129
else:
129130
return _ExecutionState.ERROR
130131

131-
def _map_status_error(self, response):
132+
def _map_status_error(self, response: Any) -> Union[PrecheckError,ReceiptStatusError]:
132133
"""
133134
Maps a response status code to an appropriate error object.
134135
@@ -158,7 +159,7 @@ def _map_status_error(self, response):
158159
receipt = response.transactionGetRecord.transactionRecord.receipt
159160

160161
return ReceiptStatusError(status, self.transaction_id, TransactionReceipt._from_proto(receipt))
161-
162+
162163
def execute(self, client):
163164
"""
164165
Executes the transaction record query.
@@ -184,7 +185,7 @@ def execute(self, client):
184185

185186
return TransactionRecord._from_proto(response.transactionGetRecord.transactionRecord, self.transaction_id)
186187

187-
def _get_query_response(self, response):
188+
def _get_query_response(self, response: Any):
188189
"""
189190
Extracts the transaction record response from the full response.
190191

0 commit comments

Comments
 (0)