Skip to content

Commit

Permalink
[CI][Linter] Migrate to ruff
Browse files Browse the repository at this point in the history
Closes: ray-project#48508
Signed-off-by: Chi-Sheng Liu <[email protected]>
  • Loading branch information
MortalHappiness committed Dec 25, 2024
1 parent 14e747f commit 78ab98b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 137 deletions.
53 changes: 0 additions & 53 deletions .flake8

This file was deleted.

25 changes: 7 additions & 18 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ repos:
)
- id: check-toml

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.4
hooks:
- id: ruff
args: [ --fix, --exit-non-zero-on-fix ]
# - id: ruff-format

- repo: https://github.com/psf/black
rev: 22.10.0
hooks:
Expand All @@ -46,17 +53,6 @@ repos:
)
types_or: [python]

- repo: https://github.com/pycqa/flake8
rev: 3.9.1
hooks:
- id: flake8
additional_dependencies:
[
flake8-comprehensions==3.10.1,
flake8-quotes==2.0.0,
flake8-bugbear==21.9.2,
]

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
hooks:
Expand All @@ -83,13 +79,6 @@ repos:
types-PyYAML==6.0.12.2,
]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
types_or: [python]

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
Expand Down
59 changes: 0 additions & 59 deletions ci/lint/format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
# Cause the script to exit if a single command fails
set -euo pipefail

FLAKE8_VERSION_REQUIRED="3.9.1"
BLACK_VERSION_REQUIRED="22.10.0"
SHELLCHECK_VERSION_REQUIRED="0.7.1"
MYPY_VERSION_REQUIRED="1.7.0"
ISORT_VERSION_REQUIRED="5.10.1"

