Skip to content

Commit c20a2e3

Browse files
authored
docs: Add Google-style docstrings to token_id.py (#636)
Signed-off-by: Pranay <[email protected]>
1 parent e348aab commit c20a2e3

File tree

2 files changed

+76
-19
lines changed

2 files changed

+76
-19
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+
- docs: Add Google-style docstrings to `TokenId` class and its methods in `token_id.py`.
1011
- Standardized docstrings, improved error handling, and updated type hinting (`str | None` to `Optional[str]`) for the `FileId` class (#652).
1112

1213
- Add Google-style docstrings to `AccountInfo` class and its methods in `account_info.py`.

src/hiero_sdk_python/tokens/token_id.py

Lines changed: 75 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,31 @@
1919

2020
@dataclass(frozen=True, eq=True, init=True, repr=True)
2121
class TokenId:
22-
"""Immutable token identifier (shard, realm, num) with validation and protobuf conversion."""
22+
"""Represents an immutable Hedera token identifier (shard, realm, num).
23+
24+
This is a frozen dataclass providing validation, string parsing,
25+
and protobuf conversion utilities for a token ID.
26+
27+
Attributes:
28+
shard (int): The shard number (non-negative).
29+
realm (int): The realm number (non-negative).
30+
num (int): The entity number (non-negative).
31+
checksum (str | None): An optional checksum, automatically populated
32+
when parsing from a string with a checksum. Not directly
33+
initializable.
34+
"""
2335
shard: int
2436
realm: int
2537
num: int
2638
checksum: str | None = field(default=None, init=False)
2739

2840
def __post_init__(self) -> None:
41+
"""
42+
Validates that shard, realm, and num are non-negative after initialization.
43+
44+
Raises:
45+
ValueError: If shard, realm, or num is less than 0.
46+
"""
2947
if self.shard < 0:
3048
raise ValueError('Shard must be >= 0')
3149
if self.realm < 0:
@@ -35,8 +53,17 @@ def __post_init__(self) -> None:
3553

3654
@classmethod
3755
def _from_proto(cls, token_id_proto: Optional[basic_types_pb2.TokenID] = None) -> "TokenId":
38-
"""
39-
Creates a TokenId instance from a protobuf TokenID object.
56+
"""Creates a TokenId instance from a protobuf TokenID object.
57+
58+
Args:
59+
token_id_proto (Optional[basic_types_pb2.TokenID]): The protobuf
60+
TokenID object.
61+
62+
Returns:
63+
TokenId: The corresponding TokenId instance.
64+
65+
Raises:
66+
ValueError: If token_id_proto is None.
4067
"""
4168
if token_id_proto is None:
4269
raise ValueError('TokenId is required')
@@ -48,8 +75,10 @@ def _from_proto(cls, token_id_proto: Optional[basic_types_pb2.TokenID] = None) -
4875
)
4976

5077
def _to_proto(self) -> basic_types_pb2.TokenID:
51-
"""
52-
Converts the TokenId instance to a protobuf TokenID object.
78+
"""Converts the TokenId instance to a protobuf TokenID object.
79+
80+
Returns:
81+
basic_types_pb2.TokenID: The corresponding protobuf TokenID object.
5382
"""
5483
token_id_proto = basic_types_pb2.TokenID()
5584
token_id_proto.shardNum = self.shard
@@ -59,19 +88,20 @@ def _to_proto(self) -> basic_types_pb2.TokenID:
5988

6089
@classmethod
6190
def from_string(cls, token_id_str: Optional[str] = None) -> "TokenId":
62-
"""
63-
Creates a TokenId instance from a string in the format 'shard.realm.num'.
91+
"""Parses a string to create a TokenId instance.
92+
93+
The string can be in the format 'shard.realm.num' or
94+
'shard.realm.num-checksum'.
6495
6596
Args:
66-
token_id_str (str): The string representation of the token ID.
97+
token_id_str (Optional[str]): The token ID string to parse.
6798
6899
Returns:
69-
TokenId: A new TokenId instance parsed from the provided string.
100+
TokenId: The corresponding TokenId instance.
70101
71102
Raises:
72-
ValueError:
73-
- If token_id_str is None.
74-
- If the input string cannot be parsed or is malformed.
103+
ValueError: If the token_id_str is None, empty, or in an
104+
invalid format.
75105
"""
76106
if token_id_str is None:
77107
raise ValueError("token_id_str cannot be None")
@@ -92,7 +122,19 @@ def from_string(cls, token_id_str: Optional[str] = None) -> "TokenId":
92122
)from e
93123

94124
def validate_checksum(self, client: Client) -> None:
95-
"""Validate the checksum for the TokenId instance"""
125+
"""Validates the checksum (if present) against the client's network.
126+
127+
Args:
128+
client (Client): The client instance, used to determine the
129+
network ledger ID for validation.
130+
131+
Raises:
132+
ValueError: If the client's ledger ID is not set (required for
133+
validation).
134+
ValueError: If the checksum is present but does not match the
135+
expected checksum for the client's network (e.g.,
136+
"Checksum mismatch for 0.0.123").
137+
"""
96138
validate_checksum(
97139
shard=self.shard,
98140
realm=self.realm,
@@ -102,9 +144,17 @@ def validate_checksum(self, client: Client) -> None:
102144
)
103145

104146
def to_string_with_checksum(self, client:Client) -> str:
105-
"""
106-
Returns the string representation of the TokenId with checksum
107-
in the format 'shard.realm.num-checksum'
147+
"""Returns the string representation with a network-specific checksum.
148+
149+
Generates a checksum based on the client's network and returns
150+
the ID in 'shard.realm.num-checksum' format.
151+
152+
Args:
153+
client (Client): The client instance used to generate the
154+
network-specific checksum.
155+
156+
Returns:
157+
str: The token ID string with a calculated checksum.
108158
"""
109159
return format_to_string_with_checksum(
110160
shard=self.shard,
@@ -114,11 +164,17 @@ def to_string_with_checksum(self, client:Client) -> str:
114164
)
115165

116166
def __str__(self) -> str:
117-
"""
118-
Returns the string representation of the TokenId in the format 'shard.realm.num'.
167+
"""Returns the simple string representation 'shard.realm.num'.
168+
169+
Returns:
170+
str: The token ID string in 'shard.realm.num' format.
119171
"""
120172
return format_to_string(self.shard, self.realm, self.num)
121173

122174
def __hash__(self) -> int:
123-
""" Returns a hash of the TokenId instance. """
175+
"""Generates a hash based on the shard, realm, and num.
176+
177+
Returns:
178+
int: A hash of the TokenId instance.
179+
"""
124180
return hash((self.shard, self.realm, self.num))

0 commit comments

Comments
 (0)