|
| 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. |
0 commit comments