Skip to content

Commit ffdbeb3

Browse files
authored
[mypyc] Various documentation updates (#9098)
Includes many changes (this list is not exhaustive): * Document how to pass mypy options. * Document some additional primitives. * Protocols are now supported. * General wordsmithing.
1 parent b98c47e commit ffdbeb3

File tree

5 files changed

+145
-123
lines changed

5 files changed

+145
-123
lines changed

mypyc/doc/dict_operations.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ Statements
3333
----------
3434

3535
* ``d[key] = value``
36+
* ``for key in d:``
3637

3738
Methods
3839
-------
3940

4041
* ``d.get(key)``
4142
* ``d.get(key, default)``
43+
* ``d.keys()``
44+
* ``d.values()``
45+
* ``d.items()``
4246
* ``d1.update(d2: dict)``
4347
* ``d.update(x: Iterable)``
4448

mypyc/doc/differences_from_python.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,6 @@ Descriptors
262262
Native classes can't contain arbitrary descriptors. Properties, static
263263
methods and class methods are supported.
264264

265-
Defining protocols
266-
******************
267-
268-
Protocols can't be defined in compiled code. You can use protocols
269-
defined elsewhere, however.
270-
271265
Stack introspection
272266
*******************
273267

mypyc/doc/getting_started.rst

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ On some systems you need to use this instead:
5050
5151
$ python -m pip install -U mypy
5252
53-
Compile and run a program
54-
-------------------------
53+
Example program
54+
---------------
5555

56-
Let's now compile a classic micro-benchmark, recursive fibonacci. Save
56+
Let's start with a classic micro-benchmark, recursive fibonacci. Save
5757
this file as ``fib.py``:
5858

5959
.. code-block:: python
@@ -70,8 +70,8 @@ this file as ``fib.py``:
7070
fib(32)
7171
print(time.time() - t0)
7272
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.
7575

7676
.. note::
7777

@@ -81,6 +81,9 @@ won't be as impressive after compilation.
8181
mypy to perform type checking and type inference, so some familiarity
8282
with mypy is very useful.
8383

84+
Compiling and running
85+
---------------------
86+
8487
We can run ``fib.py`` as a regular, interpreted program using CPython:
8588

8689
.. code-block:: console
@@ -114,9 +117,12 @@ After compilation, the program is about 10x faster. Nice!
114117

115118
``__name__`` in ``fib.py`` would now be ``"fib"``, not ``"__main__"``.
116119

120+
You can also pass most
121+
`mypy command line options <https://mypy.readthedocs.io/en/stable/command_line.html>`_
122+
to ``mypyc``.
117123

118-
Delete compiled binary
119-
----------------------
124+
Deleting compiled binary
125+
------------------------
120126

121127
You can manually delete the C extension to get back to an interpreted
122128
version (this example works on Linux):
@@ -125,11 +131,11 @@ version (this example works on Linux):
125131
126132
$ rm fib.*.so
127133
128-
Compile using setup.py
129-
----------------------
134+
Using setup.py
135+
--------------
130136

131137
You can also use ``setup.py`` to compile modules using mypyc. Here is an
132-
example::
138+
example ``setup.py`` file::
133139

134140
from setuptools import setup
135141

@@ -154,40 +160,67 @@ Now you can build a wheel (.whl) file for the package::
154160

155161
The wheel is created under ``dist/``.
156162

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+
157192
Recommended workflow
158193
--------------------
159194

160195
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):
165201

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

169205
2. Use type annotations liberally and use mypy to type check your code
170206
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.)
174209

175210
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.
181215

182216
4. Release or deploy a compiled version. Optionally, include a
183217
fallback interpreted version for platforms that mypyc doesn't
184218
support.
185219

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).
191224

192225
Next steps
193226
----------

0 commit comments

Comments
 (0)