Skip to content

Commit be1a4e8

Browse files
Adjust typing check to use wrapper/shim script for now.
1 parent 1443cd1 commit be1a4e8

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

Diff for: tests/verify_types.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""This script is a shim around `pyright --verifytypes` to determine if the
2+
current typing coverage meets the expected coverage. The previous command by
3+
itself won't suffice, since its expected coverage can't be modified from 100%.
4+
Useful while still adding annotations to the library.
5+
"""
6+
7+
import argparse
8+
import json
9+
import subprocess
10+
from decimal import Decimal
11+
12+
PYRIGHT_CMD = ("pyright", "--verifytypes", "rfc3986", "--outputjson")
13+
14+
15+
def validate_coverage(inp: str) -> Decimal:
16+
"""Ensure the given coverage score is between 0 and 100 (inclusive)."""
17+
18+
coverage = Decimal(inp)
19+
if not (0 <= coverage <= 100):
20+
raise ValueError
21+
return coverage
22+
23+
24+
def main() -> int:
25+
"""Determine if rfc3986's typing coverage meets our expected coverage."""
26+
27+
parser = argparse.ArgumentParser()
28+
parser.add_argument(
29+
"--fail-under",
30+
default=Decimal("75"),
31+
type=validate_coverage,
32+
help="The target typing coverage to not fall below (default: 75).",
33+
)
34+
parser.add_argument(
35+
"--quiet",
36+
action="store_true",
37+
help="Whether to hide the full output from `pyright --verifytypes`.",
38+
)
39+
40+
args = parser.parse_args()
41+
42+
expected_coverage: Decimal = args.fail_under / 100
43+
quiet: bool = args.quiet
44+
45+
try:
46+
output = subprocess.check_output(
47+
PYRIGHT_CMD,
48+
stderr=subprocess.STDOUT,
49+
text=True,
50+
)
51+
except subprocess.CalledProcessError as exc:
52+
output = exc.output
53+
54+
verifytypes_output = json.loads(output)
55+
raw_score = verifytypes_output["typeCompleteness"]["completenessScore"]
56+
actual_coverage = Decimal(raw_score)
57+
58+
if not quiet:
59+
# Switch back to normal output instead of json, for readability.
60+
subprocess.run(PYRIGHT_CMD[:-1])
61+
62+
if actual_coverage >= expected_coverage:
63+
print(
64+
f"OK - Required typing coverage of {expected_coverage:.2%} "
65+
f"reached. Total typing coverage: {actual_coverage:.2%}."
66+
)
67+
return 0
68+
else:
69+
print(
70+
f"FAIL - Required typing coverage of {expected_coverage:.2%} not "
71+
f"reached. Total typing coverage: {actual_coverage:.2%}."
72+
)
73+
return 1
74+
75+
76+
if __name__ == "__main__":
77+
raise SystemExit(main())

Diff for: tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ commands = flake8 {posargs} src/rfc3986
4343
[testenv:typing]
4444
deps =
4545
pyright
46-
commands = pyright {posargs:--verifytypes rfc3986}
46+
commands = python tests/verify_types.py
4747

4848
[testenv:venv]
4949
commands = {posargs}

0 commit comments

Comments
 (0)