mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-07 12:44:28 +08:00
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:
@@ -10,6 +10,7 @@
|
||||
# symlinks but that doesn't always work on Windows, so now you must
|
||||
# manually update both files, and this test verifies that they are
|
||||
# identical. The list below indicates which sets of files must match.
|
||||
from __future__ import annotations
|
||||
|
||||
import filecmp
|
||||
import os
|
||||
@@ -22,7 +23,7 @@ metadata_keys = {"version", "requires", "extra_description", "obsolete_since", "
|
||||
allowed_files = {"README.md"}
|
||||
|
||||
|
||||
def assert_stubs_only(directory):
|
||||
def assert_stubs_only(directory: str) -> None:
|
||||
"""Check that given directory contains only valid stub files."""
|
||||
top = directory.split(os.sep)[-1]
|
||||
assert top.isidentifier(), f"Bad directory name: {top}"
|
||||
@@ -37,7 +38,7 @@ def assert_stubs_only(directory):
|
||||
assert subdir.isidentifier(), f"Directories must be valid packages, got: {subdir}"
|
||||
|
||||
|
||||
def check_stdlib():
|
||||
def check_stdlib() -> None:
|
||||
for entry in os.listdir("stdlib"):
|
||||
if os.path.isfile(os.path.join("stdlib", entry)):
|
||||
name, ext = os.path.splitext(entry)
|
||||
@@ -57,7 +58,7 @@ def check_stdlib():
|
||||
assert_stubs_only(os.path.join("stdlib/@python2", entry))
|
||||
|
||||
|
||||
def check_stubs():
|
||||
def check_stubs() -> None:
|
||||
for distribution in os.listdir("stubs"):
|
||||
assert not os.path.isfile(distribution), f"Only directories allowed in stubs, got {distribution}"
|
||||
for entry in os.listdir(os.path.join("stubs", distribution)):
|
||||
@@ -73,7 +74,7 @@ def check_stubs():
|
||||
assert_stubs_only(os.path.join("stubs", distribution, entry))
|
||||
|
||||
|
||||
def check_same_files():
|
||||
def check_same_files() -> None:
|
||||
files = [os.path.join(root, file) for root, dir, files in os.walk(".") for file in files]
|
||||
no_symlink = "You cannot use symlinks in typeshed, please copy {} to its link."
|
||||
for file in files:
|
||||
@@ -94,7 +95,7 @@ def check_same_files():
|
||||
_VERSIONS_RE = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_.]*): [23]\.\d{1,2}-(?:[23]\.\d{1,2})?$")
|
||||
|
||||
|
||||
def check_versions():
|
||||
def check_versions() -> None:
|
||||
versions = set()
|
||||
with open("stdlib/VERSIONS") as f:
|
||||
data = f.read().splitlines()
|
||||
@@ -131,7 +132,7 @@ def _find_stdlib_modules() -> set[str]:
|
||||
return modules
|
||||
|
||||
|
||||
def _strip_dep_version(dependency):
|
||||
def _strip_dep_version(dependency: str) -> tuple[str, str, str]:
|
||||
dep_version_pos = len(dependency)
|
||||
for pos, c in enumerate(dependency):
|
||||
if c in "=<>":
|
||||
@@ -151,7 +152,7 @@ def _strip_dep_version(dependency):
|
||||
return stripped, relation, version
|
||||
|
||||
|
||||
def check_metadata():
|
||||
def check_metadata() -> None:
|
||||
for distribution in os.listdir("stubs"):
|
||||
with open(os.path.join("stubs", distribution, "METADATA.toml")) as f:
|
||||
data = tomli.loads(f.read())
|
||||
|
||||
Reference in New Issue
Block a user