|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Test typeshed using stubtest |
| 3 | +
|
| 4 | +stubtest is a script in the mypy project that compares stubs to the actual objects at runtime. |
| 5 | +Note that therefore the output of stubtest depends on which Python version it is run with. |
| 6 | +In typeshed CI, we run stubtest with each Python minor version from 3.5 through 3.8 inclusive. |
| 7 | +
|
| 8 | +We pin the version of mypy / stubtest we use in .travis.yml so changes to those don't break |
| 9 | +typeshed CI. |
| 10 | +
|
| 11 | +""" |
| 12 | + |
| 13 | +from pathlib import Path |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | + |
| 18 | +def run_stubtest(typeshed_dir: Path) -> int: |
| 19 | + whitelist_dir = typeshed_dir / "tests" / "stubtest_whitelists" |
| 20 | + version_whitelist = "py{}{}.txt".format(sys.version_info.major, sys.version_info.minor) |
| 21 | + |
| 22 | + cmd = [ |
| 23 | + sys.executable, |
| 24 | + "-m", |
| 25 | + "mypy.stubtest", |
| 26 | + # Use --ignore-missing-stub, because if someone makes a correct addition, they'll need to |
| 27 | + # also make a whitelist change and if someone makes an incorrect addition, they'll run into |
| 28 | + # false negatives. |
| 29 | + "--ignore-missing-stub", |
| 30 | + "--check-typeshed", |
| 31 | + "--custom-typeshed-dir", |
| 32 | + str(typeshed_dir), |
| 33 | + "--whitelist", |
| 34 | + str(whitelist_dir / "py3_common.txt"), |
| 35 | + "--whitelist", |
| 36 | + str(whitelist_dir / version_whitelist), |
| 37 | + ] |
| 38 | + if sys.version_info < (3, 8): |
| 39 | + # As discussed in https://github.com/python/typeshed/issues/3693, we only aim for |
| 40 | + # positional-only arg accuracy for the latest Python version. |
| 41 | + cmd += ["--ignore-positional-only"] |
| 42 | + try: |
| 43 | + print(" ".join(cmd), file=sys.stderr) |
| 44 | + subprocess.run(cmd, check=True) |
| 45 | + except subprocess.CalledProcessError as e: |
| 46 | + print( |
| 47 | + "\nNB: stubtest output depends on the Python version it is run with. See README.md " |
| 48 | + "for more details.\n" |
| 49 | + "NB: We only check positional-only arg accuracy for Python 3.8.\n" |
| 50 | + "If stubtest is complaining about 'unused whitelist entry' after your fix, please " |
| 51 | + "remove the entry from the whitelist file. Note you may have to do this for other " |
| 52 | + "version-specific whitelists as well. Thanks for helping burn the backlog of errors!\n" |
| 53 | + "\nCommand run was: {}\n".format(" ".join(cmd)), |
| 54 | + file=sys.stderr, |
| 55 | + ) |
| 56 | + print("stubtest failed", file=sys.stderr) |
| 57 | + return e.returncode |
| 58 | + else: |
| 59 | + print("stubtest succeeded", file=sys.stderr) |
| 60 | + return 0 |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + sys.exit(run_stubtest(typeshed_dir=Path("."))) |
0 commit comments