Skip to content

Commit 67949f4

Browse files
committed
Updates text also adds the talk video
1 parent 1246f2b commit 67949f4

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

docs/typehinting.rst

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,32 @@ We can install mypy inside of a virtual environment.
5656

5757
::
5858

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
7078

7179

7280
Our example code
7381
-----------------
7482

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.
7685

7786
::
7887

@@ -116,7 +125,8 @@ We wil working on the following example code. This does not do much useful thing
116125
print("Passed: {0}. The toral score of {1} is {2}".format(std.is_passed(), std.name, std.total_score()))
117126

118127

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.
120130

121131
Using mypy
122132
-----------
@@ -137,7 +147,7 @@ code length, I am only showing the changed code below.
137147

138148
class Student:
139149

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:
141151
self.name = name
142152
self.batch = batch
143153
self.branch = branch
@@ -152,7 +162,7 @@ code length, I am only showing the changed code below.
152162
students2.py:11: error: Need type annotation for variable
153163
students2.py:31: error: Argument 4 to "Student" has incompatible type "str"; expected "int"
154164

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
156166
annotations, and also found that in line 31, as argument 4 we are passing
157167
*str*, where as we were supposed to send in an Integer for the rull number.
158168
Let us fix these.
@@ -163,7 +173,7 @@ Let us fix these.
163173

164174
class Student:
165175

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:
167177
self.name = name
168178
self.batch = batch
169179
self.branch = branch
@@ -262,7 +272,7 @@ More examples of type annotations
262272
print_all([1,2,3])
263273
print_all({"name": "kushal", "class": 5})
264274
# 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
266276

267277
def add_ten(number: Optional[int] = None) -> int:
268278
if number:
@@ -273,4 +283,10 @@ More examples of type annotations
273283
print(add_ten())
274284
print(add_ten(12))
275285

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

Comments
 (0)