Skip to content

Commit fe788ce

Browse files
authored
chore: resolve pylint issues in /consensus module (#278)
* feat: resolve linting issues and formatting Signed-off-by: harshadsutar <[email protected]> * chore:linting topic_create_transaction Signed-off-by: harshadsutar <[email protected]> * fix : rename .env.example filename Signed-off-by: harshadsutar <[email protected]> --------- Signed-off-by: harshadsutar <[email protected]>
1 parent 2cfd9fa commit fe788ce

File tree

9 files changed

+194
-66
lines changed

9 files changed

+194
-66
lines changed

CHANGELOG.md

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

2424
### Fixed
2525
- missing ECDSA support in query.py and contract_create_transaction.py (was only creating ED25519 keys)
26+
- Applied linting and code formatting across the consensus module
2627

2728

2829
### Breaking API changes

generate_proto.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Source the activate script to set up the PATH for this shell session
44
source ./.venv/bin/activate
55

6+
67
hapi_version="v0.57.3"
78
protos_dir=".protos"
89
services_dir="src/hiero_sdk_python/hapi/services"

src/hiero_sdk_python/consensus/topic_create_transaction.py

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1-
from typing import Union
1+
"""
2+
This module defines the TopicCreateTransaction class for creating topics
3+
on the Hedera Consensus Service (HCS) using the Hiero Python SDK.
4+
5+
It provides methods to set properties such as memo, admin key, submit key,
6+
auto-renew period, and auto-renew account, and to build the protobuf
7+
transaction body for submission to the Hedera network .
8+
"""
29

10+
from typing import Union
311
from hiero_sdk_python.Duration import Duration
412
from hiero_sdk_python.transaction.transaction import Transaction
5-
from hiero_sdk_python.hapi.services import consensus_create_topic_pb2, transaction_body_pb2, basic_types_pb2
13+
from hiero_sdk_python.hapi.services import (
14+
consensus_create_topic_pb2,
15+
transaction_body_pb2,
16+
basic_types_pb2)
617
from hiero_sdk_python.channels import _Channel
718
from hiero_sdk_python.executable import _Method
819
from hiero_sdk_python.account.account_id import AccountId
920

21+
1022
class TopicCreateTransaction(Transaction):
23+
"""
24+
Represents a transaction to create a new topic in the Hedera
25+
Consensus Service (HCS).
26+
27+
This transaction can optionally define an admin key, submit key,
28+
auto-renew period, auto-renew account, and memo.
29+
"""
1130
def __init__(
12-
self,
13-
memo: str = None,
14-
admin_key: basic_types_pb2.Key = None,
15-
submit_key: basic_types_pb2.Key = None,
16-
auto_renew_period: Duration = None,
17-
auto_renew_account: AccountId = None
31+
self,
32+
memo: str = None,
33+
admin_key: basic_types_pb2.Key = None,
34+
submit_key: basic_types_pb2.Key = None,
35+
auto_renew_period: Duration = None,
36+
auto_renew_account: AccountId = None
1837
) -> None:
1938
"""
2039
Initializes a new instance of the TopicCreateTransaction class.
@@ -69,7 +88,7 @@ def set_submit_key(self, key: basic_types_pb2.Key) -> "TopicCreateTransaction":
6988
self.submit_key = key
7089
return self
7190

72-
def set_auto_renew_period(self, seconds: Union[Duration,int]) -> "TopicCreateTransaction":
91+
def set_auto_renew_period(self, seconds: Union[Duration, int]) -> "TopicCreateTransaction":
7392
"""
7493
Sets the auto-renew period for the topic creation transaction.
7594
Args:
@@ -110,14 +129,28 @@ def build_transaction_body(self):
110129
Raises:
111130
ValueError: If required fields are missing.
112131
"""
113-
transaction_body: transaction_body_pb2.TransactionBody = self.build_base_transaction_body()
114-
transaction_body.consensusCreateTopic.CopyFrom(consensus_create_topic_pb2.ConsensusCreateTopicTransactionBody(
115-
adminKey=self.admin_key._to_proto() if self.admin_key is not None else None,
116-
submitKey=self.submit_key._to_proto() if self.submit_key is not None else None,
117-
autoRenewPeriod=self.auto_renew_period._to_proto() if self.auto_renew_period is not None else None,
118-
autoRenewAccount=self.auto_renew_account._to_proto() if self.auto_renew_account is not None else None,
119-
memo=self.memo
120-
))
132+
transaction_body: transaction_body_pb2.TransactionBody = (
133+
self.build_base_transaction_body())
134+
transaction_body.consensusCreateTopic.CopyFrom(
135+
consensus_create_topic_pb2.ConsensusCreateTopicTransactionBody(
136+
adminKey=(
137+
self.admin_key._to_proto()
138+
if self.admin_key is not None
139+
else None),
140+
submitKey=(
141+
self.submit_key._to_proto()
142+
if self.submit_key is not None
143+
else None),
144+
autoRenewPeriod=(
145+
self.auto_renew_period._to_proto()
146+
if self.auto_renew_period is not None
147+
else None),
148+
autoRenewAccount=(
149+
self.auto_renew_account._to_proto()
150+
if self.auto_renew_account is not None
151+
else None),
152+
memo=self.memo
153+
))
121154

122155
return transaction_body
123156

src/hiero_sdk_python/consensus/topic_delete_transaction.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,28 @@
1+
"""
2+
This module provides the `TopicDeleteTransaction` class for deleting consensus topics
3+
on the Hedera network using the Hiero SDK.
4+
5+
It handles setting the target topic ID, building the protobuf transaction body, and
6+
defining the execution method required to perform the deletion transaction.
7+
"""
8+
9+
110
from hiero_sdk_python.transaction.transaction import Transaction
2-
from hiero_sdk_python.hapi.services import consensus_delete_topic_pb2, transaction_body_pb2, basic_types_pb2
11+
from hiero_sdk_python.hapi.services import (
12+
consensus_delete_topic_pb2,
13+
transaction_body_pb2,
14+
basic_types_pb2
15+
)
316
from hiero_sdk_python.channels import _Channel
417
from hiero_sdk_python.executable import _Method
518

19+
620
class TopicDeleteTransaction(Transaction):
21+
"""
22+
Represents a transaction to delete an existing topic in the Hedera
23+
Consensus Service (HCS).
24+
25+
"""
726
def __init__(self, topic_id: basic_types_pb2.TopicID = None):
827
super().__init__()
928
self.topic_id: basic_types_pb2.TopicID = topic_id
@@ -35,9 +54,9 @@ def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
3554
"""
3655
if self.topic_id is None:
3756
raise ValueError("Missing required fields: topic_id")
38-
3957
transaction_body: transaction_body_pb2.TransactionBody = self.build_base_transaction_body()
40-
transaction_body.consensusDeleteTopic.CopyFrom(consensus_delete_topic_pb2.ConsensusDeleteTopicTransactionBody(
58+
transaction_body.consensusDeleteTopic.CopyFrom(
59+
consensus_delete_topic_pb2.ConsensusDeleteTopicTransactionBody(
4160
topicID=self.topic_id._to_proto()
4261
))
4362

@@ -54,4 +73,4 @@ def _get_method(self, channel: _Channel) -> _Method:
5473
return _Method(
5574
transaction_func=channel.topic.deleteTopic,
5675
query_func=None
57-
)
76+
)

src/hiero_sdk_python/consensus/topic_id.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
"""
2+
This module defines the `TopicId` class, a utility for working with Hedera
3+
Consensus Service (HCS) topic identifiers.
4+
5+
It provides methods for converting between protobuf `TopicID` objects and
6+
string representations, making it easier to work with topics in different
7+
formats within the Hiero SDK.
8+
"""
9+
110
from hiero_sdk_python.hapi.services import basic_types_pb2
211

312
class TopicId:
13+
14+
"""
15+
Represents the unique identifier of a topic in the Hedera Consensus Service (HCS).
16+
17+
A `TopicId` consists of three components: shard, realm, and num.
18+
This class provides convenient methods for converting between Python objects,
19+
protobuf `TopicID` instances, and string formats.
20+
"""
421
def __init__(self, shard: int = 0, realm: int = 0, num: int = 0) -> None:
522
"""
623
Initializes a new instance of the TopicId class.
@@ -48,4 +65,4 @@ def from_string(cls, topic_id_str) -> "TopicId":
4865
parts = topic_id_str.strip().split('.')
4966
if len(parts) != 3:
5067
raise ValueError("Invalid TopicId format. Expected 'shard.realm.num'")
51-
return cls(shard=int(parts[0]), realm=int(parts[1]), num=int(parts[2]))
68+
return cls(shard=int(parts[0]), realm=int(parts[1]), num=int(parts[2]))

src/hiero_sdk_python/consensus/topic_info.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
1+
"""
2+
This module provides the `TopicInfo` class for representing consensus topic
3+
metadata on the Hedera network using the Hiero SDK.
4+
5+
It handles constructing the object from a protobuf message, formatting
6+
optional fields, and providing a readable string representation of the
7+
topic state.
8+
"""
19
from datetime import datetime
10+
from typing import Optional
211
from hiero_sdk_python.hapi.services.basic_types_pb2 import Key, AccountID
312
from hiero_sdk_python.hapi.services.timestamp_pb2 import Timestamp
413
from hiero_sdk_python.hapi.services import consensus_topic_info_pb2
514
from hiero_sdk_python.Duration import Duration
615
from hiero_sdk_python.utils.key_format import format_key
7-
from typing import Optional
16+
817

918
class TopicInfo:
19+
"""
20+
Represents consensus topic information on the Hedera network.
21+
22+
It wraps the `ConsensusTopicInfo` protobuf message, exposing attributes
23+
such as memo, running hash, sequence number, expiration time, admin key,
24+
submit key, auto-renewal configuration, and ledger ID.
25+
"""
1026
def __init__(
11-
self,
12-
memo: str,
13-
running_hash: bytes,
14-
sequence_number: int,
15-
expiration_time: Timestamp,
16-
admin_key: Optional[Key],
17-
submit_key: Optional[Key],
18-
auto_renew_period: Optional[Duration],
19-
auto_renew_account: Optional[AccountID],
20-
ledger_id: Optional[bytes],
27+
self,
28+
memo: str,
29+
running_hash: bytes,
30+
sequence_number: int,
31+
expiration_time: Timestamp,
32+
admin_key: Optional[Key],
33+
submit_key: Optional[Key],
34+
auto_renew_period: Optional[Duration],
35+
auto_renew_account: Optional[AccountID],
36+
ledger_id: Optional[bytes],
2137

2238
) -> None:
2339
"""
@@ -44,7 +60,10 @@ def __init__(
4460
self.ledger_id: bytes = ledger_id
4561

4662
@classmethod
47-
def _from_proto(cls, topic_info_proto: consensus_topic_info_pb2.ConsensusTopicInfo) -> "TopicInfo":
63+
def _from_proto(
64+
cls,
65+
topic_info_proto: consensus_topic_info_pb2.ConsensusTopicInfo
66+
) -> "TopicInfo":
4867
"""
4968
Constructs a TopicInfo object from a protobuf ConsensusTopicInfo message.
5069
"""
@@ -53,26 +72,27 @@ def _from_proto(cls, topic_info_proto: consensus_topic_info_pb2.ConsensusTopicIn
5372
running_hash=topic_info_proto.runningHash,
5473
sequence_number=topic_info_proto.sequenceNumber,
5574
expiration_time=(
56-
topic_info_proto.expirationTime
75+
topic_info_proto.expirationTime
5776
if topic_info_proto.HasField("expirationTime") else None
5877
),
5978
admin_key=(
60-
topic_info_proto.adminKey
79+
topic_info_proto.adminKey
6180
if topic_info_proto.HasField("adminKey") else None
6281
),
6382
submit_key=(
64-
topic_info_proto.submitKey
83+
topic_info_proto.submitKey
6584
if topic_info_proto.HasField("submitKey") else None
6685
),
6786
auto_renew_period=(
6887
Duration._from_proto(proto=topic_info_proto.autoRenewPeriod)
6988
if topic_info_proto.HasField("autoRenewPeriod") else None
7089
),
7190
auto_renew_account=(
72-
topic_info_proto.autoRenewAccount
91+
topic_info_proto.autoRenewAccount
7392
if topic_info_proto.HasField("autoRenewAccount") else None
7493
),
75-
ledger_id=getattr(topic_info_proto, "ledger_id", None), # fallback if the field doesn't exist
94+
ledger_id=getattr(topic_info_proto, "ledger_id", None),
95+
# fallback if the field doesn't exist
7696
)
7797

7898
def __repr__(self) -> str:
@@ -89,7 +109,7 @@ def __str__(self) -> str:
89109
exp_dt: datetime = None
90110
if self.expiration_time and hasattr(self.expiration_time, "seconds"):
91111
exp_dt = datetime.fromtimestamp(self.expiration_time.seconds)
92-
112+
93113
running_hash_hex: str = self.running_hash.hex() if self.running_hash else None
94114
ledger_id_hex: Optional[str] = (
95115
self.ledger_id.hex() if isinstance(self.ledger_id, (bytes, bytearray)) else None

src/hiero_sdk_python/consensus/topic_message.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
"""
2+
This module provides the `TopicMessage` and `TopicMessageChunk` classes for handling
3+
Hedera Consensus Service topic messages using the Hiero SDK.
4+
"""
5+
16
from datetime import datetime
27
from typing import Optional, List, Union, Dict
3-
48
from hiero_sdk_python import Timestamp
59
from hiero_sdk_python.hapi.mirror import consensus_service_pb2 as mirror_proto
610

@@ -17,22 +21,25 @@ def __init__(self, response: mirror_proto.ConsensusTopicResponse) -> None:
1721
Args:
1822
response: The ConsensusTopicResponse containing chunk data.
1923
"""
20-
self.consensus_timestamp: datetime = Timestamp._from_protobuf(response.consensusTimestamp).to_date()
24+
self.consensus_timestamp: datetime = Timestamp._from_protobuf(
25+
response.consensusTimestamp
26+
).to_date()
2127
self.content_size: int = len(response.message)
2228
self.running_hash: Union[bytes, int] = response.runningHash
2329
self.sequence_number: Union[bytes, int] = response.sequenceNumber
2430

31+
2532
class TopicMessage:
2633
"""
2734
Represents a Hedera TopicMessage, possibly composed of multiple chunks.
2835
"""
2936

3037
def __init__(
31-
self,
32-
consensus_timestamp: datetime,
33-
message_data: Dict[str, Union[bytes, int]],
34-
chunks: List[TopicMessageChunk],
35-
transaction_id: Optional[str] = None,
38+
self,
39+
consensus_timestamp: datetime,
40+
message_data: Dict[str, Union[bytes, int]],
41+
chunks: List[TopicMessageChunk],
42+
transaction_id: Optional[str] = None,
3643
) -> None:
3744
"""
3845
Args:
@@ -100,9 +107,9 @@ def of_many(cls, responses: List[mirror_proto.ConsensusTopicResponse]) -> "Topic
100107
total_size += len(r.message)
101108

102109
if (
103-
transaction_id is None
104-
and r.HasField("chunkInfo")
105-
and r.chunkInfo.HasField("initialTransactionID")
110+
transaction_id is None
111+
and r.HasField("chunkInfo")
112+
and r.chunkInfo.HasField("initialTransactionID")
106113
):
107114
tx_id = r.chunkInfo.initialTransactionID
108115
transaction_id = (
@@ -135,9 +142,12 @@ def of_many(cls, responses: List[mirror_proto.ConsensusTopicResponse]) -> "Topic
135142

136143
@classmethod
137144
def _from_proto(
138-
cls,
139-
response_or_responses: Union[mirror_proto.ConsensusTopicResponse, List[mirror_proto.ConsensusTopicResponse]],
140-
chunking_enabled: bool = False
145+
cls,
146+
response_or_responses: Union[
147+
mirror_proto.ConsensusTopicResponse,
148+
List[mirror_proto.ConsensusTopicResponse]
149+
],
150+
chunking_enabled: bool = False
141151
) -> "TopicMessage":
142152
"""
143153
Creates a TopicMessage from either:
@@ -159,7 +169,8 @@ def _from_proto(
159169
response = response_or_responses
160170
if chunking_enabled and response.HasField("chunkInfo") and response.chunkInfo.total > 1:
161171
raise ValueError(
162-
"Cannot handle multi-chunk in a single response. Pass all chunk responses in a list."
172+
"Cannot handle multi-chunk in a single response."
173+
" Pass all chunk responses in a list."
163174
)
164175
return cls.of_single(response)
165176

0 commit comments

Comments
 (0)