3
3
Using installed packages
4
4
========================
5
5
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:
54
92
55
93
.. code-block :: text
56
94
@@ -60,7 +98,7 @@ structure as follows
60
98
lib.py
61
99
py.typed
62
100
63
- the ``setup.py `` might look like
101
+ The ``setup.py `` file could look like this:
64
102
65
103
.. code-block :: python
66
104
@@ -80,7 +118,7 @@ the ``setup.py`` might look like
80
118
``setup() ``, or mypy will not be able to find the installed package.
81
119
82
120
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:
84
122
85
123
.. code-block :: text
86
124
@@ -91,7 +129,7 @@ require a ``py.typed`` file. An example can be seen below
91
129
lib.pyi
92
130
py.typed
93
131
94
- the ``setup.py `` might look like:
132
+ The ``setup.py `` file might look like this :
95
133
96
134
.. code-block :: python
97
135
@@ -121,7 +159,7 @@ had stubs for ``package_c``, we might do the following:
121
159
__init__.pyi
122
160
lib.pyi
123
161
124
- the ``setup.py `` might look like:
162
+ The ``setup.py `` might look like this :
125
163
126
164
.. code-block :: python
127
165
@@ -134,3 +172,8 @@ the ``setup.py`` might look like:
134
172
package_data = {" package_c-stubs" : [" __init__.pyi" , " lib.pyi" ]},
135
173
packages = [" package_c-stubs" ]
136
174
)
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.
0 commit comments