add pre-commit config (#6341)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Akuli <akuviljanen17@gmail.com>
This commit is contained in:
Jelle Zijlstra
2021-11-19 06:46:51 -08:00
committed by GitHub
parent 1fcb613338
commit 1f1bc6f27c
9 changed files with 52 additions and 68 deletions

View File

@@ -44,7 +44,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: pip install $(grep black requirements-tests-py3.txt)
- run: black --check --diff stdlib stubs
- run: black --check --diff scripts stdlib stubs tests
isort:
name: Check imports with isort
@@ -53,7 +53,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- run: pip install $(grep isort requirements-tests-py3.txt)
- run: isort --check-only --diff stdlib stubs
- run: isort --check-only --diff scripts stdlib stubs tests
pytype:
name: Run pytype against the stubs

19
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,19 @@
repos:
- repo: https://github.com/psf/black
rev: 21.11b1 # must match requirements-tests-py3.txt
hooks:
- id: black
language_version: python3.9
- repo: https://github.com/pycqa/isort
rev: 5.10.1 # must match requirements-tests-py3.txt
hooks:
- id: isort
name: isort (python)
ci:
autofix_commit_msg: '[pre-commit.ci] auto fixes from pre-commit.com hooks'
autofix_prs: true
autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate'
autoupdate_schedule: weekly
skip: []
submodules: false

View File

@@ -1,7 +1,7 @@
[tool.black]
line_length = 130
target_version = ["py37"]
exclude = ".*_pb2.pyi"
force-exclude = ".*_pb2.pyi"
skip_magic_trailing_comma = true
[tool.isort]

View File

@@ -1,8 +1,10 @@
mypy==0.910
pytype==2021.11.2; platform_system != "Windows"
black==21.9b0
# must match .pre-commit-config.yaml
black==21.11b1
flake8==4.0.1
flake8-bugbear==21.9.2
flake8-pyi==20.10.0
isort==5.9.3
# must match .pre-commit-config.yaml
isort==5.10.1
tomli==1.1.0

View File

@@ -34,9 +34,7 @@ def check_new_syntax(tree: ast.AST, path: Path) -> list[str]:
errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`")
# Tuple[Foo, ...] must be allowed because of mypy bugs
if node.value.id == "Tuple" and not (
isinstance(node.slice, ast.Tuple)
and len(node.slice.elts) == 2
and is_dotdotdot(node.slice.elts[1])
isinstance(node.slice, ast.Tuple) and len(node.slice.elts) == 2 and is_dotdotdot(node.slice.elts[1])
):
new_syntax = f"tuple[{unparse_without_tuple_parens(node.slice)}]"
errors.append(f"{path}:{node.lineno}: Use built-in generics, e.g. `{new_syntax}`")

View File

@@ -198,18 +198,18 @@ def run_mypy(args, configurations, major, minor, files, *, custom_typeshed=False
def get_mypy_flags(args, major: int, minor: int, temp_name: str, *, custom_typeshed: bool) -> list[str]:
flags = [
"--python-version",
"%d.%d" % (major, minor),
"--config-file",
temp_name,
"--no-site-packages",
"--show-traceback",
"--no-implicit-optional",
"--disallow-any-generics",
"--warn-incomplete-stub",
"--show-error-codes",
"--no-error-summary",
]
"--python-version",
"%d.%d" % (major, minor),
"--config-file",
temp_name,
"--no-site-packages",
"--show-traceback",
"--no-implicit-optional",
"--disallow-any-generics",
"--warn-incomplete-stub",
"--show-error-codes",
"--no-error-summary",
]
if custom_typeshed:
# Setting custom typeshed dir prevents mypy from falling back to its bundled
# typeshed in case of stub deletions
@@ -235,12 +235,7 @@ def read_dependencies(distribution: str) -> list[str]:
def add_third_party_files(
distribution: str,
major: int,
files: list[str],
args,
configurations: list[MypyDistConf],
seen_dists: set[str],
distribution: str, major: int, files: list[str], args, configurations: list[MypyDistConf], seen_dists: set[str]
) -> None:
if distribution in seen_dists:
return
@@ -264,9 +259,7 @@ def add_third_party_files(
add_configuration(configurations, distribution)
def test_third_party_distribution(
distribution: str, major: int, minor: int, args
) -> tuple[int, int]:
def test_third_party_distribution(distribution: str, major: int, minor: int, args) -> tuple[int, int]:
"""Test the stubs of a third-party distribution.
Return a tuple, where the first element indicates mypy's return code

View File

@@ -38,10 +38,7 @@ def main() -> None:
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,
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:
del os.environ[TYPESHED_HOME]
@@ -59,11 +56,7 @@ def create_parser() -> argparse.ArgumentParser:
"--print-stderr", action="store_true", default=False, help="Print stderr every time an error is encountered."
)
parser.add_argument(
"files",
metavar="FILE",
type=str,
nargs="*",
help="Files or directories to check. (Default: Check all files.)",
"files", metavar="FILE", type=str, nargs="*", help="Files or directories to check. (Default: Check all files.)"
)
return parser
@@ -71,8 +64,7 @@ def create_parser() -> argparse.ArgumentParser:
def run_pytype(*, filename: str, python_version: str, typeshed_location: str) -> Optional[str]:
"""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)
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]
@@ -103,7 +95,7 @@ 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:]
module_parts = parts[parts.index("@python2") + 1 :]
elif parts[0] == "stdlib":
module_parts = parts[1:]
else:
@@ -157,13 +149,7 @@ def run_all_tests(*, files_to_test: Sequence[str], typeshed_location: str, print
for i, f in enumerate(files_to_test):
python_version = "{0.major}.{0.minor}".format(sys.version_info)
stderr = (
run_pytype(
filename=f,
python_version=python_version,
typeshed_location=typeshed_location,
)
if not dry_run
else None
run_pytype(filename=f, python_version=python_version, typeshed_location=typeshed_location) if not dry_run else None
)
if stderr:
if print_stderr:

View File

@@ -39,15 +39,9 @@ def run_stubtest(typeshed_dir: Path) -> int:
if ignore_unused_allowlist:
cmd += ["--ignore-unused-allowlist"]
if (allowlist_dir / platform_allowlist).exists():
cmd += [
"--allowlist",
str(allowlist_dir / platform_allowlist),
]
cmd += ["--allowlist", str(allowlist_dir / platform_allowlist)]
if (allowlist_dir / combined_allowlist).exists():
cmd += [
"--allowlist",
str(allowlist_dir / combined_allowlist),
]
cmd += ["--allowlist", str(allowlist_dir / combined_allowlist)]
if sys.version_info < (3, 10):
# As discussed in https://github.com/python/typeshed/issues/3693, we only aim for
# positional-only arg accuracy for the latest Python version.

View File

@@ -6,11 +6,11 @@ import functools
import subprocess
import sys
import tempfile
import tomli
import venv
from glob import glob
from pathlib import Path
import tomli
EXCLUDE_LIST = [
"Flask", # fails when stubtest tries to stringify some object
@@ -18,7 +18,7 @@ EXCLUDE_LIST = [
"backports", # errors on python version
"six", # ???
"aiofiles", # easily fixable, some platform specific difference between local and ci
"pycurl" # install failure, missing libcurl
"pycurl", # install failure, missing libcurl
]
@@ -105,17 +105,9 @@ def run_stubtest(dist: Path) -> None:
print(f"stubtest failed for {dist.name}", file=sys.stderr)
print("\n\n", file=sys.stderr)
if allowlist_path.exists():
print(
'To fix "unused allowlist" errors, remove the corresponding entries from '
f"{allowlist_path}",
file=sys.stderr,
)
print('To fix "unused allowlist" errors, remove the corresponding entries from {allowlist_path}', file=sys.stderr)
else:
print(
"Re-running stubtest with --generate-allowlist.\n"
f"Add the following to {allowlist_path}:",
file=sys.stderr,
)
print("Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path}:", file=sys.stderr)
subprocess.run(cmd + ["--generate-allowlist"], env={"MYPYPATH": str(dist)})
print("\n\n", file=sys.stderr)
raise StubtestFailed from None