Skip to content

Commit 5ef312c

Browse files
authored
feat: FileId class cleanup, docstrings and typing (#656)
Signed-off-by: Raja Rathour <imraja729@gmail.com> Signed-off-by: Raja Rathour <193507075+Raja-89@users.noreply.github.com>
1 parent 43fb57f commit 5ef312c

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

CHANGELOG.md

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

99
### Added
10+
- Standardized docstrings, improved error handling, and updated type hinting (`str | None` to `Optional[str]`) for the `FileId` class (#652).
1011

1112
- Add Google-style docstrings to `AccountInfo` class and its methods in `account_info.py`.
1213
- Added comprehensive Google-style docstrings to the `Logger` class and all utility functions in `src/hiero_sdk_python/logger/logger.py` (#639).

src/hiero_sdk_python/file/file_id.py

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
"""
22
FileId class.
3+
4+
This module defines the FileId object, which serves as a unique identifier for
5+
files stored on the Hedera network. It supports string parsing, checksum validation,
6+
and conversion to and from protobuf structures.
37
"""
48
from dataclasses import dataclass, field
9+
from typing import Optional
510

611
from hiero_sdk_python.client.client import Client
712
from hiero_sdk_python.hapi.services import basic_types_pb2
@@ -17,23 +22,31 @@ class FileId:
1722
"""
1823
Represents a file ID on the network.
1924
20-
A file ID consists of three components: shard, realm, and file (number).
25+
A file ID consists of three components: shard, realm, and file number.
2126
These components uniquely identify a file in the network.
2227
2328
Attributes:
2429
shard (int): The shard number. Defaults to 0.
2530
realm (int): The realm number. Defaults to 0.
2631
file (int): The file number. Defaults to 0.
32+
checksum (Optional[str]): The calculated checksum for the file ID.
2733
"""
2834
shard: int = 0
2935
realm: int = 0
3036
file: int = 0
31-
checksum: str | None = field(default=None, init=False)
37+
checksum: Optional[str] = field(default=None, init=False)
3238

3339
@classmethod
3440
def _from_proto(cls, file_id_proto: basic_types_pb2.FileID) -> 'FileId':
3541
"""
3642
Creates a FileId instance from a protobuf FileID object.
43+
44+
Args:
45+
file_id_proto (basic_types_pb2.FileID): The protobuf object containing
46+
the file ID components.
47+
48+
Returns:
49+
FileId: A new instance of the FileId class.
3750
"""
3851
return cls(
3952
shard=file_id_proto.shardNum,
@@ -44,6 +57,9 @@ def _from_proto(cls, file_id_proto: basic_types_pb2.FileID) -> 'FileId':
4457
def _to_proto(self) -> basic_types_pb2.FileID:
4558
"""
4659
Converts the FileId instance to a protobuf FileID object.
60+
61+
Returns:
62+
basic_types_pb2.FileID: The protobuf representation of the FileId.
4763
"""
4864
return basic_types_pb2.FileID(
4965
shardNum=self.shard,
@@ -54,7 +70,17 @@ def _to_proto(self) -> basic_types_pb2.FileID:
5470
@classmethod
5571
def from_string(cls, file_id_str: str) -> 'FileId':
5672
"""
57-
Creates a FileId instance from a string in the format 'shard.realm.file'.
73+
Parses a string in the format 'shard.realm.file' (with optional checksum)
74+
to create a FileId instance.
75+
76+
Args:
77+
file_id_str (str): The string representation of the File ID.
78+
79+
Returns:
80+
FileId: A new instance with components and checksum (if present).
81+
82+
Raises:
83+
ValueError: If the input string is malformed or cannot be parsed.
5884
"""
5985
try:
6086
shard, realm, file, checksum = parse_from_string(file_id_str)
@@ -67,19 +93,30 @@ def from_string(cls, file_id_str: str) -> 'FileId':
6793
object.__setattr__(file_id, 'checksum', checksum)
6894

6995
return file_id
70-
except Exception as e:
96+
except Exception as e:
7197
raise ValueError(
7298
f"Invalid file ID string '{file_id_str}'. Expected format 'shard.realm.file'."
7399
) from e
74-
75100
def __str__(self) -> str:
76101
"""
77-
Returns a string representation of the FileId instance.
102+
Returns the string representation of the FileId in the format 'shard.realm.file'.
103+
104+
Returns:
105+
str: The string representation of the file ID components.
78106
"""
79107
return f"{self.shard}.{self.realm}.{self.file}"
80108

81109
def validate_checksum(self, client: Client) -> None:
82-
"""Validate the checksum for the FileId instance"""
110+
"""
111+
Validates the stored checksum against the calculated checksum using the provided client.
112+
113+
Args:
114+
client (Client): The client instance used to retrieve network information
115+
necessary for checksum calculation.
116+
117+
Raises:
118+
ValueError: If the stored checksum is invalid or does not match the calculated value.
119+
"""
83120
validate_checksum(
84121
self.shard,
85122
self.realm,
@@ -90,12 +127,19 @@ def validate_checksum(self, client: Client) -> None:
90127

91128
def to_string_with_checksum(self, client: Client) -> str:
92129
"""
93-
Returns the string representation of the FileId with checksum
130+
Returns the string representation of the FileId with its calculated checksum
94131
in 'shard.realm.file-checksum' format.
132+
133+
Args:
134+
client (Client): The client instance used to retrieve network information
135+
necessary for checksum calculation.
136+
137+
Returns:
138+
str: The file ID formatted with its checksum.
95139
"""
96140
return format_to_string_with_checksum(
97141
self.shard,
98142
self.realm,
99143
self.file,
100144
client
101-
)
145+
)

0 commit comments

Comments
 (0)