Skip to content

Commit 5ff5f3b

Browse files
JukkaLGuido van Rossum
authored and
Guido van Rossum
committed
Update documentation of stubgen (#6346)
This is mostly style and language tweaks. The content remains roughly the same.
1 parent 5ed81b5 commit 5ff5f3b

File tree

1 file changed

+74
-56
lines changed

1 file changed

+74
-56
lines changed

docs/source/stubgen.rst

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
.. _stugen:
22

3-
Automatic stub generation
4-
=========================
3+
Automatic stub generation (stubgen)
4+
===================================
55

6-
Stub files (see `PEP 484 <https://www.python.org/dev/peps/pep-0484/#stub-files>`_)
7-
are files containing only type hints not the actual runtime implementation.
8-
They can be useful for C extension modules, third-party modules whose authors
9-
have not yet added type hints, etc.
6+
A stub file (see `PEP 484 <https://www.python.org/dev/peps/pep-0484/#stub-files>`_)
7+
contains only type hints for the public interface of a module, with empty
8+
function bodies. Mypy can use a stub file instead of the real implementation
9+
to provide type information for the module. They are useful for third-party
10+
modules whose authors have not yet added type hints (and when no stubs are
11+
available in typeshed) and C extension modules (which mypy can't directly
12+
process).
1013

11-
Mypy comes with a ``stubgen`` tool for automatic generation of
12-
stub files (``.pyi`` files) from Python source files. For example,
13-
this source file:
14+
Mypy includes the ``stubgen`` tool that can automatically generate
15+
stub files (``.pyi`` files) for Python modules and C extension modules.
16+
For example, consider this source file:
1417

1518
.. code-block:: python
1619
@@ -27,7 +30,7 @@ this source file:
2730
def create_empty() -> Window:
2831
return Window(0, 0)
2932
30-
will be transformed into the following stub file:
33+
Stubgen can generate this stub file based on the above file:
3134

3235
.. code-block:: python
3336
@@ -43,74 +46,89 @@ will be transformed into the following stub file:
4346
4447
def create_empty() -> Window: ...
4548
46-
In most cases, the auto-generated stub files require manual check for
47-
completeness. This section documents stubgen's command line interface.
48-
You can view a quick summary of the available flags by running
49-
``stubgen --help``.
49+
Stubgen generates *draft* stubs. The auto-generated stub files often
50+
require some some manual updates, and most types will default to ``Any``.
51+
The stubs will be much more useful if you add more precise type annotations,
52+
at least for the most commonly used functionality.
53+
54+
The rest of this section documents the command line interface of stubgen.
55+
Run ``stubgen --help`` for a quick summary of options.
5056

5157
.. note::
5258

53-
Stubgen tool is still experimental and will evolve. Command line flags
54-
are liable to change between releases.
59+
The command-line flags may change between releases.
5560

5661
Specifying what to stub
5762
***********************
5863

59-
By default, you can specify for what code you want to generate
60-
stub files by passing in the paths to the sources::
64+
You can give stubgen paths of the source files for which you want to
65+
generate stubs::
66+
67+
$ stubgen foo.py bar.py
68+
69+
This generates stubs ``out/foo.pyi`` and ``out/bar.pyi``. The default
70+
output directory ``out`` can be overridden with ``-o DIR``.
71+
72+
You can also pass directories, and stubgen will recursively search
73+
them for any ``.py`` files and generate stubs for all of them::
74+
75+
$ stubgen my_pkg_dir
6176

62-
$ stubgen foo.py bar.py some_directory
77+
Alternatively, you can give module or package names using the
78+
``-m`` or ``-p`` options::
6379

64-
Note that directories are checked recursively.
80+
$ stubgen -m foo -m bar -p my_pkg_dir
6581

66-
Stubgen also lets you specify modules for stub generation in two
67-
other ways. The relevant flags are:
82+
Details of the options:
6883

6984
``-m MODULE``, ``--module MODULE``
70-
Asks stubgen to generate stub file for the provided module. This flag
71-
may be repeated multiple times.
85+
Generate a stub file for the given module. This flag may be repeated
86+
multiple times.
7287

7388
Stubgen *will not* recursively generate stubs for any submodules of
7489
the provided module.
7590

7691
``-p PACKAGE``, ``--package PACKAGE``
77-
Asks stubgen to generate stubs for the provided package. This flag may
78-
be repeated multiple times.
92+
Generate stubs for the given package. This flag maybe repeated
93+
multiple times.
7994

8095
Stubgen *will* recursively generate stubs for all submodules of
8196
the provided package. This flag is identical to ``--module`` apart from
8297
this behavior.
8398

8499
.. note::
85100

86-
You can use either module/package mode or source code mode, these two
87-
can't be mixed together in the same stubgen invocation.
101+
You can't mix paths and ``-m``/``-p`` options in the same stubgen
102+
invocation.
88103

89104
Specifying how to generate stubs
90105
********************************
91106

92-
By default stubgen will try to import the modules and packages given.
93-
This has an advantage of possibility to discover and stub also C modules.
94-
By default stubgen will use mypy to semantically analyze the Python
95-
sources found. To alter this behavior, you can use following flags:
107+
By default stubgen will try to import the target modules and packages.
108+
This allows stubgen to use runtime introspection to generate stubs for C
109+
extension modules and to improve the quality of the generated
110+
stubs. By default, stubgen will also use mypy to perform light-weight
111+
semantic analysis of any Python modules. Use the following flags to
112+
alter the default behavior:
96113

97114
``--no-import``
98-
Don't try to import modules, instead use mypy's normal mechanisms to find
99-
sources. This will not find any C extension modules. Stubgen also uses
100-
runtime introspection to find actual value of ``__all__``, so with this flag
101-
the set of re-exported names may be incomplete. This flag will be useful if
102-
importing the module causes an error.
115+
Don't try to import modules. Instead use mypy's normal search mechanism to find
116+
sources. This does not support C extension modules. This flag also disables
117+
runtime introspection functionality, which mypy uses to find the value of
118+
``__all__``. As result the set of exported imported names in stubs may be
119+
incomplete. This flag is generally only useful when importing a module generates
120+
an error for some reason.
103121

104122
``--parse-only``
105-
Don't perform mypy semantic analysis of source files. This may generate
106-
worse stubs: in particular some module, class, and function aliases may
107-
be typed as variables with ``Any`` type. This can be useful if semantic
108-
analysis causes a critical mypy error.
123+
Don't perform semantic analysis of source files. This may generate
124+
worse stubs -- in particular, some module, class, and function aliases may
125+
be represented as variables with the ``Any`` type. This is generally only
126+
useful if semantic analysis causes a critical mypy error.
109127

110128
``--doc-dir PATH``
111-
Try to infer function and class signatures by parsing .rst documentation
112-
in ``PATH``. This may result in better stubs, but currently only works for
113-
C modules.
129+
Try to infer better signatures by parsing .rst documentation in ``PATH``.
130+
This may result in better stubs, but currently it only works for C extension
131+
modules.
114132

115133
Additional flags
116134
****************
@@ -119,25 +137,25 @@ Additional flags
119137
Run stubgen in Python 2 mode (the default is Python 3 mode).
120138

121139
``--ignore-errors``
122-
Ignore any errors when trying to generate stubs for modules and packages.
123-
This may be useful for C modules where runtime introspection is used
124-
intensively.
140+
If an exception was raised during stub generation, continue to process any
141+
remaining modules instead of immediately failing with an error.
125142

126143
``--include-private``
127-
Generate stubs for objects and members considered private (with single
128-
leading underscore and no trailing underscores).
144+
Include definitions that are considered private in stubs (with names such
145+
as ``_foo`` with single leading underscore and no trailing underscores).
129146

130147
``--search-path PATH``
131-
Specify module search directories, separated by colons (currently only
132-
used if ``--no-import`` is given).
148+
Specify module search directories, separated by colons (only used if
149+
``--no-import`` is given).
133150

134151
``--python-executable PATH``
135-
Use Python interpreter at ``PATH`` for module finding and runtime
136-
introspection (has no effect with ``--no-import``). Currently only works
137-
for Python 2. In Python 3 mode only the default interpreter will be used.
152+
Use Python interpreter at ``PATH`` for importing modules and runtime
153+
introspection. This has no effect with ``--no-import``, and this only works
154+
in Python 2 mode. In Python 3 mode the Python interpreter used to run stubgen
155+
will always be used.
138156

139157
``-o PATH``, ``--output PATH``
140-
Change the output directory. By default the stubs are written in
141-
``./out`` directory. The output directory will be created if it didn't
158+
Change the output directory. By default the stubs are written in the
159+
``./out`` directory. The output directory will be created if it doesn't
142160
exist. Existing stubs in the output directory will be overwritten without
143161
warning.

0 commit comments

Comments
 (0)