Drop support for Python 2 (#8272)

This commit is contained in:
Alex Waygood
2022-07-12 08:08:56 +01:00
committed by GitHub
parent 4e0aaccdab
commit 78d96cd17e
419 changed files with 15 additions and 24715 deletions

View File

@@ -3,22 +3,13 @@
# For security (and simplicity) reasons, only a limited kind of files can be
# present in /stdlib and /stubs directories, see README for detail. Here we
# verify these constraints.
# In addition, for various reasons we need the contents of certain files to be
# duplicated in two places, for example stdlib/@python2/builtins.pyi and
# stdlib/@python2/__builtin__.pyi must be identical. In the past we used
# 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
import re
import tomli
consistent_files = [{"stdlib/@python2/builtins.pyi", "stdlib/@python2/__builtin__.pyi"}]
metadata_keys = {"version", "requires", "extra_description", "obsolete_since", "no_longer_updated", "tool"}
tool_keys = {"stubtest": {"skip", "apt_dependencies", "ignore_missing_stub"}}
allowed_files = {"README.md"}
@@ -47,16 +38,7 @@ def check_stdlib() -> None:
assert entry == "VERSIONS", f"Unexpected file in stdlib root: {entry}"
assert name.isidentifier(), "Bad file name in stdlib"
else:
if entry == "@python2":
continue
assert_stubs_only(os.path.join("stdlib", entry))
for entry in os.listdir("stdlib/@python2"):
if os.path.isfile(os.path.join("stdlib/@python2", entry)):
name, ext = os.path.splitext(entry)
assert name.isidentifier(), "Bad file name in stdlib"
assert ext == ".pyi", "Unexpected file in stdlib/@python2 root"
else:
assert_stubs_only(os.path.join("stdlib/@python2", entry))
def check_stubs() -> None:
@@ -82,15 +64,6 @@ def check_same_files() -> None:
_, ext = os.path.splitext(file)
if ext == ".pyi" and os.path.islink(file):
raise ValueError(no_symlink.format(file))
for file1, *others in consistent_files:
f1 = os.path.join(os.getcwd(), file1)
for file2 in others:
f2 = os.path.join(os.getcwd(), file2)
if not filecmp.cmp(f1, f2):
raise ValueError(
"File {f1} does not match file {f2}. Please copy it to {f2}\n"
"Run either:\ncp {f1} {f2}\nOr:\ncp {f2} {f1}".format(f1=file1, f2=file2)
)
_VERSIONS_RE = re.compile(r"^([a-zA-Z_][a-zA-Z0-9_.]*): [23]\.\d{1,2}-(?:[23]\.\d{1,2})?$")
@@ -121,8 +94,6 @@ def check_versions() -> None:
def _find_stdlib_modules() -> set[str]:
modules = set()
for path, _, files in os.walk("stdlib"):
if "@python2" in path:
continue
for filename in files:
base_module = ".".join(os.path.normpath(path).split(os.sep)[1:])
if filename == "__init__.pyi":

View File

@@ -96,8 +96,6 @@ def check_new_syntax(tree: ast.AST, path: Path, stub: str) -> list[str]:
def main() -> None:
errors = []
for path in chain(Path("stdlib").rglob("*.pyi"), Path("stubs").rglob("*.pyi")):
if "@python2" in path.parts:
continue
if Path("stubs/protobuf/google/protobuf") in path.parents: # TODO: fix protobuf stubs
continue

View File

@@ -28,7 +28,7 @@ from typing_extensions import Annotated, TypeAlias
import tomli
from colors import colored, print_error, print_success_msg
SUPPORTED_VERSIONS = [(3, 11), (3, 10), (3, 9), (3, 8), (3, 7), (2, 7)]
SUPPORTED_VERSIONS = [(3, 11), (3, 10), (3, 9), (3, 8), (3, 7)]
SUPPORTED_PLATFORMS = frozenset({"linux", "win32", "darwin"})
TYPESHED_DIRECTORIES = frozenset({"stdlib", "stubs", "tests", "test_cases", "scripts"})
@@ -390,22 +390,14 @@ def test_stdlib(code: int, args: TestConfig) -> TestResults:
seen = {"__builtin__", "builtins", "typing"} # Always ignore these.
files: list[str] = []
if args.major == 2:
root = os.path.join("stdlib", "@python2")
for name in os.listdir(root):
mod, _ = os.path.splitext(name)
if mod in seen or mod.startswith("."):
continue
supported_versions = parse_versions(os.path.join("stdlib", "VERSIONS"))
root = "stdlib"
for name in os.listdir(root):
if name == "VERSIONS" or name.startswith("."):
continue
mod, _ = os.path.splitext(name)
if supported_versions[mod][0] <= (args.major, args.minor) <= supported_versions[mod][1]:
add_files(files, seen, root, name, args)
else:
supported_versions = parse_versions(os.path.join("stdlib", "VERSIONS"))
root = "stdlib"
for name in os.listdir(root):
if name == "@python2" or name == "VERSIONS" or name.startswith("."):
continue
mod, _ = os.path.splitext(name)
if supported_versions[mod][0] <= (args.major, args.minor) <= supported_versions[mod][1]:
add_files(files, seen, root, name, args)
if files:
print(f"Testing stdlib ({len(files)} files)...")
@@ -500,9 +492,6 @@ def test_typeshed(code: int, args: TestConfig) -> TestResults:
files_checked_this_version += stdlib_files_checked
print()
if args.major == 2:
return TestResults(code, files_checked_this_version)
if "stubs" in args.directories:
code, third_party_files_checked = test_third_party_stubs(code, args)
files_checked_this_version += third_party_files_checked

View File

@@ -2,9 +2,6 @@
# pytype has its own version of these files, and thus doesn't mind if it
# can't parse the typeshed version:
stdlib/@python2/__builtin__.pyi
stdlib/@python2/builtins.pyi
stdlib/@python2/typing.pyi
stdlib/builtins.pyi
stdlib/typing.pyi

View File

@@ -93,9 +93,7 @@ def _get_relative(filename: str) -> str:
def _get_module_name(filename: str) -> str:
"""Converts a filename {subdir}/m.n/module/foo to module.foo."""
parts = _get_relative(filename).split(os.path.sep)
if "@python2" in parts:
module_parts = parts[parts.index("@python2") + 1 :]
elif parts[0] == "stdlib":
if parts[0] == "stdlib":
module_parts = parts[1:]
else:
assert parts[0] == "stubs"
@@ -123,7 +121,7 @@ def determine_files_to_test(*, typeshed_location: str, paths: Sequence[str]) ->
files = []
for f in sorted(filenames):
rel = _get_relative(f)
if rel in skipped or "@python2" in f:
if rel in skipped:
continue
files.append(f)
return files

View File

@@ -3,7 +3,7 @@
stubtest is a script in the mypy project that compares stubs to the actual objects at runtime.
Note that therefore the output of stubtest depends on which Python version it is run with.
In typeshed CI, we run stubtest with each currently supported Python minor version, except 2.7.
In typeshed CI, we run stubtest with each currently supported Python minor version.
"""