Skip to content

Commit 916e05a

Browse files
srittauJelleZijlstrahamdanal
authored
Unify allowlist handling (#11889)
Co-authored-by: Jelle Zijlstra <[email protected]> Co-authored-by: Ali Hamdan <[email protected]>
1 parent b570af5 commit 916e05a

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

tests/stubtest_stdlib.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
In typeshed CI, we run stubtest with each currently supported Python minor version.
77
88
"""
9+
910
from __future__ import annotations
1011

1112
import subprocess
1213
import sys
1314
from pathlib import Path
1415

16+
from utils import allowlist_stubtest_arguments, allowlists_path
17+
1518

1619
def run_stubtest(typeshed_dir: Path) -> int:
17-
allowlist_dir = typeshed_dir / "tests" / "stubtest_allowlists"
1820
version_allowlist = f"py{sys.version_info.major}{sys.version_info.minor}.txt"
19-
platform_allowlist = f"{sys.platform}.txt"
2021
combined_allowlist = f"{sys.platform}-py{sys.version_info.major}{sys.version_info.minor}.txt"
2122
local_version_allowlist = version_allowlist + ".local"
23+
extra_allowlists = [version_allowlist, combined_allowlist, local_version_allowlist]
2224

2325
# Note when stubtest imports distutils, it will likely actually import setuptools._distutils
2426
# This is fine because we don't care about distutils and allowlist all errors from it
@@ -31,17 +33,8 @@ def run_stubtest(typeshed_dir: Path) -> int:
3133
"--check-typeshed",
3234
"--custom-typeshed-dir",
3335
str(typeshed_dir),
34-
"--allowlist",
35-
str(allowlist_dir / "py3_common.txt"),
36-
"--allowlist",
37-
str(allowlist_dir / version_allowlist),
36+
*allowlist_stubtest_arguments("stdlib", extra_allowlists),
3837
]
39-
if (allowlist_dir / platform_allowlist).exists():
40-
cmd += ["--allowlist", str(allowlist_dir / platform_allowlist)]
41-
if (allowlist_dir / combined_allowlist).exists():
42-
cmd += ["--allowlist", str(allowlist_dir / combined_allowlist)]
43-
if (allowlist_dir / local_version_allowlist).exists():
44-
cmd += ["--allowlist", str(allowlist_dir / local_version_allowlist)]
4538
if sys.version_info < (3, 10):
4639
# As discussed in https://github.com/python/typeshed/issues/3693, we only aim for
4740
# positional-only arg accuracy for python 3.10 and above.
@@ -58,7 +51,10 @@ def run_stubtest(typeshed_dir: Path) -> int:
5851
file=sys.stderr,
5952
)
6053
print("\n\n", file=sys.stderr)
61-
print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_dir}', file=sys.stderr)
54+
print(
55+
f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlists_path("stdlib")}',
56+
file=sys.stderr,
57+
)
6258
return e.returncode
6359
else:
6460
print("stubtest succeeded", file=sys.stderr)

tests/stubtest_third_party.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313
from typing import NoReturn
1414

1515
from parse_metadata import NoSuchStubError, get_recursive_requirements, read_metadata
16-
from utils import PYTHON_VERSION, colored, get_mypy_req, print_divider, print_error, print_success_msg, tests_path
16+
from utils import (
17+
PYTHON_VERSION,
18+
allowlist_stubtest_arguments,
19+
allowlists_path,
20+
colored,
21+
get_mypy_req,
22+
print_divider,
23+
print_error,
24+
print_success_msg,
25+
tests_path,
26+
)
1727

1828

1929
def run_stubtest(
@@ -112,12 +122,7 @@ def run_stubtest(
112122
# "bisect" to see which variables are actually needed.
113123
stubtest_env = os.environ | {"MYPYPATH": mypypath, "MYPY_FORCE_COLOR": "1"}
114124

115-
allowlist_path = tests_path(dist_name) / "stubtest_allowlist.txt"
116-
if allowlist_path.exists():
117-
stubtest_cmd.extend(["--allowlist", str(allowlist_path)])
118-
platform_allowlist = tests_path(dist_name) / f"stubtest_allowlist_{sys.platform}.txt"
119-
if platform_allowlist.exists():
120-
stubtest_cmd.extend(["--allowlist", str(platform_allowlist)])
125+
stubtest_cmd += allowlist_stubtest_arguments(dist_name, [])
121126

122127
# Perform some black magic in order to run stubtest inside uWSGI
123128
if dist_name == "uWSGI":
@@ -150,11 +155,12 @@ def run_stubtest(
150155
print_command_output(ret)
151156

152157
print_divider()
153-
if allowlist_path.exists():
154-
print(f'To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_path}')
158+
main_allowlist_path = allowlists_path(dist_name) / "stubtest_allowlist.txt"
159+
if main_allowlist_path.exists():
160+
print(f'To fix "unused allowlist" errors, remove the corresponding entries from {main_allowlist_path}')
155161
print()
156162
else:
157-
print(f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path}:")
163+
print(f"Re-running stubtest with --generate-allowlist.\nAdd the following to {main_allowlist_path}:")
158164
ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True)
159165
print_command_output(ret)
160166

tests/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ def get_all_testcase_directories() -> list[DistributionTests]:
161161
return [distribution_info("stdlib"), *sorted(testcase_directories)]
162162

163163

164+
def allowlists_path(distribution_name: str) -> Path:
165+
if distribution_name == "stdlib":
166+
return Path("tests", "stubtest_allowlists")
167+
else:
168+
return tests_path(distribution_name)
169+
170+
171+
def common_allowlists(distribution_name: str) -> list[str]:
172+
if distribution_name == "stdlib":
173+
return ["py3_common.txt", f"{sys.platform}.txt"]
174+
else:
175+
return ["stubtest_allowlist.txt", f"stubtest_allowlist_{sys.platform}.txt"]
176+
177+
164178
# ====================================================================
165179
# Parsing .gitignore
166180
# ====================================================================
@@ -177,3 +191,17 @@ def spec_matches_path(spec: pathspec.PathSpec, path: Path) -> bool:
177191
if path.is_dir():
178192
normalized_path += "/"
179193
return spec.match_file(normalized_path)
194+
195+
196+
# ====================================================================
197+
# mypy call
198+
# ====================================================================
199+
200+
201+
def allowlist_stubtest_arguments(distribution_name: str, additional_allowlists: list[str]) -> list[str]:
202+
stubtest_arguments: list[str] = []
203+
for allowlist in common_allowlists(distribution_name) + additional_allowlists:
204+
path = allowlists_path(distribution_name) / allowlist
205+
if path.exists():
206+
stubtest_arguments.extend(["--allowlist", str(path)])
207+
return stubtest_arguments

0 commit comments

Comments
 (0)