Skip to content

Commit f7aa412

Browse files
authored
Drop Python 2 support in third-party stubs (#7703)
1 parent 2d46896 commit f7aa412

File tree

19 files changed

+17
-67
lines changed

19 files changed

+17
-67
lines changed

CONTRIBUTING.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ supported:
159159
When the stubs are updated to a newer version
160160
of the library, the version of the stub should be bumped (note that
161161
previous versions are still available on PyPI).
162-
* `python2` (default: `false`): If set to `true`, the top-level stubs
163-
support both Python 2 and Python 3.
164162
* `requires` (optional): A list of other stub packages or packages with type
165163
information that are imported by the stubs in this package. Only packages
166164
generated by typeshed or required by the upstream package are allowed to

stubs/Deprecated/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
version = "1.2.*"
2-
python2 = true
32
requires = []
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "3.7.*"
2-
python2 = true

stubs/certifi/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "2021.10.8"
2-
python2 = true

stubs/characteristic/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "14.3.*"
2-
python2 = true

stubs/colorama/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "0.4.*"
2-
python2 = true

stubs/docopt/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# Prior to v0.6, docopt() had only 3 optional args
22
version = "0.6.*"
3-
python2 = true

stubs/first/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "2.0.*"
2-
python2 = true

stubs/mypy-extensions/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "0.4.*"
2-
python2 = true

stubs/pyaudio/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
version = "0.2.*"
2-
python2 = true
32
stubtest_apt_dependencies = ["portaudio19-dev"]

stubs/pytz/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "2021.3"
2-
python2 = true

stubs/retry/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "0.9.*"
2-
python2 = true

stubs/singledispatch/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "3.7.*"
2-
python2 = true

stubs/tabulate/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "0.8.*"
2-
python2 = true

stubs/xxhash/METADATA.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
version = "2.0.*"
2-
python2 = true

tests/check_consistent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import tomli
1919

2020
consistent_files = [{"stdlib/@python2/builtins.pyi", "stdlib/@python2/__builtin__.pyi"}]
21-
metadata_keys = {"version", "python2", "requires", "extra_description", "obsolete_since", "stubtest", "stubtest_apt_dependencies"}
21+
metadata_keys = {"version", "requires", "extra_description", "obsolete_since", "stubtest", "stubtest_apt_dependencies"}
2222
allowed_files = {"README.md"}
2323

2424

@@ -162,7 +162,6 @@ def check_metadata():
162162
assert re.fullmatch(r"\d+(\.\d+)+|\d+(\.\d+)*\.\*", version), msg
163163
for key in data:
164164
assert key in metadata_keys, f"Unexpected key {key} for {distribution}"
165-
assert isinstance(data.get("python2", False), bool), f"Invalid python2 value for {distribution}"
166165
assert isinstance(data.get("requires", []), list), f"Invalid requires value for {distribution}"
167166
for dep in data.get("requires", []):
168167
assert isinstance(dep, str), f"Invalid dependency {dep} for {distribution}"

tests/check_new_syntax.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,10 @@
55
from itertools import chain
66
from pathlib import Path
77

8-
STUBS_SUPPORTING_PYTHON_2 = frozenset(
9-
path.parent for path in Path("stubs").rglob("METADATA.toml") if "python2 = true" in path.read_text().splitlines()
10-
)
11-
128

139
def check_new_syntax(tree: ast.AST, path: Path, stub: str) -> list[str]:
1410
errors = []
1511
sourcelines = stub.splitlines()
16-
python_2_support_required = any(directory in path.parents for directory in STUBS_SUPPORTING_PYTHON_2)
1712

1813
class AnnotationUnionFinder(ast.NodeVisitor):
1914
def visit_Subscript(self, node: ast.Subscript) -> None:
@@ -103,10 +98,9 @@ def visit_If(self, node: ast.If) -> None:
10398
)
10499
self.generic_visit(node)
105100

106-
if not python_2_support_required:
107-
ObjectClassdefFinder().visit(tree)
108-
if path != Path("stdlib/typing_extensions.pyi"):
109-
TextFinder().visit(tree)
101+
ObjectClassdefFinder().visit(tree)
102+
if path != Path("stdlib/typing_extensions.pyi"):
103+
TextFinder().visit(tree)
110104

