Rework our linting setup (#11522)

Co-authored-by: Avasam <samuel.06@hotmail.com>
This commit is contained in:
Alex Waygood
2024-03-03 23:11:54 +00:00
committed by GitHub
parent 1c40e64611
commit 35b74bc431
9 changed files with 75 additions and 87 deletions

View File

@@ -45,10 +45,7 @@ PYTHON_PROTOBUF_DIR="protobuf-$PYTHON_PROTOBUF_VERSION"
VENV=venv
python3 -m venv "$VENV"
source "$VENV/bin/activate"
pip install -r "$REPO_ROOT/requirements-tests.txt" # for Black and Ruff
# Install mypy-protobuf
pip install mypy-protobuf=="$MYPY_PROTOBUF_VERSION"
pip install pre-commit mypy-protobuf=="$MYPY_PROTOBUF_VERSION"
# Remove existing pyi
find "$REPO_ROOT/stubs/protobuf/" -name '*_pb2.pyi' -delete
@@ -73,9 +70,9 @@ PROTO_FILES=$(grep "GenProto.*google" $PYTHON_PROTOBUF_DIR/python/setup.py | \
# shellcheck disable=SC2086
protoc_install/bin/protoc --proto_path="$PYTHON_PROTOBUF_DIR/src" --mypy_out="relax_strict_optional_primitives:$REPO_ROOT/stubs/protobuf" $PROTO_FILES
ruff check "$REPO_ROOT/stubs/protobuf" --fix-only
ruff check "$REPO_ROOT/stubs/protobuf" --fix-only --select=UP036 --unsafe-fixes
black "$REPO_ROOT/stubs/protobuf"
# use `|| true` so the script still continues even if a pre-commit hook
# applies autofixes (which will result in a nonzero exit code)
pre-commit run --files "$REPO_ROOT/stubs/protobuf" || true
sed --in-place="" \
"s/extra_description = .*$/extra_description = \"Generated using [mypy-protobuf==$MYPY_PROTOBUF_VERSION](https:\/\/github.com\/nipunn1313\/mypy-protobuf\/tree\/v$MYPY_PROTOBUF_VERSION) on protobuf==$PYTHON_PROTOBUF_VERSION\"/" \

View File

@@ -81,17 +81,8 @@ def main() -> None:
stubtest_result: subprocess.CompletedProcess[bytes] | None = None
pytype_result: subprocess.CompletedProcess[bytes] | None = None
# Run formatters first. Order matters.
print("\nRunning Ruff...")
subprocess.run([sys.executable, "-m", "ruff", "check", path])
print("\nRunning Black...")
black_result = subprocess.run([sys.executable, "-m", "black", path])
if black_result.returncode == 123:
print("Could not run tests due to an internal error with Black. See above for details.", file=sys.stderr)
sys.exit(black_result.returncode)
print("\nRunning Flake8...")
flake8_result = subprocess.run([sys.executable, "-m", "flake8", path])
print("\nRunning pre-commit...")
pre_commit_result = subprocess.run(["pre-commit", "run", "--all-files"])
print("\nRunning check_consistent.py...")
check_consistent_result = subprocess.run([sys.executable, "tests/check_consistent.py"])
@@ -187,7 +178,7 @@ def main() -> None:
any_failure = any(
[
flake8_result.returncode,
pre_commit_result.returncode,
check_consistent_result.returncode,
check_new_syntax_result.returncode,
pyright_returncode,
@@ -203,7 +194,18 @@ def main() -> None:
print(colored("\n\n--- TEST SUMMARY: One or more tests failed. See above for details. ---\n", "red"))
else:
print(colored("\n\n--- TEST SUMMARY: All tests passed! ---\n", "green"))
print("Flake8:", _SUCCESS if flake8_result.returncode == 0 else _FAILED)
if pre_commit_result.returncode == 0:
print("pre-commit", _SUCCESS)
else:
print("pre-commit", _FAILED)
print(
"""\
Check the output of pre-commit for more details.
This could mean that there's a lint failure on your code,
but could also just mean that one of the pre-commit tools
applied some autofixes. If the latter, you may want to check
that the autofixes did sensible things."""
)
print("Check consistent:", _SUCCESS if check_consistent_result.returncode == 0 else _FAILED)
print("Check new syntax:", _SUCCESS if check_new_syntax_result.returncode == 0 else _FAILED)
if pyright_skipped:

View File

@@ -15,7 +15,7 @@ TENSORFLOW_VERSION=2.12.1
# protobuf<4.
MYPY_PROTOBUF_VERSION=3.5.0
pip install mypy-protobuf=="$MYPY_PROTOBUF_VERSION"
pip install pre-commit mypy-protobuf=="$MYPY_PROTOBUF_VERSION"
cd "$(dirname "$0")" > /dev/null
cd ../stubs/tensorflow
@@ -61,9 +61,9 @@ rm tensorflow/compiler/xla/service/hlo_execution_profile_data_pb2.pyi \
tensorflow/core/protobuf/worker_service_pb2.pyi \
tensorflow/core/util/example_proto_fast_parsing_test_pb2.pyi
ruff check "$REPO_ROOT/stubs/tensorflow/tensorflow" --fix-only
ruff check "$REPO_ROOT/stubs/protobuf" --fix-only --select=UP036 --unsafe-fixes
black "$REPO_ROOT/stubs/tensorflow/tensorflow"
# use `|| true` so the script still continues even if a pre-commit hook
# applies autofixes (which will result in a nonzero exit code)
pre-commit run --files "$REPO_ROOT/stubs/tensorflow/tensorflow" || true
sed --in-place="" \
"s/extra_description = .*$/extra_description = \"Partially generated using [mypy-protobuf==$MYPY_PROTOBUF_VERSION](https:\/\/github.com\/nipunn1313\/mypy-protobuf\/tree\/v$MYPY_PROTOBUF_VERSION) on tensorflow==$TENSORFLOW_VERSION\"/" \