@@ -56,23 +56,32 @@ We can install mypy inside of a virtual environment.
56
56
57
57
::
58
58
59
- $ python3 -m venv env
60
- $ source env/bin/activate
61
- (env) $ pip install mypy
62
- Collecting mypy
63
- Downloading mypy-0.511-py3-none-any.whl (1.0MB)
64
- 100% |################################| 1.0MB 965kB/s
65
- Collecting typed-ast<1.1.0,>=1.0.3 (from mypy)
66
- Downloading typed_ast-1.0.3-cp36-cp36m-macosx_10_11_x86_64.whl (214kB)
67
- 100% |################################| 215kB 682kB/s
68
- Installing collected packages: typed-ast, mypy
69
- Successfully installed mypy-0.511 typed-ast-1.0.3
59
+ $ pipenv install mypy
60
+ Installing mypy…
61
+ Looking in indexes: https://pypi.python.org/simple
62
+ Collecting mypy
63
+ Downloading https://files.pythonhosted.org/packages/e2/3f/e20e2544b35e862fbed4e26a89e3d857007c5bd32abc019ef21c02aecd98/mypy-0.600-py3-none-any.whl (1.3MB)
64
+ Collecting typed-ast<1.2.0,>=1.1.0 (from mypy)
65
+ Downloading https://files.pythonhosted.org/packages/5b/4e/79e873aa89b8038ca6474c00afe96f9468973b604e7f737cb82697a680c0/typed_ast-1.1.0-cp35-cp35m-manylinux1_x86_64.whl (724kB)
66
+ Installing collected packages: typed-ast, mypy
67
+ Successfully installed mypy-0.600 typed-ast-1.1.0
68
+
69
+ Adding mypy to Pipfile's [packages]…
70
+ Pipfile.lock (627f99) out of date, updating to (67e074)…
71
+ Locking [dev-packages] dependencies…
72
+ Locking [packages] dependencies…
73
+ Updated Pipfile.lock (67e074)!
74
+ Installing dependencies from Pipfile.lock (67e074)…
75
+ 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 7/7 — 00:00:01
76
+ To activate this project's virtualenv, run the following:
77
+ $ pipenv shell
70
78
71
79
72
80
Our example code
73
81
-----------------
74
82
75
- We wil working on the following example code. This does not do much useful things, but we can use this to learn about type annotations and mypy.
83
+ We wil working on the following example code. This does not do much useful
84
+ things, but we can use this to learn about type annotations and mypy.
76
85
77
86
::
78
87
@@ -116,7 +125,8 @@ We wil working on the following example code. This does not do much useful thing
116
125
print("Passed: {0}. The toral score of {1} is {2}".format(std.is_passed(), std.name, std.total_score()))
117
126
118
127
119
- You may find some errors in the code, but in case of a large codebase we can not detect the similar issues unless we see the runtime errors.
128
+ You may find some errors in the code, but in case of a large codebase we can
129
+ not detect the similar issues unless we see the runtime errors.
120
130
121
131
Using mypy
122
132
-----------
@@ -137,7 +147,7 @@ code length, I am only showing the changed code below.
137
147
138
148
class Student:
139
149
140
- def __init__(self, name: str, batch: int, branch: str, roll: int) -> None:
150
+ def __init__(self, name: str, batch: int, branch: str, roll: int) -> None:
141
151
self.name = name
142
152
self.batch = batch
143
153
self.branch = branch
@@ -152,7 +162,7 @@ code length, I am only showing the changed code below.
152
162
students2.py:11: error: Need type annotation for variable
153
163
students2.py:31: error: Argument 4 to "Student" has incompatible type "str"; expected "int"
154
164
155
- You can see mypy is complaing about variable which does not have type
165
+ You can see mypy is complaining about variable which does not have type
156
166
annotations, and also found that in line 31, as argument 4 we are passing
157
167
*str *, where as we were supposed to send in an Integer for the rull number.
158
168
Let us fix these.
@@ -163,7 +173,7 @@ Let us fix these.
163
173
164
174
class Student:
165
175
166
- def __init__(self, name: str, batch: int, branch: str, roll: int) -> None:
176
+ def __init__(self, name: str, batch: int, branch: str, roll: int) -> None:
167
177
self.name = name
168
178
self.batch = batch
169
179
self.branch = branch
@@ -262,7 +272,7 @@ More examples of type annotations
262
272
print_all([1,2,3])
263
273
print_all({"name": "kushal", "class": 5})
264
274
# alltypes.py:23: error: Argument 1 to "print_all" has incompatible type Dict[str, object]; expected Sequence[Any]
265
- # But running the code will give us no error with wrong output
275
+ # But running the code will give us no error with wrong output
266
276
267
277
def add_ten(number: Optional[int] = None) -> int:
268
278
if number:
@@ -273,4 +283,10 @@ More examples of type annotations
273
283
print(add_ten())
274
284
print(add_ten(12))
275
285
276
- You can learn more about types from `PEP 484 <https://www.python.org/dev/peps/pep-0484/ >`_.
286
+ You can learn more about types from `PEP 484
287
+ <https://www.python.org/dev/peps/pep-0484/> `_. The `typing module
288
+ <https://docs.python.org/3/library/typing.html> `_ has detailed explanation and
289
+ more examples about how to add type annotations in your codebase.
290
+
291
+ You can also view `the talk <https://www.youtube.com/watch?v=pMgmKJyWKn8 >`_
292
+ from Carl Meyer to learn about type checking in Python.
0 commit comments