Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pre-commit config #6341

Merged
merged 10 commits into from
Nov 19, 2021
Merged
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
6 changes: 4 additions & 2 deletions requirements-tests-py3.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions tests/check_new_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def visit_Subscript(self, node: ast.Subscript) -> None:
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}`")
Expand Down
35 changes: 14 additions & 21 deletions tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 5 additions & 19 deletions tests/pytype_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -59,20 +56,15 @@ 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


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]
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 2 additions & 8 deletions tests/stubtest_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 4 additions & 12 deletions tests/stubtest_third_party.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
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
"pyaudio", # install failure locally
"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
]


Expand Down Expand Up @@ -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
Expand Down