111105
OldSyntaxFinder().visit(tree)
112106
IfFinder().visit(tree)

tests/mypy_test.py

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import re
1616
import sys
1717
import tempfile
18-
from glob import glob
1918
from pathlib import Path
2019
from typing import Dict, NamedTuple
2120

@@ -93,20 +92,6 @@ def parse_version(v_str):
9392
return int(m.group(1)), int(m.group(2))
9493

9594

96-
def is_supported(distribution_path: Path, major: int) -> bool:
97-
data = dict(tomli.loads((distribution_path / "METADATA.toml").read_text()))
98-
if major == 2:
99-
# Python 2 is not supported by default.
100-
return bool(data.get("python2", False))
101-
# Python 3 is supported by default.
102-
return has_py3_stubs(distribution_path)
103-
104-
105-
# Keep this in sync with stubtest_third_party.py
106-
def has_py3_stubs(dist: Path) -> bool:
107-
return len(glob(f"{dist}/*.pyi")) > 0 or len(glob(f"{dist}/[!@]*/__init__.pyi")) > 0
108-
109-
11095
def add_files(files, seen, root, name, args):
11196
"""Add all files in package or module represented by 'name' located in 'root'."""
11297
full = os.path.join(root, name)
@@ -326,22 +311,20 @@ def main():
326311
files_checked += len(files)
327312

328313
# Test files of all third party distributions.
329-
print("Running mypy " + " ".join(get_mypy_flags(args, major, minor, "/tmp/...")))
330-
for distribution in sorted(os.listdir("stubs")):
331-
if distribution == "SQLAlchemy":
332-
continue # Crashes
314+
if major != 2:
315+
print("Running mypy " + " ".join(get_mypy_flags(args, major, minor, "/tmp/...")))
316+
for distribution in sorted(os.listdir("stubs")):
317+
if distribution == "SQLAlchemy":
318+
continue # Crashes
333319

334-
distribution_path = Path("stubs", distribution)
320+
distribution_path = Path("stubs", distribution)
335321

336-
if not is_probably_stubs_folder(distribution, distribution_path):
337-
continue
338-
339-
if not is_supported(distribution_path, major):
340-
continue
322+
if not is_probably_stubs_folder(distribution, distribution_path):
323+
continue
341324

342-
this_code, checked = test_third_party_distribution(distribution, major, minor, args)
343-
code = max(code, this_code)
344-
files_checked += checked
325+
this_code, checked = test_third_party_distribution(distribution, major, minor, args)
326+
code = max(code, this_code)
327+
files_checked += checked
345328

346329
print()
347330

tests/stubtest_third_party.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import sys
1010
import tempfile
1111
import venv
12-
from glob import glob
1312
from pathlib import Path
14-
from typing import Any, NoReturn
13+
from typing import NoReturn
1514

1615
import tomli
1716

@@ -26,7 +25,7 @@ def run_stubtest(dist: Path) -> bool:
2625
with open(dist / "METADATA.toml") as f:
2726
metadata = dict(tomli.loads(f.read()))
2827

29-
if not run_stubtest_for(metadata, dist):
28+
if not metadata.get("stubtest", True):
3029
print(f"Skipping stubtest for {dist.name}\n\n")
3130
return True
3231

@@ -109,15 +108,6 @@ def run_stubtest(dist: Path) -> bool:
109108
return True
110109

111110

112-
def run_stubtest_for(metadata: dict[str, Any], dist: Path) -> bool:
113-
return has_py3_stubs(dist) and metadata.get("stubtest", True)
114-
115-
116-
# Keep this in sync with mypy_test.py
117-
def has_py3_stubs(dist: Path) -> bool:
118-
return len(glob(f"{dist}/*.pyi")) > 0 or len(glob(f"{dist}/[!@]*/__init__.pyi")) > 0
119-
120-
121111
def main() -> NoReturn:
122112
parser = argparse.ArgumentParser()
123113
parser.add_argument("--num-shards", type=int, default=1)

0 commit comments

Comments
 (0)