check_python_command_exist() {
VERSION=""
case "$1" in
black)
VERSION=$BLACK_VERSION_REQUIRED
;;
flake8)
VERSION=$FLAKE8_VERSION_REQUIRED
;;
mypy)
VERSION=$MYPY_VERSION_REQUIRED
;;
isort)
VERSION=$ISORT_VERSION_REQUIRED
;;
*)
echo "$1 is not a required dependency"
exit 1
Expand Down Expand Up @@ -53,9 +45,7 @@ check_docstyle() {

# TODO(can): add shellcheck, clang-format, and google-java-format to this check
check_python_command_exist black
check_python_command_exist flake8
check_python_command_exist mypy
check_python_command_exist isort

# this stops git rev-parse from failing if we run this from the .git directory
builtin cd "$(dirname "${BASH_SOURCE:-$0}")"
Expand All @@ -77,9 +67,7 @@ then
else
BLACK_VERSION=$(echo "$BLACK_VERSION_STR" | head -n 1 | awk '{print $3}')
fi
FLAKE8_VERSION=$(flake8 --version | head -n 1 | awk '{print $1}')
MYPY_VERSION=$(mypy --version | awk '{print $2}')
ISORT_VERSION=$(isort --version | grep VERSION | awk '{print $2}')
GOOGLE_JAVA_FORMAT_JAR=/tmp/google-java-format-1.7-all-deps.jar

# params: tool name, tool version, required version
Expand All @@ -89,10 +77,8 @@ tool_version_check() {
fi
}

tool_version_check "flake8" "$FLAKE8_VERSION" "$FLAKE8_VERSION_REQUIRED"
tool_version_check "black" "$BLACK_VERSION" "$BLACK_VERSION_REQUIRED"
tool_version_check "mypy" "$MYPY_VERSION" "$MYPY_VERSION_REQUIRED"
tool_version_check "isort" "$ISORT_VERSION" "$ISORT_VERSION_REQUIRED"

if command -v shellcheck >/dev/null; then
SHELLCHECK_VERSION=$(shellcheck --version | awk '/^version:/ {print $2}')
Expand All @@ -117,14 +103,6 @@ else
echo "WARNING:java is not installed, skip format java files!"
fi

if [[ $(flake8 --version) != *"flake8_quotes"* ]]; then
echo "WARNING: Ray uses flake8 with flake8_quotes. Might error without it. Install with: pip install flake8-quotes"
fi

if [[ $(flake8 --version) != *"flake8-bugbear"* ]]; then
echo "WARNING: Ray uses flake8 with flake8-bugbear. Might error without it. Install with: pip install flake8-bugbear"
fi

SHELLCHECK_FLAGS=(
"--exclude=1090" # "Can't follow non-constant source. Use a directive to specify location."
"--exclude=1091" # "Not following {file} due to some error"
Expand Down Expand Up @@ -180,11 +158,6 @@ for f in "${JAVA_EXCLUDES[@]}"; do
done
JAVA_EXCLUDES_REGEX=${JAVA_EXCLUDES_REGEX#|}

# TODO(barakmich): This should be cleaned up. I've at least excised the copies
# of these arguments to this location, but the long-term answer is to actually
# make a flake8 config file
FLAKE8_PYX_IGNORES="--ignore=C408,E121,E123,E126,E211,E225,E226,E227,E24,E704,E999,W503,W504,W605"

shellcheck_scripts() {
shellcheck "${SHELLCHECK_FLAGS[@]}" "$@"
}
Expand Down Expand Up @@ -247,7 +220,6 @@ format_files() {
done

if [ 0 -lt "${#python_files[@]}" ]; then
isort "${python_files[@]}"
black "${python_files[@]}"
fi

Expand All @@ -266,26 +238,11 @@ format_files() {
}

format_all_scripts() {
command -v flake8 &> /dev/null;
HAS_FLAKE8=$?

# Run isort before black to fix imports and let black deal with file format.
echo "$(date)" "isort...."
git ls-files -- '*.py' "${GIT_LS_EXCLUDES[@]}" | xargs -P 10 \
isort
echo "$(date)" "Black...."
git ls-files -- '*.py' "${GIT_LS_EXCLUDES[@]}" | xargs -P 10 \
black "${BLACK_EXCLUDES[@]}"
echo "$(date)" "MYPY...."
mypy_on_each "${MYPY_FILES[@]}"
if [ $HAS_FLAKE8 ]; then
echo "$(date)" "Flake8...."
git ls-files -- '*.py' "${GIT_LS_EXCLUDES[@]}" | xargs -P 5 \
flake8 --config=.flake8

git ls-files -- '*.pyx' '*.pxd' '*.pxi' "${GIT_LS_EXCLUDES[@]}" | xargs -P 5 \
flake8 --config=.flake8 "$FLAKE8_PYX_IGNORES"
fi

if command -v shellcheck >/dev/null; then
local shell_files bin_like_files
Expand Down Expand Up @@ -330,25 +287,9 @@ format_changed() {
# exist on both branches.
MERGEBASE="$(git merge-base upstream/master HEAD)"

if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.py' &>/dev/null; then
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
isort
fi

if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.py' &>/dev/null; then
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
black "${BLACK_EXCLUDES[@]}"
if command -v flake8 >/dev/null; then
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.py' | xargs -P 5 \
flake8 --config=.flake8
fi
fi

if ! git diff --diff-filter=ACRM --quiet --exit-code "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' &>/dev/null; then
if command -v flake8 >/dev/null; then
git diff --name-only --diff-filter=ACRM "$MERGEBASE" -- '*.pyx' '*.pxd' '*.pxi' | xargs -P 5 \
flake8 --config=.flake8 "$FLAKE8_PYX_IGNORES"
fi
fi

if command -v clang-format >/dev/null; then
Expand Down
40 changes: 40 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[project]
requires-python = ">=3.9"

[tool.ruff.lint]
extend-select = ["I", "B", "Q", "C4" ]
ignore = [
"E402",
"E711",
"E721",
"E741",
"F401",
"F403",
"F405",
"F602",
"F811",
"F821",
"F841",
"B003",
"B005",
"B007",
"B008",
"B011",
"B012",
"B015",
"B017",
"B018",
"B023",
"B024",
"B026",
"B027",
"B028",
"B035",
"B039",
"B904",
"C408",
"C416",
"C417",
"C418",
"C419",
]
2 changes: 0 additions & 2 deletions python/ray/tune/requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
flake8==3.9.1
flake8-quotes
gym>=0.21.0,<0.24.0
scikit-image
pandas
Expand Down
6 changes: 1 addition & 5 deletions python/requirements/lint-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
clang-format==12.0.1
docutils
flake8==3.9.1
flake8-comprehensions==3.10.1
flake8-quotes==2.0.0
flake8-bugbear==21.9.2
ruff==0.8.4
mypy==1.7.0
mypy-extensions==1.0.0
types-PyYAML==6.0.12.2
black==22.10.0
isort==5.10.1
semgrep==1.32.0
shellcheck-py==0.7.1.1
yq
Expand Down

0 comments on commit 78ab98b

Please sign in to comment.