Skip to content

Commit 002c8e2

Browse files
authored
Cleanup mypy_test.py (#7738)
1 parent c3ebc7e commit 002c8e2

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

tests/mypy_test.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
import sys
1717
import tempfile
1818
from pathlib import Path
19-
from typing import Dict, NamedTuple
19+
from typing import TYPE_CHECKING, Dict, List, NamedTuple, Set, Tuple
20+
21+
if TYPE_CHECKING:
22+
from _typeshed import StrPath
23+
24+
from typing_extensions import TypeAlias
2025

2126
import tomli
2227

@@ -26,23 +31,15 @@
2631
parser.add_argument("-x", "--exclude", type=str, nargs="*", help="Exclude pattern")
2732
parser.add_argument("-p", "--python-version", type=str, nargs="*", help="These versions only (major[.minor])")
2833
parser.add_argument("--platform", help="Run mypy for a certain OS platform (defaults to sys.platform)")
29-
parser.add_argument(
30-
"--warn-unused-ignores",
31-
action="store_true",
32-
help="Run mypy with --warn-unused-ignores "
33-
"(hint: only get rid of warnings that are "
34-
"unused for all platforms and Python versions)",
35-
)
36-
3734
parser.add_argument("filter", type=str, nargs="*", help="Include pattern (default all)")
3835

3936

40-
def log(args, *varargs):
37+
def log(args: argparse.Namespace, *varargs: object) -> None:
4138
if args.verbose >= 2:
4239
print(*varargs)
4340

4441

45-
def match(fn, args):
42+
def match(fn: str, args: argparse.Namespace) -> bool:
4643
if not args.filter and not args.exclude:
4744
log(args, fn, "accept by default")
4845
return True
@@ -64,9 +61,10 @@ def match(fn, args):
6461

6562

6663
_VERSION_LINE_RE = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_.]*): ([23]\.\d{1,2})-([23]\.\d{1,2})?$")
64+
VersionsTuple: TypeAlias = Tuple[int, int]
6765

6866

69-
def parse_versions(fname):
67+
def parse_versions(fname: "StrPath") -> Dict[str, Tuple[VersionsTuple, VersionsTuple]]:
7068
result = {}
7169
with open(fname) as f:
7270
for line in f:
@@ -76,7 +74,7 @@ def parse_versions(fname):
7674
continue
7775
m = _VERSION_LINE_RE.match(line)
7876
assert m, "invalid VERSIONS line: " + line
79-
mod = m.group(1)
77+
mod: str = m.group(1)
8078
min_version = parse_version(m.group(2))
8179
max_version = parse_version(m.group(3)) if m.group(3) else (99, 99)
8280
result[mod] = min_version, max_version
@@ -86,13 +84,13 @@ def parse_versions(fname):
8684
_VERSION_RE = re.compile(r"^([23])\.(\d+)$")
8785

8886

89-
def parse_version(v_str):
87+
def parse_version(v_str: str) -> Tuple[int, int]:
9088
m = _VERSION_RE.match(v_str)
9189
assert m, "invalid version: " + v_str
9290
return int(m.group(1)), int(m.group(2))
9391

9492

95-
def add_files(files, seen, root, name, args):
93+
def add_files(files: List[str], seen: Set[str], root: str, name: str, args: argparse.Namespace) -> None:
9694
"""Add all files in package or module represented by 'name' located in 'root'."""
9795
full = os.path.join(root, name)
9896
mod, ext = os.path.splitext(name)
@@ -127,7 +125,7 @@ class MypyDistConf(NamedTuple):
127125
# disallow_untyped_defs = true
128126

129127

130-
def add_configuration(configurations: list[MypyDistConf], distribution: str) -> None:
128+
def add_configuration(configurations: List[MypyDistConf], distribution: str) -> None:
131129
with open(os.path.join("stubs", distribution, "METADATA.toml")) as f:
132130
data = dict(tomli.loads(f.read()))
133131

