Make test scripts pass mypy --strict (#7745)

- Add several type hints to untyped functions.
- While we're at it, upgrade several annotations to use modern syntax by using `from __future__ import annotations`. This means that the tests can't be run on Python 3.6, but 3.6 is EOL, and it is already the case that some scripts in this directory can only be run on more recent Python versions. E.g. `check_new_syntax.py` uses `ast.unparse`, which is only available in Python 3.9+.
- Fix a few pieces of code that didn't type check.
This commit is contained in:
Alex Waygood
2022-04-29 21:55:12 -06:00
committed by GitHub
parent 145e659482
commit 9d450cb50c
4 changed files with 41 additions and 39 deletions

View File

@@ -11,21 +11,19 @@ Option two will load the file and all the builtins, typeshed dependencies. This
will also discover incorrect usage of imported modules.
"""
from __future__ import annotations
import argparse
import os
import sys
import traceback
from typing import List, Optional, Sequence
from collections.abc import Sequence
from pytype import config as pytype_config, load_pytd
from pytype.pytd import typeshed
TYPESHED_SUBDIRS = ["stdlib", "stubs"]
TYPESHED_HOME = "TYPESHED_HOME"
UNSET = object() # marker for tracking the TYPESHED_HOME environment variable
_LOADERS = {}
@@ -34,13 +32,13 @@ def main() -> None:
typeshed_location = args.typeshed_location or os.getcwd()
subdir_paths = [os.path.join(typeshed_location, d) for d in TYPESHED_SUBDIRS]
check_subdirs_discoverable(subdir_paths)
old_typeshed_home = os.environ.get(TYPESHED_HOME, UNSET)
old_typeshed_home = os.environ.get(TYPESHED_HOME)
os.environ[TYPESHED_HOME] = typeshed_location
files_to_test = determine_files_to_test(typeshed_location=typeshed_location, paths=args.files or subdir_paths)
run_all_tests(
files_to_test=files_to_test, typeshed_location=typeshed_location, print_stderr=args.print_stderr, dry_run=args.dry_run
)
if old_typeshed_home is UNSET:
if old_typeshed_home is None:
del os.environ[TYPESHED_HOME]
else:
os.environ[TYPESHED_HOME] = old_typeshed_home
@@ -61,13 +59,14 @@ def create_parser() -> argparse.ArgumentParser:
return parser
def run_pytype(*, filename: str, python_version: str, typeshed_location: str) -> Optional[str]:
def run_pytype(*, filename: str, python_version: str, typeshed_location: str) -> str | None:
"""Runs pytype, returning the stderr if any."""
if python_version not in _LOADERS:
options = pytype_config.Options.create("", parse_pyi=True, python_version=python_version)
loader = load_pytd.create_loader(options)
_LOADERS[python_version] = (options, loader)
options, loader = _LOADERS[python_version]
stderr: str | None
try:
with pytype_config.verbosity_from(options):
ast = loader.load_file(_get_module_name(filename), filename)
@@ -108,13 +107,13 @@ def _is_version(path: str, version: str) -> bool:
return any("{}{}{}".format(d, os.path.sep, version) in path for d in TYPESHED_SUBDIRS)
def check_subdirs_discoverable(subdir_paths: List[str]) -> None:
def check_subdirs_discoverable(subdir_paths: list[str]) -> None:
for p in subdir_paths:
if not os.path.isdir(p):
raise SystemExit("Cannot find typeshed subdir at {} (specify parent dir via --typeshed-location)".format(p))
def determine_files_to_test(*, typeshed_location: str, paths: Sequence[str]) -> List[str]:
def determine_files_to_test(*, typeshed_location: str, paths: Sequence[str]) -> list[str]:
"""Determine all files to test, checking if it's in the exclude list and which Python versions to use.
Returns a list of pairs of the file path and Python version as an int."""
@@ -130,8 +129,8 @@ def determine_files_to_test(*, typeshed_location: str, paths: Sequence[str]) ->
return files
def find_stubs_in_paths(paths: Sequence[str]) -> List[str]:
filenames = []
def find_stubs_in_paths(paths: Sequence[str]) -> list[str]:
filenames: list[str] = []
for path in paths:
if os.path.isdir(path):
for root, _, fns in os.walk(path):