-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslides.html
197 lines (182 loc) · 10.7 KB
/
slides.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>README</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="https://unpkg.com/reveal.js@^4//dist/reset.css">
<link rel="stylesheet" href="https://unpkg.com/reveal.js@^4//dist/reveal.css">
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<link rel="stylesheet" href="https://unpkg.com/reveal.js@^4//dist/theme/black.css" id="theme">
</head>
<body>
<div class="reveal">
<div class="slides">
<section id="a-brief-introduction-to-mypy" class="slide level1">
<h1>A brief introduction to MyPy</h1>
<p>Disclaimer: I wrote this to crystalize my own learning. I don’t claim to be an expert.</p>
</section>
<section class="slide level1">
<h2 id="synopsis">Synopsis</h2>
<p>A brief introduction to MyPy.</p>
<p>Contents:</p>
<ul>
<li>Why bother with MyPy.</li>
<li>Two example scripts.</li>
<li>Two exercise scripts.</li>
</ul>
</section>
<section class="slide level1">
<h2 id="why-bother-with-mypy">Why Bother With MyPy</h2>
<p>Python is dynamically typed, but sometimes this can make life harder.</p>
</section>
<section class="slide level1">
<h3 id="allows-you-to-spot-bugs-sooner">Allows you to spot bugs sooner:</h3>
<p>Debugging is easy in this trivial example, but in a bigger codebase adding typechecking to this function would reduce risk of accidentally calling it with args of the wrong type:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true"></a><span class="kw">def</span> get_reachable_hosts(allhosts, badhosts):</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true"></a> <span class="co">"""Do something for every host that you can</span></span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true"></a><span class="co"> reach from a list of hosts.</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true"></a><span class="co"> """</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true"></a> goodhosts <span class="op">=</span> allhosts <span class="op">-</span> badhosts</span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true"></a> <span class="cf">for</span> goodhost <span class="kw">in</span> goodhosts:</span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true"></a> ... <span class="co"># do stuff</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true"></a>get_reachable_hosts([<span class="st">'foo'</span>, <span class="st">'bar'</span>, <span class="st">'baz'</span>], <span class="bu">set</span>(<span class="st">'foo'</span>))</span></code></pre></div>
</section>
<section class="slide level1">
<h3 id="programatically-ok-intended-usage">Programatically OK ≠ Intended Usage:</h3>
<p>Classic example is:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true"></a><span class="kw">def</span> greet(name):</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true"></a> <span class="bu">print</span>(<span class="ss">f'Hello </span><span class="sc">{</span>name<span class="sc">}</span><span class="ss">'</span>)</span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true"></a></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true"></a>greet({<span class="dv">42</span>: <span class="st">'Deep thought'</span>})</span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true"></a><span class="co"># Python handles this</span></span>
<span id="cb2-6"><a href="#cb2-6" aria-hidden="true"></a><span class="co"># even if it's not sensible.</span></span>
<span id="cb2-7"><a href="#cb2-7" aria-hidden="true"></a><span class="op">>>></span> Hello {<span class="dv">42</span>: <span class="st">'Deep thought'</span>}</span></code></pre></div>
</section>
<section class="slide level1">
<h3 id="sets-typing-in-the-code-rather-than-the-docstring">Sets typing in the code rather than the docstring:</h3>
<p>Docstring relies on human proofing for being up to date.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true"></a><span class="kw">def</span> function_has_evolved(input_):</span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true"></a> <span class="co">"""Do something to a string.</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true"></a><span class="co"> Args (str): Do something to a string</span></span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true"></a><span class="co"> """</span></span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true"></a> <span class="cf">return</span> [func(i) <span class="cf">for</span> i <span class="kw">in</span> input_]</span></code></pre></div>
</section>
<section class="slide level1">
<p>Ultimately though, MyPy is optional. You can have both static and dynamic typing for different bits of the same script!</p>
</section>
<section class="slide level1">
<h2 id="how-to-get-a-copy-of-this-repo">How to get a copy of this repo:</h2>
<p>Either:</p>
<ol type="1">
<li><a href="https://github.com/wxtim/learn-mypy/archive/refs/heads/master.zip">Use this link to download and unzip</a> in a location of your choice.</li>
<li>On the command line: <code>git clone https://github.com/wxtim/learn-mypy.git</code>. Then <code>cd learn-mypy</code>.</li>
</ol>
</section>
<section class="slide level1">
<h2 id="examples-of-mypy">Examples of MyPy</h2>
<ol type="1">
<li><a href="01.basic_syntax.py">Brief introduction to the syntax.</a> (<a href=".answers/01.basic_syntax.py.diff">Answers</a>)</li>
<li><a href="02.real_world_example.py">An example where type annotation would have made my life better.</a> (<a href=".answers/02.real_world_example.py.diff">Answers</a>)</li>
<li><a href="03.broken_function.py">Exercise 1: Make a broken function fail MyPy validation</a> (<a href="answers/03.broken_function.py.diff">Answers</a>)</li>
<li><a href="04.add_typing.py">Exercise 2: Use MyPy on some example legacy code.</a> (<a href=".answers/04.add_typing.py.diff">Answers</a>)</li>
</ol>
</section>
</div>
</div>
<script src="https://unpkg.com/reveal.js@^4//dist/reveal.js"></script>
// reveal.js plugins
<script src="https://unpkg.com/reveal.js@^4//plugin/notes/notes.js"></script>
<script src="https://unpkg.com/reveal.js@^4//plugin/search/search.js"></script>
<script src="https://unpkg.com/reveal.js@^4//plugin/zoom/zoom.js"></script>
<script>
// Full list of configuration options available at:
// https://revealjs.com/config/
Reveal.initialize({
// Push each slide change to the browser history
history: true,
// reveal.js plugins
plugins: [
RevealNotes,
RevealSearch,
RevealZoom
]
});
</script>
</body>
</html>