|
| 1 | +# Python Typing Guide |
| 2 | + |
| 3 | +A beginner-friendly guide to Python type hints and typing concepts commonly used in modern Python projects. |
| 4 | + |
| 5 | +This document focuses on the typing features frequently used in 42 Python modules and real-world applications. |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +# Why Use Typing? |
| 10 | + |
| 11 | +Type hints improve: |
| 12 | + |
| 13 | +- Code readability |
| 14 | +- Error detection |
| 15 | +- IDE autocompletion |
| 16 | +- Project maintainability |
| 17 | +- Static analysis with `mypy` |
| 18 | + |
| 19 | +Typing makes code easier to understand and safer to maintain. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +# Basic Type Hints |
| 24 | + |
| 25 | +## Example |
| 26 | + |
| 27 | +```python |
| 28 | +name: str = "Sara" |
| 29 | +age: int = 20 |
| 30 | +height: float = 1.70 |
| 31 | +is_active: bool = True |
| 32 | +``` |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +# Function Type Hints |
| 37 | + |
| 38 | +## Example |
| 39 | + |
| 40 | +```python |
| 41 | +def add(a: int, b: int) -> int: |
| 42 | + return a + b |
| 43 | +``` |
| 44 | + |
| 45 | +### Explanation |
| 46 | + |
| 47 | +- `a: int` → parameter must be an integer |
| 48 | +- `-> int` → function returns an integer |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +# list[str] |
| 53 | + |
| 54 | +Used for lists containing only strings. |
| 55 | + |
| 56 | +## Example |
| 57 | + |
| 58 | +```python |
| 59 | +names: list[str] = ["Sara", "John", "Alice"] |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +# dict[str, int] |
| 65 | + |
| 66 | +Dictionary where: |
| 67 | +- keys are strings |
| 68 | +- values are integers |
| 69 | + |
| 70 | +## Example |
| 71 | + |
| 72 | +```python |
| 73 | +student_scores: dict[str, int] = { |
| 74 | + "Sara": 18, |
| 75 | + "John": 15 |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +--- |
| 80 | + |
| 81 | +# tuple[int, int] |
| 82 | + |
| 83 | +Tuple with two integers. |
| 84 | + |
| 85 | +Very common in: |
| 86 | +- game development |
| 87 | +- coordinates |
| 88 | +- maze projects |
| 89 | +- pathfinding systems |
| 90 | + |
| 91 | +## Example |
| 92 | + |
| 93 | +```python |
| 94 | +position: tuple[int, int] = (5, 10) |
| 95 | +``` |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +# Optional |
| 100 | + |
| 101 | +`Optional` means a value can be: |
| 102 | +- a specific type |
| 103 | +- or `None` |
| 104 | + |
| 105 | +## Example |
| 106 | + |
| 107 | +```python |
| 108 | +from typing import Optional |
| 109 | + |
| 110 | +nickname: Optional[str] = None |
| 111 | +``` |
| 112 | + |
| 113 | +Equivalent to: |
| 114 | + |
| 115 | +```python |
| 116 | +nickname: str | None = None |
| 117 | +``` |
| 118 | + |
| 119 | +--- |
| 120 | + |
| 121 | +# Union |
| 122 | + |
| 123 | +`Union` allows multiple possible types. |
| 124 | + |
| 125 | +## Example |
| 126 | + |
| 127 | +```python |
| 128 | +from typing import Union |
| 129 | + |
| 130 | +value: Union[int, str] |
| 131 | +``` |
| 132 | + |
| 133 | +Equivalent to: |
| 134 | + |
| 135 | +```python |
| 136 | +value: int | str |
| 137 | +``` |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +# Any |
| 142 | + |
| 143 | +`Any` disables type checking for a variable. |
| 144 | + |
| 145 | +## Example |
| 146 | + |
| 147 | +```python |
| 148 | +from typing import Any |
| 149 | + |
| 150 | +data: Any |
| 151 | +``` |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +# Callable |
| 156 | + |
| 157 | +`Callable` describes functions as types. |
| 158 | + |
| 159 | +## Example |
| 160 | + |
| 161 | +```python |
| 162 | +from collections.abc import Callable |
| 163 | + |
| 164 | +operation: Callable[[int, int], int] |
| 165 | +``` |
| 166 | + |
| 167 | +Meaning: |
| 168 | +- accepts two integers |
| 169 | +- returns one integer |
| 170 | + |
| 171 | +--- |
| 172 | + |
| 173 | +# Callable Function Example |
| 174 | + |
| 175 | +```python |
| 176 | +from collections.abc import Callable |
| 177 | + |
| 178 | +def calculate( |
| 179 | + a: int, |
| 180 | + b: int, |
| 181 | + operation: Callable[[int, int], int] |
| 182 | +) -> int: |
| 183 | + |
| 184 | + return operation(a, b) |
| 185 | +``` |
| 186 | + |
| 187 | +--- |
| 188 | + |
| 189 | +# Nested Typing |
| 190 | + |
| 191 | +Typing structures can be combined. |
| 192 | + |
| 193 | +## Example |
| 194 | + |
| 195 | +```python |
| 196 | +maze: list[list[int]] |
| 197 | +``` |
| 198 | + |
| 199 | +A list containing lists of integers. |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +# Complex Example |
| 204 | + |
| 205 | +```python |
| 206 | +player_positions: dict[str, tuple[int, int]] |
| 207 | +``` |
| 208 | + |
| 209 | +Dictionary: |
| 210 | +- key → player name |
| 211 | +- value → `(x, y)` coordinates |
| 212 | + |
| 213 | +--- |
| 214 | + |
| 215 | +# Common 42 Examples |
| 216 | + |
| 217 | +## Maze coordinates |
| 218 | + |
| 219 | +```python |
| 220 | +position: tuple[int, int] |
| 221 | +``` |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +## BFS queue |
| 226 | + |
| 227 | +```python |
| 228 | +queue: list[tuple[int, int]] |
| 229 | +``` |
| 230 | + |
| 231 | +--- |
| 232 | + |
| 233 | +## Graph structure |
| 234 | + |
| 235 | +```python |
| 236 | +graph: dict[str, list[str]] |
| 237 | +``` |
| 238 | + |
| 239 | +--- |
| 240 | + |
| 241 | +# Type Aliases |
| 242 | + |
| 243 | +Useful for improving readability. |
| 244 | + |
| 245 | +## Example |
| 246 | + |
| 247 | +```python |
| 248 | +Position = tuple[int, int] |
| 249 | + |
| 250 | +player_position: Position |
| 251 | +enemy_position: Position |
| 252 | +``` |
| 253 | + |
| 254 | +--- |
| 255 | + |
| 256 | +# Static Type Checking with mypy |
| 257 | + |
| 258 | +`mypy` checks typing without running the code. |
| 259 | + |
| 260 | +## Run mypy |
| 261 | + |
| 262 | +```bash |
| 263 | +mypy . |
| 264 | +``` |
| 265 | + |
| 266 | +--- |
| 267 | + |
| 268 | +# Common mypy Errors |
| 269 | + |
| 270 | +## Missing return statement |
| 271 | + |
| 272 | +```python |
| 273 | +def get_value() -> int: |
| 274 | + pass |
| 275 | +``` |
| 276 | + |
| 277 | +--- |
| 278 | + |
| 279 | +## Incompatible types |
| 280 | + |
| 281 | +```python |
| 282 | +age: int = "hello" |
| 283 | +``` |
| 284 | + |
| 285 | +--- |
| 286 | + |
| 287 | +# Best Practices |
| 288 | + |
| 289 | +- Avoid unnecessary `Any` |
| 290 | +- Use precise types |
| 291 | +- Use `Optional` when needed |
| 292 | +- Keep function return types explicit |
| 293 | +- Prefer readable typing over overly complex typing |
| 294 | + |
| 295 | +--- |
| 296 | + |
| 297 | +# Summary Table |
| 298 | + |
| 299 | +| Typing Feature | Purpose | |
| 300 | +|---|---| |
| 301 | +| list[str] | List of strings | |
| 302 | +| dict[str, int] | Dictionary with string keys and integer values | |
| 303 | +| tuple[int, int] | Tuple with two integers | |
| 304 | +| Optional[T] | Type or None | |
| 305 | +| Union[A, B] | Multiple possible types | |
| 306 | +| Any | Disables type checking | |
| 307 | +| Callable | Function as a type | |
| 308 | + |
| 309 | +--- |
| 310 | + |
| 311 | +# Final Notes |
| 312 | + |
| 313 | +Typing is not only about satisfying `mypy`. |
| 314 | + |
| 315 | +Good typing: |
| 316 | +- documents code |
| 317 | +- improves maintainability |
| 318 | +- helps teamwork |
| 319 | +- reduces bugs |
| 320 | +- makes large projects easier to manage |
| 321 | + |
| 322 | +--- |
0 commit comments