Skip to content

Commit 959b499

Browse files
committed
Update documentation for modular typeshed
1 parent cd617cb commit 959b499

File tree

4 files changed

+195
-68
lines changed

4 files changed

+195
-68
lines changed

docs/source/command_line.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,26 @@ format into the specified directory.
802802
Miscellaneous
803803
*************
804804

805+
.. option:: --install-types
806+
807+
This flag causes mypy to install known missing stub packages for
808+
third-party libraries using pip. It will display the pip command
809+
line to run, and expects a confirmation before installing
810+
anything.
811+
812+
If you use this option without providing any files or modules to
813+
type check, mypy will install stub packages suggested during the
814+
previous mypy run. If there are files or modules to type check,
815+
mypy first type checks those, and proposes to install missing
816+
stubs at the end of the run, but only if any missing modules were
817+
detected.
818+
819+
.. note::
820+
821+
This is new in mypy 0.800. Previous mypy versions included a
822+
selection of third-party packages, instead of having them
823+
installed separately.
824+
805825
.. option:: --junit-xml JUNIT_XML
806826

807827
Causes mypy to generate a JUnit XML test result document with

docs/source/getting_started.rst

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ Library stubs and typeshed
303303
Mypy uses library *stubs* to type check code interacting with library
304304
modules, including the Python standard library. A library stub defines
305305
a skeleton of the public interface of the library, including classes,
306-
variables and functions, and their types. Mypy ships with stubs from
307-
the `typeshed <https://github.com/python/typeshed>`_ project, which
308-
contains library stubs for the Python builtins, the standard library,
309-
and selected third-party packages.
306+
variables and functions, and their types. Mypy ships with stubs for
307+
the standard library from the `typeshed
308+
<https://github.com/python/typeshed>`_ project, which contains library
309+
stubs for the Python builtins, the standard library, and selected
310+
third-party packages.
310311

311312
For example, consider this code:
312313

@@ -318,11 +319,40 @@ Without a library stub, mypy would have no way of inferring the type of ``x``
318319
and checking that the argument to :py:func:`chr` has a valid type.
319320

320321
Mypy complains if it can't find a stub (or a real module) for a
321-
library module that you import. Some modules ship with stubs that mypy
322-
can automatically find, or you can install a 3rd party module with
323-
additional stubs (see :ref:`installed-packages` for details). You can
324-
also :ref:`create stubs <stub-files>` easily. We discuss ways of
325-
silencing complaints about missing stubs in :ref:`ignore-missing-imports`.
322+
library module that you import. Some modules ship with stubs or inline
323+
annotations that mypy can automatically find, or you can install
324+
additional stubs using pip (see :ref:`fix-missing-imports` and
325+
:ref:`installed-packages` for the details). For example, you can install
326+
the stubs for the ``requests`` package like this:
327+
328+
.. code-block::
329+
330+
python3 -m pip install types-requests
331+
332+
The stubs are usually packaged in a distribution named
333+
``types-<distribution>``. Note that the distribution name may be
334+
different from the name of the package that you import. For example,
335+
``types-PyYAML`` contains stubs for the ``yaml`` package. Mypy can
336+
often suggest the name of the stub distribution:
337+
338+
.. code-block:: text
339+
340+
prog.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
341+
prog.py:1: note: Hint: "python3 -m pip install types-PyYAML"
342+
...
343+
344+
.. note::
345+
346+
Starting in mypy 0.800, most third-party package stubs must be
347+
installed explicitly. This decouples mypy and stub versioning,
348+
allowing stubs to updated without updating mypy. This also allows
349+
stubs not originally included with mypy to be installed. Earlier
350+
mypy versions included a fixed set of stubs for third-party
351+
packages.
352+
353+
You can also :ref:`create
354+
stubs <stub-files>` easily. We discuss ways of silencing complaints
355+
about missing stubs in :ref:`ignore-missing-imports`.
326356

327357
Configuring mypy
328358
****************

docs/source/installed_packages.rst

Lines changed: 95 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,92 @@
33
Using installed packages
44
========================
55

