Debian Codegraph @

docs/source/stubgen.rst

raw
1 .. _stubgen:
2
3 .. program:: stubgen
4
5 Automatic stub generation (stubgen)
6 ===================================
7
8 A stub file (see :pep:`484`) contains only type hints for the public
9 interface of a module, with empty function bodies. Mypy can use a stub
10 file instead of the real implementation to provide type information
11 for the module. They are useful for third-party modules whose authors
12 have not yet added type hints (and when no stubs are available in
13 typeshed) and C extension modules (which mypy can't directly process).
14
15 Mypy includes the ``stubgen`` tool that can automatically generate
16 stub files (``.pyi`` files) for Python modules and C extension modules.
17 For example, consider this source file:
18
19 .. code-block:: python
20
21 from other_module import dynamic
22
23 BORDER_WIDTH = 15
24
25 class Window:
26 parent = dynamic()
27 def __init__(self, width, height):
28 self.width = width
29 self.height = height
30
31 def create_empty() -> Window:
32 return Window(0, 0)
33
34 Stubgen can generate this stub file based on the above file:
35
36 .. code-block:: python
37
38 from typing import Any
39
40 BORDER_WIDTH: int = ...
41
42 class Window:
43 parent: Any = ...
44 width: Any = ...
45 height: Any = ...
46 def __init__(self, width, height) -> None: ...
47
48 def create_empty() -> Window: ...
49
50 Stubgen generates *draft* stubs. The auto-generated stub files often
51 require some manual updates, and most types will default to ``Any``.
52 The stubs will be much more useful if you add more precise type annotations,
53 at least for the most commonly used functionality.
54
55 The rest of this section documents the command line interface of stubgen.
56 Run :option:`stubgen --help` for a quick summary of options.
57
58 .. note::
59
60 The command-line flags may change between releases.
61
62 Specifying what to stub
63 ***********************
64
65 You can give stubgen paths of the source files for which you want to
66 generate stubs::
67
68 $ stubgen foo.py bar.py
69
70 This generates stubs ``out/foo.pyi`` and ``out/bar.pyi``. The default
71 output directory ``out`` can be overridden with :option:`-o DIR <-o>`.
72
73 You can also pass directories, and stubgen will recursively search
74 them for any ``.py`` files and generate stubs for all of them::
75
76 $ stubgen my_pkg_dir
77
78 Alternatively, you can give module or package names using the
79 :option:`-m` or :option:`-p` options::
80
81 $ stubgen -m foo -m bar -p my_pkg_dir
82
83 Details of the options:
84
85 .. option:: -m MODULE, --module MODULE
86
87 Generate a stub file for the given module. This flag may be repeated
88 multiple times.
89
90 Stubgen *will not* recursively generate stubs for any submodules of
91 the provided module.
92
93 .. option:: -p PACKAGE, --package PACKAGE
94
95 Generate stubs for the given package. This flag maybe repeated
96 multiple times.
97
98 Stubgen *will* recursively generate stubs for all submodules of
99 the provided package. This flag is identical to :option:`--module` apart from
100 this behavior.
101
102 .. note::
103
104 You can't mix paths and :option:`-m`/:option:`-p` options in the same stubgen
105 invocation.
106
107 Stubgen applies heuristics to avoid generating stubs for submodules
108 that include tests or vendored third-party packages.
109
110 Specifying how to generate stubs
111 ********************************
112
113 By default stubgen will try to import the target modules and packages.
114 This allows stubgen to use runtime introspection to generate stubs for C
115 extension modules and to improve the quality of the generated
116 stubs. By default, stubgen will also use mypy to perform light-weight
117 semantic analysis of any Python modules. Use the following flags to
118 alter the default behavior:
119
120 .. option:: --no-import
121
122 Don't try to import modules. Instead only use mypy's normal search mechanism to find
123 sources. This does not support C extension modules. This flag also disables
124 runtime introspection functionality, which mypy uses to find the value of
125 ``__all__``. As result the set of exported imported names in stubs may be
126 incomplete. This flag is generally only useful when importing a module causes
127 unwanted side effects, such as the running of tests. Stubgen tries to skip test
128 modules even without this option, but this does not always work.
129
130 .. option:: --no-analysis
131
132 Don't perform semantic analysis of source files. This may generate
133 worse stubs -- in particular, some module, class, and function aliases may
134 be represented as variables with the ``Any`` type. This is generally only
135 useful if semantic analysis causes a critical mypy error. Does not apply to
136 C extension modules. Incompatible with :option:`--inspect-mode`.
137
138 .. option:: --inspect-mode
139
140 Import and inspect modules instead of parsing source code. This is the default
141 behavior for C modules and pyc-only packages. The flag is useful to force
142 inspection for pure Python modules that make use of dynamically generated
143 members that would otherwise be omitted when using the default behavior of
144 code parsing. Implies :option:`--no-analysis` as analysis requires source
145 code.
146
147 .. option:: --doc-dir PATH
148
149 Try to infer better signatures by parsing .rst documentation in ``PATH``.
150 This may result in better stubs, but currently it only works for C extension
151 modules.
152
153 Additional flags
154 ****************
155
156 .. option:: -h, --help
157
158 Show help message and exit.
159
160 .. option:: --ignore-errors
161
162 If an exception was raised during stub generation, continue to process any
163 remaining modules instead of immediately failing with an error.
164
165 .. option:: --include-private
166
167 Include definitions that are considered private in stubs (with names such
168 as ``_foo`` with single leading underscore and no trailing underscores).
169
170 .. option:: --export-less
171
172 Don't export all names imported from other modules within the same package.
173 Instead, only export imported names that are not referenced in the module
174 that contains the import.
175
176 .. option:: --include-docstrings
177
178 Include docstrings in stubs. This will add docstrings to Python function and
179 classes stubs and to C extension function stubs.
180
181 .. option:: --search-path PATH
182
183 Specify module search directories, separated by colons (only used if
184 :option:`--no-import` is given).
185
186 .. option:: -o PATH, --output PATH
187
188 Change the output directory. By default the stubs are written in the
189 ``./out`` directory. The output directory will be created if it doesn't
190 exist. Existing stubs in the output directory will be overwritten without
191 warning.
192
193 .. option:: -v, --verbose
194
195 Produce more verbose output.
196
197 .. option:: -q, --quiet
198
199 Produce less verbose output.