| 1 |
.. _installed-packages: |
| 2 |
|
| 3 |
Using installed packages |
| 4 |
======================== |
| 5 |
|
| 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 |
.. note:: |
| 29 |
|
| 30 |
New versions of stub packages often use type system features not |
| 31 |
supported by older, and even fairly recent mypy versions. If you |
| 32 |
pin to an older version of mypy (using ``requirements.txt``, for |
| 33 |
example), it is recommended that you also pin the versions of all |
| 34 |
your stub package dependencies. |
| 35 |
|
| 36 |
.. note:: |
| 37 |
|
| 38 |
Starting in mypy 0.900, most third-party package stubs must be |
| 39 |
installed explicitly. This decouples mypy and stub versioning, |
| 40 |
allowing stubs to updated without updating mypy. This also allows |
| 41 |
stubs not originally included with mypy to be installed. Earlier |
| 42 |
mypy versions included a fixed set of stubs for third-party |
| 43 |
packages. |
| 44 |
|
| 45 |
Using installed packages with mypy (PEP 561) |
| 46 |
******************************************** |
| 47 |
|
| 48 |
Typically mypy will automatically find and use installed packages that |
| 49 |
support type checking or provide stubs. This requires that you install |
| 50 |
the packages in the Python environment that you use to run mypy. As |
| 51 |
many packages don't support type checking yet, you may also have to |
| 52 |
install a separate stub package, usually named |
| 53 |
``types-<library>``. (See :ref:`fix-missing-imports` for how to deal |
| 54 |
with libraries that don't support type checking and are also missing |
| 55 |
stubs.) |
| 56 |
|
| 57 |
If you have installed typed packages in another Python installation or |
| 58 |
environment, mypy won't automatically find them. One option is to |
| 59 |
install another copy of those packages in the environment in which you |
| 60 |
installed mypy. Alternatively, you can use the |
| 61 |
:option:`--python-executable <mypy --python-executable>` flag to point |
| 62 |
to the Python executable for another environment, and mypy will find |
| 63 |
packages installed for that Python executable. |
| 64 |
|
| 65 |
Note that mypy does not support some more advanced import features, |
| 66 |
such as zip imports and custom import hooks. |
| 67 |
|
| 68 |
If you don't want to use installed packages that provide type |
| 69 |
information at all, use the :option:`--no-site-packages <mypy |
| 70 |
--no-site-packages>` flag to disable searching for installed packages. |
| 71 |
|
| 72 |
Note that stub-only packages cannot be used with ``MYPYPATH``. If you |
| 73 |
want mypy to find the package, it must be installed. For a package |
| 74 |
``foo``, the name of the stub-only package (``foo-stubs``) is not a |
| 75 |
legal package name, so mypy will not find it, unless it is installed |
| 76 |
(see :pep:`PEP 561: Stub-only Packages <561#stub-only-packages>` for |
| 77 |
more information). |
| 78 |
|
| 79 |
Creating PEP 561 compatible packages |
| 80 |
************************************ |
| 81 |
|
| 82 |
.. note:: |
| 83 |
|
| 84 |
You can generally ignore this section unless you maintain a package on |
| 85 |
PyPI, or want to publish type information for an existing PyPI |
| 86 |
package. |
| 87 |
|
| 88 |
:pep:`561` describes three main ways to distribute type |
| 89 |
information: |
| 90 |
|
| 91 |
1. A package has inline type annotations in the Python implementation. |
| 92 |
|
| 93 |
2. A package ships :ref:`stub files <stub-files>` with type |
| 94 |
information alongside the Python implementation. |
| 95 |
|
| 96 |
3. A package ships type information for another package separately as |
| 97 |
stub files (also known as a "stub-only package"). |
| 98 |
|
| 99 |
If you want to create a stub-only package for an existing library, the |
| 100 |
simplest way is to contribute stubs to the `typeshed |
| 101 |
<https://github.com/python/typeshed>`_ repository, and a stub package |
| 102 |
will automatically be uploaded to PyPI. |
| 103 |
|
| 104 |
If you would like to publish a library package to a package repository |
| 105 |
yourself (e.g. on PyPI) for either internal or external use in type |
| 106 |
checking, packages that supply type information via type comments or |
| 107 |
annotations in the code should put a ``py.typed`` file in their |
| 108 |
package directory. For example, here is a typical directory structure: |
| 109 |
|
| 110 |
.. code-block:: text |
| 111 |
|
| 112 |
setup.py |
| 113 |
package_a/ |
| 114 |
__init__.py |
| 115 |
lib.py |
| 116 |
py.typed |
| 117 |
|
| 118 |
The ``setup.py`` file could look like this: |
| 119 |
|
| 120 |
.. code-block:: python |
| 121 |
|
| 122 |
from setuptools import setup |
| 123 |
|
| 124 |
setup( |
| 125 |
name="SuperPackageA", |
| 126 |
author="Me", |
| 127 |
version="0.1", |
| 128 |
package_data={"package_a": ["py.typed"]}, |
| 129 |
packages=["package_a"] |
| 130 |
) |
| 131 |
|
| 132 |
Some packages have a mix of stub files and runtime files. These packages also |
| 133 |
require a ``py.typed`` file. An example can be seen below: |
| 134 |
|
| 135 |
.. code-block:: text |
| 136 |
|
| 137 |
setup.py |
| 138 |
package_b/ |
| 139 |
__init__.py |
| 140 |
lib.py |
| 141 |
lib.pyi |
| 142 |
py.typed |
| 143 |
|
| 144 |
The ``setup.py`` file might look like this: |
| 145 |
|
| 146 |
.. code-block:: python |
| 147 |
|
| 148 |
from setuptools import setup |
| 149 |
|
| 150 |
setup( |
| 151 |
name="SuperPackageB", |
| 152 |
author="Me", |
| 153 |
version="0.1", |
| 154 |
package_data={"package_b": ["py.typed", "lib.pyi"]}, |
| 155 |
packages=["package_b"] |
| 156 |
) |
| 157 |
|
| 158 |
In this example, both ``lib.py`` and the ``lib.pyi`` stub file exist. At |
| 159 |
runtime, the Python interpreter will use ``lib.py``, but mypy will use |
| 160 |
``lib.pyi`` instead. |
| 161 |
|
| 162 |
If the package is stub-only (not imported at runtime), the package should have |
| 163 |
a prefix of the runtime package name and a suffix of ``-stubs``. |
| 164 |
A ``py.typed`` file is not needed for stub-only packages. For example, if we |
| 165 |
had stubs for ``package_c``, we might do the following: |
| 166 |
|
| 167 |
.. code-block:: text |
| 168 |
|
| 169 |
setup.py |
| 170 |
package_c-stubs/ |
| 171 |
__init__.pyi |
| 172 |
lib.pyi |
| 173 |
|
| 174 |
The ``setup.py`` might look like this: |
| 175 |
|
| 176 |
.. code-block:: python |
| 177 |
|
| 178 |
from setuptools import setup |
| 179 |
|
| 180 |
setup( |
| 181 |
name="SuperPackageC", |
| 182 |
author="Me", |
| 183 |
version="0.1", |
| 184 |
package_data={"package_c-stubs": ["__init__.pyi", "lib.pyi"]}, |
| 185 |
packages=["package_c-stubs"] |
| 186 |
) |
| 187 |
|
| 188 |
The instructions above are enough to ensure that the built wheels |
| 189 |
contain the appropriate files. However, to ensure inclusion inside the |
| 190 |
``sdist`` (``.tar.gz`` archive), you may also need to modify the |
| 191 |
inclusion rules in your ``MANIFEST.in``: |
| 192 |
|
| 193 |
.. code-block:: text |
| 194 |
|
| 195 |
global-include *.pyi |
| 196 |
global-include *.typed |