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
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.
214
+
We use docstrings to describe data types that we pass into functions as parameters or
215
+
into classes as attributes. *We do it with our users in mind.*
216
216
217
-
What with us – developers? We can think of ourselves and the new contributors
217
+
**What with us – developers?** We can think of ourselves and the new contributors,
218
218
and start using *type hinting* to make our journey safer!
219
219
220
220
There are solid reasons why to use type hints:
221
221
222
-
- Development and debugging is faster,
222
+
- Development and debugging are faster,
223
223
- We clearly see data flow and its transformations,
224
224
- We can use tools like `mypy` or integrated tools of Python IDEs for static type checking and code debugging.
225
225
@@ -230,22 +230,22 @@ The icing on the cake is that the code in our package will be aligned with the b
230
230
But there are reasons to *skip* type hinting:
231
231
232
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.
233
+
-Writing type hints for simple scripts and functions that perform obvious operations don't make sense.
234
234
235
235
Fortunately for us, type hinting is not all black and white.
236
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.
237
+
Type hinting can be a task for new contributors to get them used to the package structure.
238
+
That way, their learning curve about data flow and dependencies between API endpoints will be smoother.
239
239
240
240
## Type hints in practice
241
241
242
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
243
+
**PEP 484** defines the scope of type hinting. Is Python drifting towards compiled languages with this feature?
244
+
It is not. Type hints are optional and static. They will work like that in the future until 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
246
throughout development.
247
247
248
-
We've seen type hints in the simple example earlier, and we will change it slightly:
248
+
We've seen type hints in the simple example earlier. Let's come back to it and change it slightly:
249
249
250
250
251
251
```python
@@ -254,23 +254,28 @@ from typing import Dict, List
254
254
255
255
defextent_to_json(ext_obj: List) -> Dict:
256
256
"""Convert bounds to a shapely geojson like spatial object."""
257
-
pass
257
+
...
258
+
258
259
```
259
260
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
+
Here we focus on the new syntax. First, we described the parameter `ext_obj` as the `List` class. How do we do it?
262
+
Add a colon after the parameter (variable) and the name of a class that is passed into a function.
263
+
It’s not over. Do you see, that the function definition after closing parenthesis is expanded?
264
+
If we want to inform the type checker what the function returns, then we create the arrow sign `->` that points to a returned type,
265
+
and after it, we put the function’s colon. Our function returns a Python dictionary (`Dict`).
261
266
262
267
```{note}
263
-
We have exported classes `List` and `Dict` from `typing` module but we may use
268
+
We have exported classes `List` and `Dict` from the `typing` module, but we may use
264
269
`list` or `dict` keywords instead. We will achieve the same result.
265
270
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.
271
+
Python 3.9. Python 3.7 will be deprecated in June 2023, and Python 3.8 in October 2024.
272
+
Thus, if your package supports the whole ecosystem, it should use the `typing` module syntax.
268
273
```
269
274
270
275
### Type hints: basic example
271
276
272
277
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:
278
+
To avoid confusion, we start with a mathematical operation:
The second solution is to think what is a highlevel representation of passed data types. The examples are:
369
+
The second solution is to think what is a high-level representation of a passed data type. The examples are:
365
370
366
-
-`Sequence` – we can use it to describe a variable that is a sequence of elements. Sequential are `list`, `tuple`, `range` and `str`.
367
-
-`Iterable` – we can use it to describe a variable that is iterable. Iterables are `list`, `tuple`, `range`, `str`, `dict` and `set`.
371
+
-`Sequence` – we can use it to describe a variable as a sequence of elements. Sequential are `list`, `tuple`, `range` and `str`.
372
+
-`Iterable` – we can use it to describe an iterable variable. Iterables are `list`, `tuple`, `range`, `str`, `dict` and `set`.
368
373
-`Mapping` – we can use it to describe a variable that is a mapping. Mappings are `dict` and `defaultdict`.
369
-
-`Hashable` – we can use it to describe a variable that is hashable. Hashables are `int`, `float`, `str`, `tuple` and `frozenset`.
370
-
-`Collection` - we can use it to describe a variable that is a collection. Collections are `list`, `tuple`, `range`, `str`, `dict`, `set` and `frozenset`.
374
+
-`Hashable` – we can use it to describe a hashable variable. Hashables are `int`, `float`, `str`, `tuple` and `frozenset`.
375
+
-`Collection` - we can use it to describe a collection variable. Collections are `list`, `tuple`, `range`, `str`, `dict`, `set` and `frozenset`.
ImportError: cannot import name 'my_data_class' from partially initialized module 'my_package' (most likely due to a circular import) (/home/user/my_package/__init__.py)
432
436
```
433
437
434
-
Then you should use `typing.TYPE_CHECKING` clause to avoid circular imports. The example:
438
+
Then you should use the `typing.TYPE_CHECKING` clause to avoid circular imports. The example:
0 commit comments