| 1 |
.. _stubtest: |
| 2 |
|
| 3 |
.. program:: stubtest |
| 4 |
|
| 5 |
Automatic stub testing (stubtest) |
| 6 |
================================= |
| 7 |
|
| 8 |
Stub files are files containing type annotations. See |
| 9 |
`PEP 484 <https://www.python.org/dev/peps/pep-0484/#stub-files>`_ |
| 10 |
for more motivation and details. |
| 11 |
|
| 12 |
A common problem with stub files is that they tend to diverge from the |
| 13 |
actual implementation. Mypy includes the ``stubtest`` tool that can |
| 14 |
automatically check for discrepancies between the stubs and the |
| 15 |
implementation at runtime. |
| 16 |
|
| 17 |
What stubtest does and does not do |
| 18 |
********************************** |
| 19 |
|
| 20 |
Stubtest will import your code and introspect your code objects at runtime, for |
| 21 |
example, by using the capabilities of the :py:mod:`inspect` module. Stubtest |
| 22 |
will then analyse the stub files, and compare the two, pointing out things that |
| 23 |
differ between stubs and the implementation at runtime. |
| 24 |
|
| 25 |
It's important to be aware of the limitations of this comparison. Stubtest will |
| 26 |
not make any attempt to statically analyse your actual code and relies only on |
| 27 |
dynamic runtime introspection (in particular, this approach means stubtest works |
| 28 |
well with extension modules). However, this means that stubtest has limited |
| 29 |
visibility; for instance, it cannot tell if a return type of a function is |
| 30 |
accurately typed in the stubs. |
| 31 |
|
| 32 |
For clarity, here are some additional things stubtest can't do: |
| 33 |
|
| 34 |
* Type check your code -- use ``mypy`` instead |
| 35 |
* Generate stubs -- use ``stubgen`` or ``pyright --createstub`` instead |
| 36 |
* Generate stubs based on running your application or test suite -- use ``monkeytype`` instead |
| 37 |
* Apply stubs to code to produce inline types -- use ``retype`` or ``libcst`` instead |
| 38 |
|
| 39 |
In summary, stubtest works very well for ensuring basic consistency between |
| 40 |
stubs and implementation or to check for stub completeness. It's used to |
| 41 |
test Python's official collection of library stubs, |
| 42 |
`typeshed <https://github.com/python/typeshed>`_. |
| 43 |
|
| 44 |
.. warning:: |
| 45 |
|
| 46 |
stubtest will import and execute Python code from the packages it checks. |
| 47 |
|
| 48 |
Example |
| 49 |
******* |
| 50 |
|
| 51 |
Here's a quick example of what stubtest can do: |
| 52 |
|
| 53 |
.. code-block:: shell |
| 54 |
|
| 55 |
$ python3 -m pip install mypy |
| 56 |
|
| 57 |
$ cat library.py |
| 58 |
x = "hello, stubtest" |
| 59 |
|
| 60 |
def foo(x=None): |
| 61 |
print(x) |
| 62 |
|
| 63 |
$ cat library.pyi |
| 64 |
x: int |
| 65 |
|
| 66 |
def foo(x: int) -> None: ... |
| 67 |
|
| 68 |
$ python3 -m mypy.stubtest library |
| 69 |
error: library.foo is inconsistent, runtime argument "x" has a default value but stub argument does not |
| 70 |
Stub: at line 3 |
| 71 |
def (x: builtins.int) |
| 72 |
Runtime: in file ~/library.py:3 |
| 73 |
def (x=None) |
| 74 |
|
| 75 |
error: library.x variable differs from runtime type Literal['hello, stubtest'] |
| 76 |
Stub: at line 1 |
| 77 |
builtins.int |
| 78 |
Runtime: |
| 79 |
'hello, stubtest' |
| 80 |
|
| 81 |
|
| 82 |
Usage |
| 83 |
***** |
| 84 |
|
| 85 |
Running stubtest can be as simple as ``stubtest module_to_check``. |
| 86 |
Run :option:`stubtest --help` for a quick summary of options. |
| 87 |
|
| 88 |
Stubtest must be able to import the code to be checked, so make sure that mypy |
| 89 |
is installed in the same environment as the library to be tested. In some |
| 90 |
cases, setting ``PYTHONPATH`` can help stubtest find the code to import. |
| 91 |
|
| 92 |
Similarly, stubtest must be able to find the stubs to be checked. Stubtest |
| 93 |
respects the ``MYPYPATH`` environment variable -- consider using this if you |
| 94 |
receive a complaint along the lines of "failed to find stubs". |
| 95 |
|
| 96 |
Note that stubtest requires mypy to be able to analyse stubs. If mypy is unable |
| 97 |
to analyse stubs, you may get an error on the lines of "not checking stubs due |
| 98 |
to mypy build errors". In this case, you will need to mitigate those errors |
| 99 |
before stubtest will run. Despite potential overlap in errors here, stubtest is |
| 100 |
not intended as a substitute for running mypy directly. |
| 101 |
|
| 102 |
Allowlist |
| 103 |
********* |
| 104 |
|
| 105 |
If you wish to ignore some of stubtest's complaints, stubtest supports a |
| 106 |
pretty handy :option:`--allowlist` system. |
| 107 |
|
| 108 |
Let's say that you have this python module called ``ex``: |
| 109 |
|
| 110 |
.. code-block:: python |
| 111 |
|
| 112 |
try: |
| 113 |
import optional_expensive_dep |
| 114 |
except ImportError: |
| 115 |
optional_expensive_dep = None |
| 116 |
|
| 117 |
first = 1 |
| 118 |
if optional_expensive_dep: |
| 119 |
second = 2 |
| 120 |
|
| 121 |
Let's say that you can't install ``optional_expensive_dep`` in CI for some reason, |
| 122 |
but you still want to include ``second: int`` in the stub file: |
| 123 |
|
| 124 |
.. code-block:: python |
| 125 |
|
| 126 |
first: int |
| 127 |
second: int |
| 128 |
|
| 129 |
In this case stubtest will correctly complain: |
| 130 |
|
| 131 |
.. code-block:: shell |
| 132 |
|
| 133 |
error: ex.second is not present at runtime |
| 134 |
Stub: in file /.../ex.pyi:2 |
| 135 |
builtins.int |
| 136 |
Runtime: |
| 137 |
MISSING |
| 138 |
|
| 139 |
Found 1 error (checked 1 module) |
| 140 |
|
| 141 |
To fix this, you can add an ``allowlist`` entry: |
| 142 |
|
| 143 |
.. code-block:: ini |
| 144 |
|
| 145 |
# Allowlist entries in `allowlist.txt` file: |
| 146 |
|
| 147 |
# Does not exist if `optional_expensive_dep` is not installed: |
| 148 |
ex.second |
| 149 |
|
| 150 |
And now when running stubtest with ``--allowlist=allowlist.txt``, |
| 151 |
no errors will be generated anymore. |
| 152 |
|
| 153 |
Allowlists also support regular expressions, |
| 154 |
which can be useful to ignore many similar errors at once. |
| 155 |
They can also be useful for suppressing stubtest errors that occur sometimes, |
| 156 |
but not on every CI run. For example, if some CI workers have |
| 157 |
``optional_expensive_dep`` installed, stubtest might complain with this message |
| 158 |
on those workers if you had the ``ex.second`` allowlist entry: |
| 159 |
|
| 160 |
.. code-block:: ini |
| 161 |
|
| 162 |
note: unused allowlist entry ex.second |
| 163 |
Found 1 error (checked 1 module) |
| 164 |
|
| 165 |
Changing ``ex.second`` to be ``(ex\.second)?`` will make this error optional, |
| 166 |
meaning that stubtest will pass whether or not a CI runner |
| 167 |
has``optional_expensive_dep`` installed. |
| 168 |
|
| 169 |
CLI |
| 170 |
*** |
| 171 |
|
| 172 |
The rest of this section documents the command line interface of stubtest. |
| 173 |
|
| 174 |
.. option:: --concise |
| 175 |
|
| 176 |
Makes stubtest's output more concise, one line per error |
| 177 |
|
| 178 |
.. option:: --ignore-missing-stub |
| 179 |
|
| 180 |
Ignore errors for stub missing things that are present at runtime |
| 181 |
|
| 182 |
.. option:: --ignore-positional-only |
| 183 |
|
| 184 |
Ignore errors for whether an argument should or shouldn't be positional-only |
| 185 |
|
| 186 |
.. option:: --strict-type-check-only |
| 187 |
|
| 188 |
Require :py:func:`@type_check_only <typing.type_check_only>` on private types |
| 189 |
that are not present at runtime. |
| 190 |
|
| 191 |
.. option:: --allowlist FILE |
| 192 |
|
| 193 |
Use file as an allowlist. Can be passed multiple times to combine multiple |
| 194 |
allowlists. Allowlists can be created with :option:`--generate-allowlist`. |
| 195 |
Allowlists support regular expressions. |
| 196 |
|
| 197 |
The presence of an entry in the allowlist means stubtest will not generate |
| 198 |
any errors for the corresponding definition. |
| 199 |
|
| 200 |
.. option:: --generate-allowlist |
| 201 |
|
| 202 |
Print an allowlist (to stdout) to be used with :option:`--allowlist`. |
| 203 |
|
| 204 |
When introducing stubtest to an existing project, this is an easy way to |
| 205 |
silence all existing errors. |
| 206 |
|
| 207 |
.. option:: --ignore-unused-allowlist |
| 208 |
|
| 209 |
Ignore unused allowlist entries |
| 210 |
|
| 211 |
Without this option enabled, the default is for stubtest to complain if an |
| 212 |
allowlist entry is not necessary for stubtest to pass successfully. |
| 213 |
|
| 214 |
Note if an allowlist entry is a regex that matches the empty string, |
| 215 |
stubtest will never consider it unused. For example, to get |
| 216 |
``--ignore-unused-allowlist`` behaviour for a single allowlist entry like |
| 217 |
``foo.bar`` you could add an allowlist entry ``(foo\.bar)?``. |
| 218 |
This can be useful when an error only occurs on a specific platform. |
| 219 |
|
| 220 |
.. option:: --mypy-config-file FILE |
| 221 |
|
| 222 |
Use specified mypy config *file* to determine mypy plugins and mypy path |
| 223 |
|
| 224 |
.. option:: --custom-typeshed-dir DIR |
| 225 |
|
| 226 |
Use the custom typeshed in *DIR* |
| 227 |
|
| 228 |
.. option:: --check-typeshed |
| 229 |
|
| 230 |
Check all stdlib modules in typeshed |
| 231 |
|
| 232 |
.. option:: --help |
| 233 |
|
| 234 |
Show a help message :-) |