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
+32-10Lines changed: 32 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,5 @@
1
1
## Introduction
2
+
This is a markdown file, click Ctrl+Shift+V to view or click open preview.
2
3
3
4
This guide provides an overview of Python’s built-in types, explains how and why to use type hints, and demonstrates how to leverage the `typing` module and MyPy for static type checking. Whether you’re new to type annotations or looking to adopt a more robust workflow, you’ll find examples and best practices for writing clearer, safer, and more maintainable Python code.
4
5
@@ -52,10 +53,12 @@ Python code can be written using various built-in types that define their behavi
52
53
53
54
### Example
54
55
56
+
```python
55
57
x: int=3
56
58
y: float=1.0
57
59
frozen: bool=True
58
60
name: str="Rupert"
61
+
```
59
62
60
63
## What are Type Hints?
61
64
@@ -72,16 +75,19 @@ Type hints let you declare the expected types of variables, function parameters,
72
75
73
76
### Examples
74
77
75
-
#### Correct
78
+
#### Example 1: Correct
79
+
```python
76
80
name: str=""
77
81
78
82
defgreet(name: str) -> str:
79
83
"""Return a personalized greeting."""
80
84
returnf"Hello, {name}!"
81
85
82
86
print(greet("Beatrice"))
87
+
```
83
88
84
-
#### Incorrect
89
+
#### Example 2: Incorrect
90
+
```python
85
91
name: str=""
86
92
87
93
defgreet(name: str) -> str:
@@ -97,6 +103,7 @@ def greet(name: str) -> str:
97
103
returnf"Hello, {name}!"
98
104
99
105
print(greet("Beatrice"))
106
+
```
100
107
101
108
## Why Type Hint?
102
109
@@ -113,8 +120,9 @@ Additionally, type hints unlock the use of static analysis tools (like MyPy), al
113
120
114
121
## Typing Using the `typing` Module
115
122
116
-
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:
123
+
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:
117
124
125
+
```python
118
126
from typing import (
119
127
Any, # “escape hatch” for unknown types
120
128
Union, # still available, though often replaced by |
@@ -131,8 +139,10 @@ from typing import (
131
139
ParamSpec, Concatenate, # for decorator/dependent signatures
def_query_hbar_balance function takes account as an argument, which is of custom type AccountId, then the function returns type None. It returns None because there is no return statement in the function, and it instead prints.
192
+
query_hbar_balance function takes account as an argument, which is of custom type AccountId, then the function returns type None. It returns None because there is no return statement in the function, and it instead prints.
180
193
181
194
### Example 2
195
+
```python
182
196
from dataclasses import dataclass
183
197
from hiero_sdk import AccountId
184
198
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:
249
+
By default, MyPy is a fairly strict type checker. This project includes a `mypy.ini` at `/mypy.ini` to facilitate gradual, module-by-module checking. Customize mypy.ini it to suit your needs:
229
250
230
251
```ini
231
252
# Do not error on imports that lack type stubs
@@ -236,6 +257,7 @@ implicit_optional = True
236
257
237
258
# Suppress errors when calling functions without type annotations
238
259
allow_untyped_calls = True
260
+
```
239
261
240
262
For a full list of flags and options, see the MyPy command-line reference:
0 commit comments