@@ -150,7 +148,15 @@ def add_configuration(configurations: list[MypyDistConf], distribution: str) ->
150148
configurations.append(MypyDistConf(module_name, values.copy()))
151149

152150

153-
def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False):
151+
def run_mypy(
152+
args: argparse.Namespace,
153+
configurations: List[MypyDistConf],
154+
major: int,
155+
minor: int,
156+
files: List[str],
157+
*,
158+
custom_typeshed: bool = False,
159+
) -> int:
154160
try:
155161
from mypy.api import run as mypy_run
156162
except ImportError:
@@ -178,7 +184,9 @@ def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False
178184
return exit_code
179185

180186

181-
def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_typeshed: bool = False) -> list[str]:
187+
def get_mypy_flags(
188+
args: argparse.Namespace, major: int, minor: int, temp_name: str, *, custom_typeshed: bool = False
189+
) -> List[str]:
182190
flags = [
183191
"--python-version",
184192
"%d.%d" % (major, minor),
@@ -200,14 +208,12 @@ def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_types
200208
# Setting custom typeshed dir prevents mypy from falling back to its bundled
201209
# typeshed in case of stub deletions
202210
flags.extend(["--custom-typeshed-dir", os.path.dirname(os.path.dirname(__file__))])
203-
if args.warn_unused_ignores:
204-
flags.append("--warn-unused-ignores")
205211
if args.platform:
206212
flags.extend(["--platform", args.platform])
207213
return flags
208214

209215

210-
def read_dependencies(distribution: str) -> list[str]:
216+
def read_dependencies(distribution: str) -> List[str]:
211217
with open(os.path.join("stubs", distribution, "METADATA.toml")) as f:
212218
data = dict(tomli.loads(f.read()))
213219
requires = data.get("requires", [])
@@ -221,7 +227,12 @@ def read_dependencies(distribution: str) -> list[str]:
221227

222228

223229
def add_third_party_files(
224-
distribution: str, major: int, files: list[str], args, configurations: list[MypyDistConf], seen_dists: set[str]
230+
distribution: str,
231+
major: int,
232+
files: List[str],
233+
args: argparse.Namespace,
234+
configurations: List[MypyDistConf],
235+
seen_dists: Set[str],
225236
) -> None:
226237
if distribution in seen_dists:
227238
return
@@ -240,16 +251,16 @@ def add_third_party_files(
240251
add_configuration(configurations, distribution)
241252

242253

243-
def test_third_party_distribution(distribution: str, major: int, minor: int, args) -> tuple[int, int]:
254+
def test_third_party_distribution(distribution: str, major: int, minor: int, args: argparse.Namespace) -> Tuple[int, int]:
244255
"""Test the stubs of a third-party distribution.
245256
246257
Return a tuple, where the first element indicates mypy's return code
247258
and the second element is the number of checked files.
248259
"""
249260

250-
files: list[str] = []
251-
configurations: list[MypyDistConf] = []
252-
seen_dists: set[str] = set()
261+
files: List[str] = []
262+
configurations: List[MypyDistConf] = []
263+
seen_dists: Set[str] = set()
253264
add_third_party_files(distribution, major, files, args, configurations, seen_dists)
254265

255266
print(f"testing {distribution} ({len(files)} files)...")
@@ -267,7 +278,7 @@ def is_probably_stubs_folder(distribution: str, distribution_path: Path) -> bool
267278
return distribution != ".mypy_cache" and distribution_path.is_dir()
268279

269280

270-
def main():
281+
def main() -> None:
271282
args = parser.parse_args()
272283

273284
versions = [(3, 11), (3, 10), (3, 9), (3, 8), (3, 7), (3, 6), (2, 7)]
@@ -285,7 +296,7 @@ def main():
285296
seen = {"__builtin__", "builtins", "typing"} # Always ignore these.
286297

287298
# Test standard library files.
288-
files = []
299+
files: List[str] = []
289300
if major == 2:
290301
root = os.path.join("stdlib", "@python2")
291302
for name in os.listdir(root):

0 commit comments

Comments
 (0)