Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48970d3

Browse files
authored
tests / scripts: blacken and isort (#4704)
I often run black and isort in typeshed root and then have to undo these changes. Co-authored-by: hauntsaninja <>
1 parent 2f13673 commit 48970d3

9 files changed

Lines changed: 97 additions & 113 deletions

File tree

.flake8

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
[flake8]
1616
per-file-ignores =
17+
*.py: E203, W503
1718
*.pyi: E301, E302, E305, E501, E701, E741, F401, F403, F405, F822
1819
# Since typing.pyi defines "overload" this is not recognized by flake8 as typing.overload.
1920
# Unfortunately, flake8 does not allow to "noqa" just a specific error inside the file itself.

scripts/migrate_script.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
import os
88
import os.path
99
import shutil
10-
1110
from dataclasses import dataclass
12-
from typing import Optional, List, Set, Tuple
11+
from typing import List, Optional, Set, Tuple
1312

1413
# These names may be still discussed so I make them constants.
1514
STDLIB_NAMESPACE = "stdlib"
@@ -62,6 +61,7 @@
6261
# The latter two are distinguished by is_dir flag.
6362
class PackageBase:
6463
"""Common attributes for packages/modules"""
64+
6565
path: str # full initial path like stdlib/2and3/argparse.pyi
6666
is_dir: bool
6767

@@ -79,6 +79,7 @@ def name(self) -> str:
7979
@dataclass
8080
class StdLibPackage(PackageBase):
8181
"""Package/module in standard library."""
82+
8283
path: str
8384
py_version: Optional[str] # Can be omitted for Python 2 only packages.
8485
is_dir: bool
@@ -93,8 +94,7 @@ class ThirdPartyPackage(PackageBase):
9394
requires: List[str] # distributions this depends on
9495

9596

96-
def add_stdlib_packages_from(subdir: str, packages: List[StdLibPackage],
97-
py_version: Optional[str]) -> None:
97+
def add_stdlib_packages_from(subdir: str, packages: List[StdLibPackage], py_version: Optional[str]) -> None:
9898
"""Add standard library packages/modules from a given stdlib/xxx subdirectory.
9999
100100
Append to packages list in-place, use py_version as the minimal supported version.
@@ -120,30 +120,27 @@ def collect_stdlib_packages() -> Tuple[List[StdLibPackage], List[StdLibPackage]]
120120
return stdlib, py2_stdlib
121121

122122

123-
def add_third_party_packages_from(subdir: str, packages: List[ThirdPartyPackage],
124-
py2_compatible: bool, py3_compatible: bool) -> None:
123+
def add_third_party_packages_from(
124+
subdir: str, packages: List[ThirdPartyPackage], py2_compatible: bool, py3_compatible: bool
125+
) -> None:
125126
"""Add third party packages/modules from a given third_party/xxx subdirectory."""
126127
for name in os.listdir(subdir):
127128
path = os.path.join(subdir, name)
128-
packages.append(ThirdPartyPackage(path, py2_compatible, py3_compatible,
129-
requires=[], is_dir=os.path.isdir(path)))
129+
packages.append(ThirdPartyPackage(path, py2_compatible, py3_compatible, requires=[], is_dir=os.path.isdir(path)))
130130

131131

132132
def collect_third_party_packages() -> Tuple[List[ThirdPartyPackage], List[ThirdPartyPackage]]:
133133
"""Collect third party packages/modules from all current third_party/xxx sub-directories."""
134134
third_party: List[ThirdPartyPackage] = []
135135
py2_third_party: List[ThirdPartyPackage] = []
136-
add_third_party_packages_from("third_party/3", third_party,
137-
py2_compatible=False, py3_compatible=True)
138-
add_third_party_packages_from("third_party/2and3", third_party,
139-
py2_compatible=True, py3_compatible=True)
136+
add_third_party_packages_from("third_party/3", third_party, py2_compatible=False, py3_compatible=True)
137+
add_third_party_packages_from("third_party/2and3", third_party, py2_compatible=True, py3_compatible=True)
140138
# We special-case Python 2 for third party packages like six.
141139
subdir = "third_party/2"
142140
py3_packages = os.listdir("third_party/3")
143141
for name in os.listdir(subdir):
144142
path = os.path.join(subdir, name)
145-
package = ThirdPartyPackage(path, py2_compatible=True, py3_compatible=False,
146-
requires=[], is_dir=os.path.isdir(path))
143+
package = ThirdPartyPackage(path, py2_compatible=True, py3_compatible=False, requires=[], is_dir=os.path.isdir(path))
147144
if name in py3_packages:
148145
# If there is a package with the same name in /2 and /3, we add the former to
149146
# a separate list, packages from there will be put into /python2 sub-directories.
@@ -168,19 +165,19 @@ def get_top_imported_names(file: str) -> Set[str]:
168165
for node in ast.walk(parsed):
169166
if isinstance(node, ast.Import):
170167
for name in node.names:
171-
top_imported.add(name.name.split('.')[0])
168+
top_imported.add(name.name.split(".")[0])
172169
elif isinstance(node, ast.ImportFrom):
173170
if node.level > 0:
174171
# Relative imports always refer to the current package.
175172
continue
176173
assert node.module
177-
top_imported.add(node.module.split('.')[0])
174+
top_imported.add(node.module.split(".")[0])
178175
return top_imported
179176

180177

181-
def populate_requirements(package: ThirdPartyPackage,
182-
stdlib: List[str], py2_stdlib: List[str],
183-
known_distributions: Set[str]) -> None:
178+
def populate_requirements(
179+
package: ThirdPartyPackage, stdlib: List[str], py2_stdlib: List[str], known_distributions: Set[str]
180+
) -> None:
184181
"""Generate requirements using imports found in a package."""
185182
assert not package.requires, "Populate must be called once"
186183
if not package.is_dir:
@@ -272,14 +269,12 @@ def generate_metadata(package: ThirdPartyPackage, py2_packages: List[str]) -> st
272269
if not package.py3_compatible:
273270
lines.append("python3 = false")
274271
if package.requires:
275-
distributions = [f'"types-{package_to_distribution.get(dep, dep)}"'
276-
for dep in package.requires]
272+
distributions = [f'"types-{package_to_distribution.get(dep, dep)}"' for dep in package.requires]
277273
lines.append(f"requires = [{', '.join(distributions)}]")
278274
return "\n".join(lines)
279275

280276

281-
def copy_third_party(packages: List[ThirdPartyPackage],
282-
py2_packages: List[ThirdPartyPackage]) -> None:
277+
def copy_third_party(packages: List[ThirdPartyPackage], py2_packages: List[ThirdPartyPackage]) -> None:
283278
"""Refactor the third party part using collected metadata."""
284279
third_party_dir = os.path.join(OUTPUT_DIR, THIRD_PARTY_NAMESPACE)
285280
os.makedirs(third_party_dir, exist_ok=True)
@@ -325,8 +320,7 @@ def main() -> None:
325320
py2_stdlib_names += [package.name for package in stdlib if package.py_version == "2.7"]
326321

327322
# Collect all known distributions (for sanity checks).
328-
known_distributions = {package_to_distribution.get(package.name, package.name)
329-
for package in third_party + py2_third_party}
323+
known_distributions = {package_to_distribution.get(package.name, package.name) for package in third_party + py2_third_party}
330324

331325
# Compute dependencies between third party packages/modules to populate metadata.
332326
for package in third_party + py2_third_party:

tests/check_consistent.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
# manually update both files, and this test verifies that they are
88
# identical. The list below indicates which sets of files must match.
99

10-
import os
1110
import filecmp
11+
import os
1212

1313
consistent_files = [
14-
{'stdlib/2/builtins.pyi', 'stdlib/2/__builtin__.pyi'},
15-
{'stdlib/2and3/threading.pyi', 'stdlib/2and3/_dummy_threading.pyi'},
14+
{"stdlib/2/builtins.pyi", "stdlib/2/__builtin__.pyi"},
15+
{"stdlib/2and3/threading.pyi", "stdlib/2and3/_dummy_threading.pyi"},
1616
]
1717

1818

1919
def main():
20-
files = [os.path.join(root, file) for root, dir, files in os.walk('.') for file in files]
21-
no_symlink = 'You cannot use symlinks in typeshed, please copy {} to its link.'
20+
files = [os.path.join(root, file) for root, dir, files in os.walk(".") for file in files]
21+
no_symlink = "You cannot use symlinks in typeshed, please copy {} to its link."
2222
for file in files:
2323
_, ext = os.path.splitext(file)
24-
if ext == '.pyi' and os.path.islink(file):
24+
if ext == ".pyi" and os.path.islink(file):
2525
raise ValueError(no_symlink.format(file))
2626
for file1, *others in consistent_files:
2727
f1 = os.path.join(os.getcwd(), file1)
@@ -34,5 +34,5 @@ def main():
3434
)
3535

3636

37-
if __name__ == '__main__':
37+
if __name__ == "__main__":
3838
main()

tests/mypy_selftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python3
22
"""Script to run mypy against its own code base."""
33

4-
from pathlib import Path
54
import subprocess
65
import sys
76
import tempfile
7+
from pathlib import Path
88

99
MYPY_VERSION = "0.790"
1010

tests/mypy_test.py

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@
1212
5. Repeat steps 2-4 for other mypy runs (e.g. --py2)
1313
"""
1414

15+
import argparse
1516
import os
1617
import re
1718
import sys
18-
import argparse
1919

20-
parser = argparse.ArgumentParser(description="Test runner for typeshed. "
21-
"Patterns are unanchored regexps on the full path.")
22-
parser.add_argument('-v', '--verbose', action='count', default=0, help="More output")
23-
parser.add_argument('-n', '--dry-run', action='store_true', help="Don't actually run mypy")
24-
parser.add_argument('-x', '--exclude', type=str, nargs='*', help="Exclude pattern")
25-
parser.add_argument('-p', '--python-version', type=str, nargs='*',
26-
help="These versions only (major[.minor])")
27-
parser.add_argument('--platform',
28-
help="Run mypy for a certain OS platform (defaults to sys.platform)")
29-
parser.add_argument('--warn-unused-ignores', action='store_true',
30-
help="Run mypy with --warn-unused-ignores "
31-
"(hint: only get rid of warnings that are "
32-
"unused for all platforms and Python versions)")
20+
parser = argparse.ArgumentParser(description="Test runner for typeshed. " "Patterns are unanchored regexps on the full path.")
21+
parser.add_argument("-v", "--verbose", action="count", default=0, help="More output")
22+
parser.add_argument("-n", "--dry-run", action="store_true", help="Don't actually run mypy")
23+
parser.add_argument("-x", "--exclude", type=str, nargs="*", help="Exclude pattern")
24+
parser.add_argument("-p", "--python-version", type=str, nargs="*", help="These versions only (major[.minor])")
25+
parser.add_argument("--platform", help="Run mypy for a certain OS platform (defaults to sys.platform)")
26+
parser.add_argument(
27+
"--warn-unused-ignores",
28+
action="store_true",
29+
help="Run mypy with --warn-unused-ignores "
30+
"(hint: only get rid of warnings that are "
31+
"unused for all platforms and Python versions)",
32+
)
3333

34-
parser.add_argument('filter', type=str, nargs='*', help="Include pattern (default all)")
34+
parser.add_argument("filter", type=str, nargs="*", help="Include pattern (default all)")
3535

3636

3737
def log(args, *varargs):
@@ -41,36 +41,35 @@ def log(args, *varargs):
4141

4242
def match(fn, args, exclude_list):
4343
if exclude_list.match(fn):
44-
log(args, fn, 'exluded by exclude list')
44+
log(args, fn, "exluded by exclude list")
4545
return False
4646
if not args.filter and not args.exclude:
47-
log(args, fn, 'accept by default')
47+
log(args, fn, "accept by default")
4848
return True
4949
if args.exclude:
5050
for f in args.exclude:
5151
if re.search(f, fn):
52-
log(args, fn, 'excluded by pattern', f)
52+
log(args, fn, "excluded by pattern", f)
5353
return False
5454
if args.filter:
5555
for f in args.filter:
5656
if re.search(f, fn):
57-
log(args, fn, 'accepted by pattern', f)
57+
log(args, fn, "accepted by pattern", f)
5858
return True
5959
if args.filter:
60-
log(args, fn, 'rejected (no pattern matches)')
60+
log(args, fn, "rejected (no pattern matches)")
6161
return False
62-
log(args, fn, 'accepted (no exclude pattern matches)')
62+
log(args, fn, "accepted (no exclude pattern matches)")
6363
return True
6464

6565

6666
def libpath(major, minor):
67-
versions = ['%d.%d' % (major, minor)
68-
for minor in reversed(range(minor + 1))]
67+
versions = ["%d.%d" % (major, minor) for minor in reversed(range(minor + 1))]
6968
versions.append(str(major))
70-
versions.append('2and3')
69+
versions.append("2and3")
7170
paths = []
7271
for v in versions:
73-
for top in ['stdlib', 'third_party']:
72+
for top in ["stdlib", "third_party"]:
7473
p = os.path.join(top, v)
7574
if os.path.isdir(p):
7675
paths.append(p)
@@ -81,8 +80,7 @@ def main():
8180
args = parser.parse_args()
8281

8382
with open(os.path.join(os.path.dirname(__file__), "mypy_exclude_list.txt")) as f:
84-
exclude_list = re.compile("(%s)$" % "|".join(
85-
re.findall(r"^\s*([^\s#]+)\s*(?:#.*)?$", f.read(), flags=re.M)))
83+
exclude_list = re.compile("(%s)$" % "|".join(re.findall(r"^\s*([^\s#]+)\s*(?:#.*)?$", f.read(), flags=re.M)))
8684

8785
try:
8886
from mypy.main import main as mypy_main
@@ -92,8 +90,7 @@ def main():
9290

9391
versions = [(3, 9), (3, 8), (3, 7), (3, 6), (3, 5), (2, 7)]
9492
if args.python_version:
95-
versions = [v for v in versions
96-
if any(('%d.%d' % v).startswith(av) for av in args.python_version)]
93+
versions = [v for v in versions if any(("%d.%d" % v).startswith(av) for av in args.python_version)]
9794
if not versions:
9895
print("--- no versions selected ---")
9996
sys.exit(1)
@@ -103,51 +100,50 @@ def main():
103100
for major, minor in versions:
104101
roots = libpath(major, minor)
105102
files = []
106-
seen = {'__builtin__', 'builtins', 'typing'} # Always ignore these.
103+
seen = {"__builtin__", "builtins", "typing"} # Always ignore these.
107104
for root in roots:
108105
names = os.listdir(root)
109106
for name in names:
110107
full = os.path.join(root, name)
111108
mod, ext = os.path.splitext(name)
112-
if mod in seen or mod.startswith('.'):
109+
if mod in seen or mod.startswith("."):
113110
continue
114-
if ext in ['.pyi', '.py']:
111+
if ext in [".pyi", ".py"]:
115112
if match(full, args, exclude_list):
116113
seen.add(mod)
117114
files.append(full)
118-
elif (os.path.isfile(os.path.join(full, '__init__.pyi')) or
119-
os.path.isfile(os.path.join(full, '__init__.py'))):
115+
elif os.path.isfile(os.path.join(full, "__init__.pyi")) or os.path.isfile(os.path.join(full, "__init__.py")):
120116
for r, ds, fs in os.walk(full):
121117
ds.sort()
122118
fs.sort()
123119
for f in fs:
124120
m, x = os.path.splitext(f)
125-
if x in ['.pyi', '.py']:
121+
if x in [".pyi", ".py"]:
126122
fn = os.path.join(r, f)
127123
if match(fn, args, exclude_list):
128124
seen.add(mod)
129125
files.append(fn)
130126
if files:
131127
runs += 1
132-
flags = ['--python-version', '%d.%d' % (major, minor)]
133-
flags.append('--strict-optional')
134-
flags.append('--no-site-packages')
135-
flags.append('--show-traceback')
136-
flags.append('--no-implicit-optional')
137-
flags.append('--disallow-any-generics')
138-
flags.append('--disallow-subclassing-any')
128+
flags = ["--python-version", "%d.%d" % (major, minor)]
129+
flags.append("--strict-optional")
130+
flags.append("--no-site-packages")
131+
flags.append("--show-traceback")
132+
flags.append("--no-implicit-optional")
133+
flags.append("--disallow-any-generics")
134+
flags.append("--disallow-subclassing-any")
139135
if args.warn_unused_ignores:
140-
flags.append('--warn-unused-ignores')
136+
flags.append("--warn-unused-ignores")
141137
if args.platform:
142-
flags.extend(['--platform', args.platform])
143-
sys.argv = ['mypy'] + flags + files
138+
flags.extend(["--platform", args.platform])
139+
sys.argv = ["mypy"] + flags + files
144140
if args.verbose:
145-
print("running", ' '.join(sys.argv))
141+
print("running", " ".join(sys.argv))
146142
else:
147-
print("running mypy", ' '.join(flags), "# with", len(files), "files")
143+
print("running mypy", " ".join(flags), "# with", len(files), "files")
148144
try:
149145
if not args.dry_run:
150-
mypy_main('', sys.stdout, sys.stderr)
146+
mypy_main("", sys.stdout, sys.stderr)
151147
except SystemExit as err:
152148
code = max(code, err.code)
153149
if code:
@@ -158,5 +154,5 @@ def main():
158154
sys.exit(1)
159155

160156

161-
if __name__ == '__main__':
157+
if __name__ == "__main__":
162158
main()

0 commit comments

Comments
 (0)