You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README_types.md
+160Lines changed: 160 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -43,3 +43,163 @@ y: float = 1.0
43
43
frozen: bool = True
44
44
name: str = "Rupert"
45
45
46
+
## What are Type Hints?
47
+
48
+
Type hints let you declare the expected types of variables, function parameters, and return values.
49
+
50
+
### When to Use Type Hints
51
+
-**Variables**
52
+
-**Function parameters**
53
+
-**Return values**
54
+
55
+
> **Note:** Inline type hints are preferred over repeating types in docstrings, which can lead to duplication and maintenance overhead. If you do include types in docstrings, they must match the inline annotations exactly.
56
+
57
+
---
58
+
59
+
### Examples
60
+
61
+
#### Correct
62
+
name: str = ""
63
+
64
+
def greet(name: str) -> str:
65
+
"""Return a personalized greeting."""
66
+
return f"Hello, {name}!"
67
+
68
+
print(greet("Beatrice"))
69
+
70
+
#### Incorrect
71
+
name: str = ""
72
+
73
+
def greet(name: str) -> str:
74
+
"""
75
+
Return a personalized greeting.
76
+
77
+
Args:
78
+
name (int): ❌ wrong type here!
79
+
80
+
Returns:
81
+
bool: ❌ wrong type here!
82
+
"""
83
+
return f"Hello, {name}!"
84
+
85
+
print(greet("Beatrice"))
86
+
87
+
## Why Type Hint?
88
+
89
+
Correct type handling is necessary for the proper functioning of code.
90
+
91
+
Proper type hinting enables developers to:
92
+
93
+
-**Understand** the intended data structures and interfaces
94
+
-**Maintain** and navigate the codebase more easily
95
+
-**Refactor** with confidence, catching mismatches early
96
+
-**Extend** the code and documentation without introducing errors
97
+
98
+
Additionally, type hints unlock the use of static analysis tools (like MyPy), allowing you to catch type-related bugs before runtime and ship more robust code.
99
+
100
+
## Typing Using the `typing` Module
101
+
102
+
Most common types (e.g., `str`, `int`, `float`, `bool`, `bytes`, `list`, `dict`, `tuple`, `set`, `None`) are built-in in Python 3.10+. For more advanced or generic types, import from the `typing` module:
103
+
104
+
from typing import (
105
+
Any, # “escape hatch” for unknown types
106
+
Union, # still available, though often replaced by |
107
+
Optional, # same as Union[..., None]
108
+
Callable, # for callables with specific signatures
109
+
TypeVar, Generic, # for custom generics
110
+
ClassVar, # annotate class-level constants
111
+
Literal, Final, # exact values & constants
112
+
TypedDict, # dicts with fixed key types
113
+
Protocol, # structural/subtype interfaces
114
+
NewType, # distinct alias for an existing type
115
+
TypeAlias, # name a complex type
116
+
Annotated, # add metadata to existing types
117
+
ParamSpec, Concatenate, # for decorator/dependent signatures
118
+
overload, cast, NoReturn, # various mypy helpers
119
+
)
120
+
121
+
### Example
122
+
from typing import TypedDict, Literal
123
+
124
+
class User(TypedDict):
125
+
id: int
126
+
name: str
127
+
active: bool
128
+
129
+
Status = Literal["pending", "active", "disabled"]
130
+
131
+
def find_user(uid: int) -> User | None:
132
+
"""
133
+
Lookup a user by ID.
134
+
Returns a `User` dict if found, or `None` otherwise.
135
+
"""
136
+
if uid == 1:
137
+
return {"id": 1, "name": "Alice", "active": True}
138
+
return None
139
+
140
+
## Custom Types
141
+
142
+
You can use custom types whenever you’ve defined your own classes.
143
+
144
+
### Example
145
+
from dataclasses import dataclass
146
+
from hiero_sdk import CryptoGetAccountBalanceQuery
By default, MyPy is a very strict type checker. This project includes a `mypy.ini` at `/mypy.ini` to enable gradual, module-by-module checking. You can customize it to suit your needs:
193
+
194
+
```ini
195
+
# Do not error on imports that lack type stubs
196
+
ignore_missing_imports = True
197
+
198
+
# Allow parameters to default to None without forcing X | None annotations
199
+
implicit_optional = True
200
+
201
+
# Suppress errors when calling functions without type annotations
202
+
allow_untyped_calls = True
203
+
204
+
For a full list of flags and options, see the MyPy command-line reference:
0 commit comments