@@ -50,10 +50,10 @@ On some systems you need to use this instead:
50
50
51
51
$ python -m pip install -U mypy
52
52
53
- Compile and run a program
54
- -------------------------
53
+ Example program
54
+ ---------------
55
55
56
- Let's now compile a classic micro-benchmark, recursive fibonacci. Save
56
+ Let's start with a classic micro-benchmark, recursive fibonacci. Save
57
57
this file as ``fib.py ``:
58
58
59
59
.. code-block :: python
@@ -70,8 +70,8 @@ this file as ``fib.py``:
70
70
fib(32 )
71
71
print (time.time() - t0)
72
72
73
- Note that we gave ``fib `` a type annotation. Without it, performance
74
- won't be as impressive after compilation.
73
+ Note that we gave the ``fib `` function a type annotation. Without it,
74
+ performance won't be as impressive after compilation.
75
75
76
76
.. note ::
77
77
@@ -81,6 +81,9 @@ won't be as impressive after compilation.
81
81
mypy to perform type checking and type inference, so some familiarity
82
82
with mypy is very useful.
83
83
84
+ Compiling and running
85
+ ---------------------
86
+
84
87
We can run ``fib.py `` as a regular, interpreted program using CPython:
85
88
86
89
.. code-block :: console
@@ -114,9 +117,12 @@ After compilation, the program is about 10x faster. Nice!
114
117
115
118
``__name__ `` in ``fib.py `` would now be ``"fib" ``, not ``"__main__" ``.
116
119
120
+ You can also pass most
121
+ `mypy command line options <https://mypy.readthedocs.io/en/stable/command_line.html >`_
122
+ to ``mypyc ``.
117
123
118
- Delete compiled binary
119
- ----------------------
124
+ Deleting compiled binary
125
+ ------------------------
120
126
121
127
You can manually delete the C extension to get back to an interpreted
122
128
version (this example works on Linux):
@@ -125,11 +131,11 @@ version (this example works on Linux):
125
131
126
132
$ rm fib.*.so
127
133
128
- Compile using setup.py
129
- ----------------------
134
+ Using setup.py
135
+ --------------
130
136
131
137
You can also use ``setup.py `` to compile modules using mypyc. Here is an
132
- example::
138
+ example `` setup.py `` file ::
133
139
134
140
from setuptools import setup
135
141
@@ -154,40 +160,67 @@ Now you can build a wheel (.whl) file for the package::
154
160
155
161
The wheel is created under ``dist/ ``.
156
162
163
+ You can also compile the C extensions in-place, in the current directory (similar
164
+ to using ``mypyc `` to compile modules)::
165
+
166
+ python3 setup.py build_ext --inplace
167
+
168
+ You can include most `mypy command line options
169
+ <https://mypy.readthedocs.io/en/stable/command_line.html> `_ in the
170
+ list of arguments passed to ``mypycify() ``. For example, here we use
171
+ the ``--disallow-untyped-defs `` flag to require that all functions
172
+ have type annotations::
173
+
174
+ ...
175
+ setup(
176
+ name='frobnicate',
177
+ packages=['frobnicate'],
178
+ ext_modules=mypycify([
179
+ '--disallow-untyped-defs', # Pass a mypy flag
180
+ 'frobnicate.py',
181
+ ]),
182
+ )
183
+
184
+ .. note:
185
+
186
+ You may be tempted to use `--check-untyped-defs
187
+ <https://mypy.readthedocs.io/en/stable/command_line.html#cmdoption-mypy-check-untyped-defs>`_
188
+ to type check functions without type annotations. Note that this
189
+ may reduce performance, due to many transitions between type-checked and unchecked
190
+ code.
191
+
157
192
Recommended workflow
158
193
--------------------
159
194
160
195
A simple way to use mypyc is to always compile your code after any
161
- code changes, but this can get tedious. Instead, you may prefer
162
- another workflow, where you compile code less often. The following
163
- development workflow has worked very well for developing mypy and
164
- mypyc, and we recommend that you to try it out:
196
+ code changes, but this can get tedious, especially if you have a lot
197
+ of code. Instead, you can do most development in interpreted mode.
198
+ This development workflow has worked smoothly for developing mypy and
199
+ mypyc (often we forget that we aren't working on a vanilla Python
200
+ project):
165
201
166
- 1. During development, use interpreted mode. This allows a very fast
167
- edit-run cycle, since you don't need to wait for mypyc compilation .
202
+ 1. During development, use interpreted mode. This gives you a fast
203
+ edit-run cycle.
168
204
169
205
2. Use type annotations liberally and use mypy to type check your code
170
206
during development. Mypy and tests can find most errors that would
171
- break your compiled version, if you have good annotation
172
- coverage. (Running mypy is faster than compiling, and you can run
173
- your code even if there are mypy errors.)
207
+ break your compiled code, if you have good type annotation
208
+ coverage. (Running mypy is pretty quick.)
174
209
175
210
3. After you've implemented a feature or a fix, compile your project
176
- and run tests again, now in compiled mode. Almost always, nothing
177
- will break here, if your type annotation coverage is good
178
- enough. This can happen locally or as part of a Continuous
179
- Integration (CI) job. If you have good CI, compiling locally may be
180
- rarely needed.
211
+ and run tests again, now in compiled mode. Usually nothing will
212
+ break here, assuming your type annotation coverage is good. This
213
+ can happen locally or in a Continuous Integration (CI) job. If you
214
+ have CI, compiling locally may be rarely needed.
181
215
182
216
4. Release or deploy a compiled version. Optionally, include a
183
217
fallback interpreted version for platforms that mypyc doesn't
184
218
support.
185
219
186
- This way of using mypyc has minimal impact on your productivity and
187
- requires only minor adjustments to a typical Python workflow. Most of
188
- development, testing and debugging happens in interpreted
189
- mode. Incremental mypy runs, especially when using mypy daemon, are
190
- very quick (often a few hundred milliseconds).
220
+ This mypyc workflow only involves minor tweaks to a typical Python
221
+ workflow. Most of development, testing and debugging happens in
222
+ interpreted mode. Incremental mypy runs, especially when using the
223
+ mypy daemon, are very quick (often a few hundred milliseconds).
191
224
192
225
Next steps
193
226
----------
0 commit comments