6-
:pep:`561` specifies how to mark a package as supporting type checking.
7-
Below is a summary of how to create PEP 561 compatible packages and have
8-
mypy use them in type checking.
9-
10-
Using PEP 561 compatible packages with mypy
11-
*******************************************
12-
13-
Generally, you do not need to do anything to use installed packages that
14-
support typing for the Python executable used to run mypy. Note that most
15-
packages do not support typing. Packages that do support typing should be
16-
automatically picked up by mypy and used for type checking.
17-
18-
By default, mypy searches for packages installed for the Python executable
19-
running mypy. It is highly unlikely you want this situation if you have
20-
installed typed packages in another Python's package directory.
21-
22-
Generally, you can use the :option:`--python-version <mypy --python-version>` flag and mypy will try to find
23-
the correct package directory. If that fails, you can use the
24-
:option:`--python-executable <mypy --python-executable>` flag to point to the exact executable, and mypy will
25-
find packages installed for that Python executable.
26-
27-
Note that mypy does not support some more advanced import features, such as zip
28-
imports and custom import hooks.
29-
30-
If you do not want to use typed packages, use the :option:`--no-site-packages <mypy --no-site-packages>` flag
31-
to disable searching.
32-
33-
Note that stub-only packages (defined in :pep:`PEP 561: Stub-only Packages
34-
<561#stub-only-packages>`) cannot be used with ``MYPYPATH``. If you want mypy
35-
to find the package, it must be installed. For a package ``foo``, the name of
36-
the stub-only package (``foo-stubs``) is not a legal package name, so mypy
37-
will not find it, unless it is installed.
38-
39-
Making PEP 561 compatible packages
40-
**********************************
41-
42-
:pep:`561` notes three main ways to distribute type information. The first is a
43-
package that has only inline type annotations in the code itself. The second is
44-
a package that ships :ref:`stub files <stub-files>` with type information
45-
alongside the runtime code. The third method, also known as a "stub only
46-
package" is a package that ships type information for a package separately as
47-
stub files.
48-
49-
If you would like to publish a library package to a package repository (e.g.
50-
PyPI) for either internal or external use in type checking, packages that
51-
supply type information via type comments or annotations in the code should put
52-
a ``py.typed`` file in their package directory. For example, with a directory
53-
structure as follows
6+
Packages installed with pip can declare that they support type
7+
checking. For example, the `aiohttp
8+
<https://docs.aiohttp.org/en/stable/>`_ package has built-in support
9+
for type checking.
10+
11+
Packages can also provide stubs for a library. For example,
12+
``types-requests`` is a stub-only package that provides stubs for the
13+
`requests <https://requests.readthedocs.io/en/master/>`_ package.
14+
Stub packages are usually published from `typeshed
15+
<https://github.com/python/typeshed>`_, a shared repository for Python
16+
library stubs, and have a name of form ``types-<library>``. Note that
17+
many stub packages are not maintained by the original maintainers of
18+
the package.
19+
20+
The sections below explain how mypy can use these packages, and how
21+
you can create such packages.
22+
23+
.. note::
24+
25+
:pep:`561` specifies how a package can declare that it supports
26+
type checking.
27+
28+
Using installed packages with mypy (PEP 561)
29+
********************************************
30+
31+
Typically mypy will automatically find and use installed packages that
32+
support type checking or provide stubs. This requires that you install
33+
the packages in the Python environment that you use to run mypy. As
34+
many packages don't support type checking yet, you may also have to
35+
install a separate stub package, usually named
36+
``types-<library>``. (See :ref:`fix-missing-imports` for how to deal
37+
with libraries that don't support type checking and are also missing
38+
stubs.)
39+
40+
If you have installed typed packages in another Python installation or
41+
environment, mypy won't automatically find them. One option is to
42+
install another copy of those packages in the environment in which you
43+
use to run mypy. Alternatively, you can use the
44+
:option:`--python-executable <mypy --python-executable>` flag to point
45+
to the target Python executable, and mypy will find packages installed
46+
for that Python executable.
47+
48+
Note that mypy does not support some more advanced import features,
49+
such as zip imports and custom import hooks.
50+
51+
If you don't want to use installed packages that provide type
52+
information at all, use the :option:`--no-site-packages <mypy
53+
--no-site-packages>` flag to disable searching for installed packages.
54+
55+
Note that stub-only packages cannot be used with ``MYPYPATH``. If you
56+
want mypy to find the package, it must be installed. For a package
57+
``foo``, the name of the stub-only package (``foo-stubs``) is not a
58+
legal package name, so mypy will not find it, unless it is installed
59+
(see :pep:`PEP 561: Stub-only Packages <561#stub-only-packages>` for
60+
more information).
61+
62+
Creating PEP 561 compatible packages
63+
************************************
64+
65+
.. note::
66+
67+
You can generally ignore this section unless you maintain a package on
68+
PyPI, or want to publish type information for an existing PyPI
69+
package.
70+
71+
:pep:`561` describes three main ways to distribute type
72+
information:
73+
74+
1. A package has inline type annotations in the Python implementation.
75+
76+
2. A package ships :ref:`stub files <stub-files>` with type
77+
information alongside the Python implementation.
78+
79+
3. A package ships type information for another package separately as
80+
stub files (also known as a "stub-only package").
81+
82+
If you want to create a stub-only package for an existing library, the
83+
simplest way is to contribute stubs to the `typeshed
84+
<https://github.com/python/typeshed>`_ repository, and a stub package
85+
will automatically be uploaded to PyPI.
86+
87+
If you would like to publish a library package to a package repository
88+
yourself (e.g. on PyPI) for either internal or external use in type
89+
checking, packages that supply type information via type comments or
90+
annotations in the code should put a ``py.typed`` file in their
91+
package directory. For example, here is a typical directory structure:
5492

