1
1
.. _stugen :
2
2
3
- Automatic stub generation
4
- =========================
3
+ Automatic stub generation (stubgen)
4
+ ===================================
5
5
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).
10
13
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:
14
17
15
18
.. code-block :: python
16
19
@@ -27,7 +30,7 @@ this source file:
27
30
def create_empty () -> Window:
28
31
return Window(0 , 0 )
29
32
30
- will be transformed into the following stub file:
33
+ Stubgen can generate this stub file based on the above file:
31
34
32
35
.. code-block :: python
33
36
@@ -43,74 +46,89 @@ will be transformed into the following stub file:
43
46
44
47
def create_empty () -> Window: ...
45
48
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.
50
56
51
57
.. note ::
52
58
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.
55
60
56
61
Specifying what to stub
57
62
***********************
58
63
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
61
76
62
- $ stubgen foo.py bar.py some_directory
77
+ Alternatively, you can give module or package names using the
78
+ ``-m `` or ``-p `` options::
63
79
64
- Note that directories are checked recursively.
80
+ $ stubgen -m foo -m bar -p my_pkg_dir
65
81
66
- Stubgen also lets you specify modules for stub generation in two
67
- other ways. The relevant flags are:
82
+ Details of the options:
68
83
69
84
``-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.
72
87
73
88
Stubgen *will not * recursively generate stubs for any submodules of
74
89
the provided module.
75
90
76
91
``-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.
79
94
80
95
Stubgen *will * recursively generate stubs for all submodules of
81
96
the provided package. This flag is identical to ``--module `` apart from
82
97
this behavior.
83
98
84
99
.. note ::
85
100
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.
88
103
89
104
Specifying how to generate stubs
90
105
********************************
91
106
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:
96
113
97
114
``--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.
103
121
104
122
``--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.
109
127
110
128
``--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.
114
132
115
133
Additional flags
116
134
****************
@@ -119,25 +137,25 @@ Additional flags
119
137
Run stubgen in Python 2 mode (the default is Python 3 mode).
120
138
121
139
``--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.
125
142
126
143
``--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).
129
146
130
147
``--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).
133
150
134
151
``--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.
138
156
139
157
``-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
142
160
exist. Existing stubs in the output directory will be overwritten without
143
161
warning.
0 commit comments