|
1 |
| -# Typing |
2 |
| -Python 3.5 introducd optional type annotation for functions, and that |
3 |
| -functionality was extended in Python 3.6. |
| 1 | +# Typing Python 3.5 introducd optional type annotation for functions, and |
| 2 | +that functionality was extended in Python 3.6. |
4 | 3 |
|
5 |
| -The `mypy` static type checker can use this annotation to detect type |
6 |
| -errors. |
| 4 | +The `mypy` static type checker can use this annotation to detect type errors. |
7 | 5 |
|
8 | 6 | Type checking can be done using [mypy](http://mypy-lang.org/index.html).
|
9 | 7 |
|
10 | 8 | ## What is it?
|
11 | 9 | 1. `mypy.ini`: mypy configuration file.
|
12 | 10 | 1. `correct.py`: code that has type annotations, and no type errors.
|
13 |
| -1. `incorrect_01.py`: code that has type annotations, and passes a string |
14 |
| - to a function that expects an `int`. |
| 11 | +1. `incorrect_01.py`: code that has type annotations, and passes a string to |
| 12 | + a function that expects an `int`. |
15 | 13 | 1. `incorrect_02.py`: code that has type annotations, and the result of a
|
16 |
| - function that returns an `int` is assigned to a `str` variable. |
| 14 | + function that returns an `int` is assigned to a `str` variable. |
17 | 15 | 1. `incorrect_03.py`: code that has type annotations, and the result of a
|
18 |
| - function that returns an `int`, assigns it to a variable that is |
19 |
| - later used as a `str`. |
20 |
| -1. `dict_correct.py`: code that counts the words in a text read from |
21 |
| - standard input. |
| 16 | + function that returns an `int`, assigns it to a variable that is later |
| 17 | + used as a `str`. |
| 18 | +1. `dict_correct.py`: code that counts the words in a text read from standard |
| 19 | + input. |
22 | 20 | 1. `dict_incorrect.py`: code that counts the words in a text read from
|
23 |
| - standard input. The counts are subsequently normalized to `float`, |
24 |
| - which is a type error. |
25 |
| -1. `people_incorrect.py`: code that defines a `People` class, stores some |
26 |
| - in a list with mistakes. |
| 21 | + standard input. The counts are subsequently normalized to `float`, which |
| 22 | + is a type error. |
| 23 | +1. `people_incorrect.py`: code that defines a `People` class, stores some in |
| 24 | + a list with mistakes. |
27 | 25 | 1. `duck_typing.py`: example code illustrating duck typing.
|
28 | 26 | 1. `duck_typing_incorrect.py`: example code illustrating duck typing, but
|
29 | 27 | with an error.
|
30 |
| -1. `typed_duck_typing.py`: example code illustrating duck typing |
31 |
| - using type hints. |
| 28 | +1. `typed_duck_typing.py`: example code illustrating duck typing using type |
| 29 | + hints. |
| 30 | +1. `typed_duck_typing_clean.py`: example code illustrating duck typing using |
| 31 | + type hints with a factory function. |
32 | 32 | 1. `typed_duck_typing_incorrect.py`: example code illustrating duck typing
|
33 | 33 | using type hints with an error.
|
34 |
| -1. `typed_duck_typing_false_positive.py`: example code illustrating |
35 |
| - duck typing using type hints for which mypy 0.910 generates a |
36 |
| - false positive. |
| 34 | +1. `typed_duck_typing_false_positive.py`: example code illustrating duck |
| 35 | + typing using type hints for which mypy 0.910 generates a false positive. |
37 | 36 | 1. `numpy_typing.py`: illustration of a script using both numpy and
|
38 | 37 | matplotlib with type hints.
|
39 |
| -1. `classes.py`: illustration of using type hints with a user-defined |
40 |
| - class. |
| 38 | +1. `classes.py`: illustration of using type hints with a user-defined class. |
41 | 39 | 1. `classes_incorrect.py`: illustration of using type hints with a
|
42 | 40 | user-defined class with errors.
|
0 commit comments