5593
.. code-block:: text
5694
@@ -60,7 +98,7 @@ structure as follows
6098
lib.py
6199
py.typed
62100
63-
the ``setup.py`` might look like
101+
The ``setup.py`` file could look like this:
64102

65103
.. code-block:: python
66104
@@ -80,7 +118,7 @@ the ``setup.py`` might look like
80118
``setup()``, or mypy will not be able to find the installed package.
81119

82120
Some packages have a mix of stub files and runtime files. These packages also
83-
require a ``py.typed`` file. An example can be seen below
121+
require a ``py.typed`` file. An example can be seen below:
84122

85123
.. code-block:: text
86124
@@ -91,7 +129,7 @@ require a ``py.typed`` file. An example can be seen below
91129
lib.pyi
92130
py.typed
93131
94-
the ``setup.py`` might look like:
132+
The ``setup.py`` file might look like this:
95133

96134
.. code-block:: python
97135
@@ -121,7 +159,7 @@ had stubs for ``package_c``, we might do the following:
121159
__init__.pyi
122160
lib.pyi
123161
124-
the ``setup.py`` might look like:
162+
The ``setup.py`` might look like this:
125163

126164
.. code-block:: python
127165
@@ -134,3 +172,8 @@ the ``setup.py`` might look like:
134172
package_data={"package_c-stubs": ["__init__.pyi", "lib.pyi"]},
135173
packages=["package_c-stubs"]
136174
)
175+
176+
If you have separate stubs for Python 2 and Python 3, you can place
177+
the Python 2 stubs in a directory with the suffix ``-python2-stubs``.
178+
We recommend that Python 2 and Python 3 stubs are bundled together for
179+
simplicity, instead of distributing them separately.

docs/source/running_mypy.rst

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,22 @@ The third outcome is what mypy will do in the ideal case. The following
124124
sections will discuss what to do in the other two cases.
125125

126126
.. _ignore-missing-imports:
127+
.. _fix-missing-imports:
127128

128129
Missing imports
129130
***************
130131

131-
When you import a module, mypy may report that it is unable to
132-
follow the import.
132+
When you import a module, mypy may report that it is unable to follow
133+
the import.
133134

134-
This can cause errors that look like the following::
135+
This can cause errors that look like the following:
135136

136-
main.py:1: error: No library stub file for standard library module 'antigravity'
137-
main.py:2: error: Skipping analyzing 'django': found module but no type hints or library stubs
138-
main.py:3: error: Cannot find implementation or library stub for module named 'this_module_does_not_exist'
137+
.. code-block:: text
138+
139+
main.py:1: error: Library stubs not installed for "requests" (or incompatible with Python 3.8)
140+
main.py:2: error: No library stub file for standard library module "antigravity"
141+
main.py:3: error: Skipping analyzing 'django': found module but no type hints or library stubs
142+
main.py:4: error: Cannot find implementation or library stub for module named "this_module_does_not_exist"
139143
140144
If you get any of these errors on an import, mypy will assume the type of that
141145
module is ``Any``, the dynamic type. This means attempting to access any
@@ -149,7 +153,37 @@ attribute of the module will automatically succeed:
149153
# But this type checks, and x will have type 'Any'
150154
x = does_not_exist.foobar()
151155
152-
The next three sections describe what each error means and recommended next steps.
156+
The next four sections describe what each error means and recommended next steps.
157+
158+
Library stubs not installed
159+
---------------------------
160+
161+
If mypy can't find stubs for a third-party library, and it knows that stubs exist for
162+
the library, you will get a message like this:
163+
164+
.. code-block:: text
165+
166+
main.py:1: error: Library stubs not installed for "yaml" (or incompatible with Python 3.8)
167+
main.py:1: note: Hint: "python3 -m pip install types-PyYAML"
168+
main.py:1: note: (or run "mypy --install-types" to install all missing stub packages)
169+
170+
You can resolve the issue by running the suggested pip command or
171+
commands. Alternatively, you can use :option:`--install-types <mypy
172+
--install-types>` to install all known missing stubs:
173+
174+
.. code-block:: text
175+
176+
mypy --install-types
177+
178+
This installs any stub packages that were suggested in the previous
179+
mypy run. You can also use your normal mypy command line with the
180+
extra :option:`--install-types <mypy --install-types>` option to
181+
install missing stubs at the end of the run (if any were found).
182+
183+
You can also get this message if the stubs only support Python 3 and
184+
your target Python version is Python 2, or vice versa. In this case
185+
follow instructions in
186+
:ref:`missing-type-hints-for-third-party-library`.
153187

154188
Missing type hints for standard library module
155189
----------------------------------------------

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy