|
| 1 | +.. _stugen: |
| 2 | + |
| 3 | +Automatic stub generation |
| 4 | +========================= |
| 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. |
| 10 | + |
| 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 | + |
| 15 | +.. code-block:: python |
| 16 | +
|
| 17 | + from other_module import dynamic |
| 18 | +
|
| 19 | + BORDER_WIDTH = 15 |
| 20 | +
|
| 21 | + class Window: |
| 22 | + parent = dynamic() |
| 23 | + def __init__(self, width, hight): |
| 24 | + self.width = width |
| 25 | + self.hight = hight |
| 26 | +
|
| 27 | + def create_empty() -> Window: |
| 28 | + return Window(0, 0) |
| 29 | +
|
| 30 | +will be transformed into the following stub file: |
| 31 | + |
| 32 | +.. code-block:: python |
| 33 | +
|
| 34 | + from typing import Any |
| 35 | +
|
| 36 | + BORDER_WIDTH: int = ... |
| 37 | +
|
| 38 | + class Window: |
| 39 | + parent: Any = ... |
| 40 | + width: Any = ... |
| 41 | + height: Any: ... |
| 42 | + def __init__(self, width, height) -> None: ... |
| 43 | +
|
| 44 | + def create_empty() -> Window: ... |
| 45 | +
|
| 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``. |
| 50 | + |
| 51 | +.. note:: |
| 52 | + |
| 53 | + Stubgen tool is still experimental and will evolve. Command line flags |
| 54 | + are liable to change between releases. |
| 55 | + |
| 56 | +Specifying what to stub |
| 57 | +*********************** |
| 58 | + |
| 59 | +By default, you can specify for what code you want to generate |
| 60 | +stub files by passing in the paths to the sources:: |
| 61 | + |
| 62 | + $ stubgen foo.py bar.py some_directory |
| 63 | + |
| 64 | +Note that directories are checked recursively. |
| 65 | + |
| 66 | +Stubgen also lets you specify modules for stub generation in two |
| 67 | +other ways. The relevant flags are: |
| 68 | + |
| 69 | +``-m MODULE``, ``--module MODULE`` |
| 70 | + Asks stubgen to generate stub file for the provided module. This flag |
| 71 | + may be repeated multiple times. |
| 72 | + |
| 73 | + Stubgen *will not* recursively generate stubs for any submodules of |
| 74 | + the provided module. |
| 75 | + |
| 76 | +``-p PACKAGE``, ``--package PACKAGE`` |
| 77 | + Asks stubgen to generate stubs for the provided package. This flag may |
| 78 | + be repeated multiple times. |
| 79 | + |
| 80 | + Stubgen *will* recursively generate stubs for all submodules of |
| 81 | + the provided package. This flag is identical to ``--module`` apart from |
| 82 | + this behavior. |
| 83 | + |
| 84 | +.. note:: |
| 85 | + |
| 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. |
| 88 | + |
| 89 | +Specifying how to generate stubs |
| 90 | +******************************** |
| 91 | + |
| 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: |
| 96 | + |
| 97 | +``--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. |
| 103 | + |
| 104 | +``--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. |
| 109 | + |
| 110 | +``--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. |
| 114 | + |
| 115 | +Additional flags |
| 116 | +**************** |
| 117 | + |
| 118 | +``--py2`` |
| 119 | + Run stubgen in Python 2 mode (the default is Python 3 mode). |
| 120 | + |
| 121 | +``--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. |
| 125 | + |
| 126 | +``--include-private`` |
| 127 | + Generate stubs for objects and members considered private (with single |
| 128 | + leading underscore and no trailing underscores). |
| 129 | + |
| 130 | +``--search-path PATH`` |
| 131 | + Specify module search directories, separated by colons (currently only |
| 132 | + used if ``--no-import`` is given). |
| 133 | + |
| 134 | +``--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. |
| 138 | + |
| 139 | +``-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 |
| 142 | + exist. Existing stubs in the output directory will be overwritten without |
| 143 | + warning. |
0 commit comments