|
| 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 more things stubtest does not do: |
| 33 | + |
| 34 | +* Type check your code, use ``mypy`` |
| 35 | +* Generate stubs, use ``stubgen`` or ``pyright --createstub`` |
| 36 | +* Generate stubs based on running your application or test suite, use ``monkeytype`` |
| 37 | +* Apply stubs to code to produce inline types, use ``retype`` or ``libcst`` |
| 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 | +Example |
| 45 | +******* |
| 46 | + |
| 47 | +Here's a quick example of what stubtest can do: |
| 48 | + |
| 49 | +.. code-block:: shell |
| 50 | +
|
| 51 | + $ python3 -m pip install mypy |
| 52 | +
|
| 53 | + $ cat library.py |
| 54 | + x = "hello, stubtest" |
| 55 | +
|
| 56 | + def foo(x=None): |
| 57 | + print(x) |
| 58 | +
|
| 59 | + $ cat library.pyi |
| 60 | + x: int |
| 61 | +
|
| 62 | + def foo(x: int) -> None: ... |
| 63 | +
|
| 64 | + $ python3 -m mypy.stubtest library |
| 65 | + error: library.foo is inconsistent, runtime argument "x" has a default value but stub argument does not |
| 66 | + Stub: at line 3 |
| 67 | + def (x: builtins.int) |
| 68 | + Runtime: at line 3 in file ~/library.py |
| 69 | + def (x=None) |
| 70 | +
|
| 71 | + error: library.x variable differs from runtime type Literal['hello, stubtest'] |
| 72 | + Stub: at line 1 |
| 73 | + builtins.int |
| 74 | + Runtime: |
| 75 | + hello, stubtest |
| 76 | +
|
| 77 | +
|
| 78 | +Usage |
| 79 | +***** |
| 80 | + |
| 81 | +Running stubtest can be as simple as ``stubtest module_to_check``. |
| 82 | +Run :option:`stubtest --help` for a quick summary of options. |
| 83 | + |
| 84 | +stubtest must be able to import the code to be checked, so make sure that mypy |
| 85 | +is installed in the same environment as the library to be tested. In some |
| 86 | +cases, setting ``PYTHONPATH`` can help stubtest find the code to import. |
| 87 | + |
| 88 | +Similarly, stubtest must be able to find the stubs to be checked. stubtest |
| 89 | +respects the ``MYPYPATH`` environment variable. |
| 90 | + |
| 91 | +If you wish to ignore some of stubtest's complaints, stubtest supports a |
| 92 | +pretty handy allowlist system. |
| 93 | + |
| 94 | +The rest of this section documents the command line interface of stubtest. |
| 95 | + |
| 96 | +.. option:: --concise |
| 97 | + |
| 98 | + Makes stubtest's output more concise, one line per error |
| 99 | + |
| 100 | +.. option:: --ignore-missing-stub |
| 101 | + |
| 102 | + Ignore errors for stub missing things that are present at runtime |
| 103 | + |
| 104 | +.. option:: --ignore-positional-only |
| 105 | + |
| 106 | + Ignore errors for whether an argument should or shouldn't be positional-only |
| 107 | + |
| 108 | +.. option:: --allowlist FILE |
| 109 | + |
| 110 | + Use file as an allowlist. Can be passed multiple times to combine multiple |
| 111 | + allowlists. Allowlists can be created with --generate-allowlist. Allowlists |
| 112 | + support regular expressions. |
| 113 | + |
| 114 | +.. option:: --generate-allowlist |
| 115 | + |
| 116 | + Print an allowlist (to stdout) to be used with --allowlist |
| 117 | + |
| 118 | +.. option:: --ignore-unused-allowlist |
| 119 | + |
| 120 | + Ignore unused allowlist entries |
| 121 | + |
| 122 | +.. option:: --mypy-config-file FILE |
| 123 | + |
| 124 | + Use specified mypy config file to determine mypy plugins and mypy path |
| 125 | + |
| 126 | +.. option:: --custom-typeshed-dir DIR |
| 127 | + |
| 128 | + Use the custom typeshed in DIR |
| 129 | + |
| 130 | +.. option:: --check-typeshed |
| 131 | + |
| 132 | + Check all stdlib modules in typeshed |
| 133 | + |
| 134 | +.. option:: --help |
| 135 | + |
| 136 | + Show a help message :-) |
0 commit comments