|
1 | 1 | #!/usr/bin/env python
|
| 2 | +""" |
| 3 | +Perform AST checks to validate consistency of type hints with implementation. |
| 4 | +
|
| 5 | +NOTE: in most cases ``stubtest`` (distributed as part of ``mypy``) should be preferred |
| 6 | +
|
| 7 | +This script was written before the configuration of ``stubtest`` was well understood. |
| 8 | +It still has some utility, particularly for checking certain deprecations or other |
| 9 | +decorators which modify the runtime signature where you want the type hint to match |
| 10 | +the python source rather than runtime signature, perhaps. |
| 11 | +
|
| 12 | +The basic kinds of checks performed are: |
| 13 | +
|
| 14 | +- Set of items defined by the stubs vs implementation |
| 15 | + - Missing stub: MISSING_STUB = 1 |
| 16 | + - Missing implementation: MISSING_IMPL = 2 |
| 17 | +- Signatures of functions/methods |
| 18 | + - Positional Only Args: POS_ARGS = 4 |
| 19 | + - Keyword or Positional Args: ARGS = 8 |
| 20 | + - Variadic Positional Args: VARARG = 16 |
| 21 | + - Keyword Only Args: KWARGS = 32 |
| 22 | + - Variadic Keyword Only Args: VARKWARG = 64 |
| 23 | +
|
| 24 | +There are some exceptions to when these are checked: |
| 25 | +
|
| 26 | +- Set checks (MISSING_STUB/MISSING_IMPL) only apply at the module level |
| 27 | + - i.e. not for classes |
| 28 | + - Inheritance makes the set arithmetic harder when only loading AST |
| 29 | + - Attributes also make it more complicated when defined in init |
| 30 | +- Functions type hinted with ``overload`` are ignored for argument checking |
| 31 | + - Usually this means the implementation is is less strict in signature but will raise |
| 32 | + if an invalid signature is used, type checking allows such errors to be caught by |
| 33 | + the type checker instead of at runtime. |
| 34 | +- Private attribute/functions are ignored |
| 35 | + - Not expecting a type hint |
| 36 | + - applies to anything beginning, but not ending in ``__`` |
| 37 | + - If ``__all__`` is defined, also applies to anything not in ``__all__`` |
| 38 | +- Deprecated methods are not checked for missing stub |
| 39 | + - Only applies to wholesale deprecation, not deprecation of an individual arg |
| 40 | + - Other kinds of deprecations (e.g. argument deletion or rename) the type hint should |
| 41 | + match the current python definition line still. |
| 42 | + - For renames, the new name is used |
| 43 | + - For deletions/make keyword only, it is removed upon expiry |
| 44 | +
|
| 45 | +Usage: |
| 46 | +
|
| 47 | +Currently there is not any argument handling/etc, so all configuration is done in |
| 48 | +source. |
| 49 | +Since stubtest has almost completely superseded this script, this is unlikely to change. |
| 50 | +
|
| 51 | +The categories outlined above can each be ignored, and ignoring multiple can be done |
| 52 | +using the bitwise or (``|``) operator, e.g. ``ARGS | VARKWARG``. |
| 53 | +
|
| 54 | +This can be done globally or on a per file basis, by editing ``per_file_ignore``. |
| 55 | +For the latter, the key is the Pathlib Path to the affected file, and the value is the |
| 56 | +integer ignore. |
| 57 | +
|
| 58 | +Must be run from repository root: |
| 59 | +
|
| 60 | +``python tools/check_typehints.py`` |
| 61 | +""" |
2 | 62 |
|
3 | 63 | import ast
|
4 | 64 | import pathlib
|
|
0 commit comments