Require isort on all Python files. (#8420)

This commit is contained in:
Jiaming Yuan
2022-11-08 12:59:06 +08:00
committed by GitHub
parent bf8de227a9
commit 0d3da9869c
69 changed files with 290 additions and 187 deletions

View File

@@ -1,7 +1,7 @@
import sys
import re
import zipfile
import glob
import re
import sys
import zipfile
if len(sys.argv) != 2:
print('Usage: {} [wheel]'.format(sys.argv[0]))

View File

@@ -12,16 +12,31 @@ CURDIR = os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.normpath(os.path.join(CURDIR, os.path.pardir, os.path.pardir))
def run_formatter(rel_path: str) -> bool:
path = os.path.join(PROJECT_ROOT, rel_path)
isort_ret = subprocess.run(["isort", "--check", "--profile=black", path]).returncode
black_ret = subprocess.run(["black", "--check", rel_path]).returncode
if isort_ret != 0 or black_ret != 0:
msg = (
"Please run the following command on your machine to address the format"
f" errors:\n isort --profile=black {rel_path}\n black {rel_path}\n"
)
print(msg, file=sys.stdout)
def run_black(rel_path: str) -> bool:
cmd = ["black", "-q", "--check", rel_path]
ret = subprocess.run(cmd).returncode
if ret != 0:
subprocess.run(["black", "--version"])
msg = """
Please run the following command on your machine to address the formatting error:
"""
msg += " ".join(cmd)
print(msg, file=sys.stderr)
return False
return True
def run_isort(rel_path: str) -> bool:
cmd = ["isort", "--check", "--profile=black", rel_path]
ret = subprocess.run(cmd).returncode
if ret != 0:
msg = """
Please run the following command on your machine to address the formatting error:
"""
msg += " ".join(cmd)
print(msg, file=sys.stderr)
return False
return True
@@ -114,8 +129,8 @@ if __name__ == "__main__":
parser.add_argument("--pylint", type=int, choices=[0, 1], default=1)
args = parser.parse_args()
if args.format == 1:
if not all(
run_formatter(path)
black_results = [
run_black(path)
for path in [
# core
"python-package/xgboost/__init__.py",
@@ -141,7 +156,28 @@ if __name__ == "__main__":
"demo/guide-python/categorical.py",
"demo/guide-python/spark_estimator_examples.py",
]
):
]
if not all(black_results):
sys.exit(-1)
isort_results = [
run_isort(path)
for path in [
# core
"python-package/",
# tests
"tests/test_distributed/",
"tests/python/",
"tests/python-gpu/",
"tests/ci_build/",
# demo
"demo/",
# misc
"dev/",
"doc/",
]
]
if not all(black_results):
sys.exit(-1)
if args.type_check == 1:

View File

@@ -1,5 +1,5 @@
import sys
import os
import sys
from contextlib import contextmanager

View File

@@ -1,15 +1,16 @@
#!/usr/bin/env python
import subprocess
import yaml
import json
from multiprocessing import Pool, cpu_count
import shutil
import os
import sys
import re
import argparse
import json
import os
import re
import shutil
import subprocess
import sys
from multiprocessing import Pool, cpu_count
from time import time
import yaml
def call(args):
'''Subprocess run wrapper.'''

View File

@@ -4,13 +4,10 @@ from typing import Any, Dict
import numpy as np
import pytest
from hypothesis import assume, given, note, settings, strategies
from xgboost.testing.params import cat_parameter_strategy, hist_parameter_strategy
import xgboost as xgb
from xgboost import testing as tm
from xgboost.testing.params import (
hist_parameter_strategy,
cat_parameter_strategy,
)
sys.path.append("tests/python")
import test_updaters as test_up

View File

@@ -1,7 +1,7 @@
import os
import subprocess
import tempfile
import sys
import tempfile
import pytest

View File

@@ -5,9 +5,8 @@ import numpy as np
import pytest
import xgboost as xgb
from xgboost import RabitTracker
from xgboost import RabitTracker, collective
from xgboost import testing as tm
from xgboost import collective
if sys.platform.startswith("win"):
pytest.skip("Skipping dask tests on Windows", allow_module_level=True)

View File

@@ -5,14 +5,14 @@ from typing import Any, Dict
import numpy as np
import pytest
from hypothesis import given, note, settings, strategies
from xgboost.testing.params import (
cat_parameter_strategy,
exact_parameter_strategy,
hist_parameter_strategy,
)
import xgboost as xgb
from xgboost import testing as tm
from xgboost.testing.params import (
exact_parameter_strategy,
hist_parameter_strategy,
cat_parameter_strategy,
)
def train_result(param, dmat, num_rounds):

View File

@@ -12,7 +12,7 @@ from itertools import starmap
from math import ceil
from operator import attrgetter, getitem
from pathlib import Path
from typing import Any, Dict, Optional, Tuple, Type, Union, Generator
from typing import Any, Dict, Generator, Optional, Tuple, Type, Union
import hypothesis
import numpy as np