Skip to content

Commit d945fea

Browse files
authored
Add a tutorial for using external libraries (#2046)
1 parent 945656b commit d945fea

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

docs/guides/writing_stubs.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Writing and Maintaining Stub Files
55
**********************************
66

77
Stub files are a means of providing type information for Python modules.
8-
For a full reference, refer to :ref:`stub-files`.
8+
For a quick introduction, see :ref:`external_libraries`. For a full reference,
9+
refer to :ref:`distributing-type`.
910

1011
Maintaining stubs can be a little cumbersome because they are separated from the
1112
implementation. This page lists some tools that make writing and maintaining

docs/index.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
Static Typing with Python
33
*************************
44

5+
Tutorials
6+
=========
7+
8+
..
9+
Keep in sync with tutorials/index.rst.
10+
11+
.. toctree::
12+
:maxdepth: 1
13+
14+
tutorials/external_libraries
15+
516
Guides
617
======
718

docs/tutorials/external_libraries.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.. _external_libraries:
2+
3+
************************
4+
Using External Libraries
5+
************************
6+
7+
.. seealso::
8+
If you are looking for information on how to write type hints for
9+
external libraries, see the :ref:`writing_stubs` guide.
10+
11+
Many external libraries -- whether installed from the
12+
`Python Package Index <https://pypi.org/>`_ (PyPI) or from other sources --
13+
provide their own type hints. This is indicated by the presence of a
14+
``py.typed`` file in the library's root directory. If you install such a
15+
library, you can use it with any type checker without any additional
16+
configuration.
17+
18+
Type hints can either be included in the library's source code the same way
19+
as in your own code, or they can be provided in separate so-called
20+
*stub files*. Stub files are named ``<module>.pyi`` and contain only type
21+
hints, without any implementation.
22+
23+
For libraries that don't include their own type hints, a separate
24+
*stub package* may provide them. These stub packages are often written by the
25+
library authors themselves, by the contributors to the
26+
`typeshed <https://github.com/python/typeshed>`_ project, or by third-party
27+
contributors. These packages are usually named ``types-<library>``
28+
or ``<library>-stubs``. These packages can be installed from PyPI as usual, and
29+
they will be automatically discovered by type checkers::
30+
31+
pip install requests types-requests
32+
33+
.. warning::
34+
35+
The usual security considerations apply when installing third-party
36+
packages. Only install packages from sources you trust. Stub packages
37+
have the same security implications as any other package.
38+
39+
..
40+
TODO: Once development dependencies are supported by pyproject.toml,
41+
and described in https://packaging.python.org/en/latest/guides/writing-pyproject-toml/
42+
we should recommend installing type stubs as a development dependency.

docs/tutorials/index.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:orphan:
2+
3+
*********************
4+
Type System Tutorials
5+
*********************
6+
7+
..
8+
Keep in sync with docs/index.rst.
9+
10+
11+
.. toctree::
12+
:maxdepth: 2
13+
:caption: Contents:
14+
15+
external_libraries

0 commit comments

Comments
 (0)