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: documentation/write-user-documentation/document-your-code-api-docstrings.md
+104Lines changed: 104 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -207,3 +207,107 @@ def add_me(aNum, aNum2):
207
207
208
208
209
209
```
210
+
211
+
212
+
## Beyond docstrings: type hints
213
+
214
+
We can use docstrings to describe data types that we pass into functions as parameters or
215
+
into classes as attributes. We do it with package users in mind.
216
+
217
+
What with us – developers? We can think of ourselves and the new contributors
218
+
and start using *type hinting* to make our journey safer!
219
+
220
+
There are solid reasons why to use type hints:
221
+
222
+
- Development and debugging is faster,
223
+
- We clearly see data flow and its transformations,
224
+
- We can use tools like `mypy` or integrated tools of Python IDEs for static type checking and code debugging.
225
+
226
+
We should consider type hinting if our package performs data processing,
227
+
functions require complex inputs, and we want to lower the entrance barrier for our contributors.
228
+
The icing on the cake is that the code in our package will be aligned with the best industry standards.
229
+
230
+
But there are reasons to *skip* type hinting:
231
+
232
+
- Type hints may make code unreadable, especially when a parameter’s input takes multiple data types and we list them all,
233
+
- It doesn’t make sense to write type hints for simple scripts and functions that perform obvious operations.
234
+
235
+
Fortunately for us, type hinting is not all black and white.
236
+
We can gradually describe the parameters and outputs of some functions but leave others as they are.
237
+
Type hinting can be an introductory task for new contributors in seasoned packages,
238
+
that way their learning curve about data flow and dependencies between API endpoints will be smoother.
239
+
240
+
## Type hints in practice
241
+
242
+
Type hinting was introduced with Python 3.5 and is described in [PEP 484](https://peps.python.org/pep-0484/).
243
+
**PEP 484** defines the scope of type hinting. Is Python drifting towards compiled languages with type hinting?
244
+
It is not. Type hints are optional and static and they will work like that in the future where Python is Python.
245
+
The power of type hints lies somewhere between docstrings and unit tests, and with it we can avoid many bugs
246
+
throughout development.
247
+
248
+
We've seen type hints in the simple example earlier, and we will change it slightly:
249
+
250
+
251
+
```python
252
+
from typing import Dict, List
253
+
254
+
255
+
defextent_to_json(ext_obj: List) -> Dict:
256
+
"""Convert bounds to a shapely geojson like spatial object."""
257
+
pass
258
+
```
259
+
260
+
Here we focus on the new syntax. First, we have described the parameter `ext_obj` as the `List` class. How do we do it? By adding a colon after parameter and the name of a class that is passed into a function. It’s not over and we see that the function definition after closing parenthesis is expanded. If we want to inform type checker what type function returns, then we create the arrow sign `->` that points to a returned type and after it we have function’s colon. Our function returns Python dictonray (`Dict`).
261
+
262
+
```{note}
263
+
We have exported classes `List` and `Dict` from `typing` module but we may use
264
+
`list` or `dict` keywords instead. We will achieve the same result.
265
+
Capitalized keywords are required when our package uses Python versions that are lower than
266
+
Python 3.9. Python 3.7 will be deprecated in June 2023, Python 3.8 in October 2024.
267
+
Thus, if your package supports the whole ecosystem, it should use `typing` module syntax.
268
+
```
269
+
270
+
### Type hints: basic example
271
+
272
+
The best way to learn is by example. We will use the [pystiche](https://github.com/pystiche/pystiche/tree/main) package.
273
+
To avoid confusion, we start from a mathematical operation with basic data types:
0 commit comments