Skip to content

Commit

Permalink
Add a pre commit config to help format before pushing (#4258)
Browse files Browse the repository at this point in the history
* pre commit config

* yapf version

* fix

* mypy check all files

* skip smoke_test.py

* add doc

* better format

* newline format

* sync with format.sh

* comment fix
  • Loading branch information
zpoint authored Nov 27, 2024
1 parent 36e4aa3 commit c3223a4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
80 changes: 80 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Ensure this configuration aligns with format.sh and requirements.txt
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 22.10.0 # Match the version from requirements
hooks:
- id: black
name: black (IBM specific)
files: "^sky/skylet/providers/ibm/.*" # Match only files in the IBM directory

- repo: https://github.com/pycqa/isort
rev: 5.12.0 # Match the version from requirements
hooks:
# First isort command
- id: isort
name: isort (general)
args:
- "--sg=build/**" # Matches "${ISORT_YAPF_EXCLUDES[@]}"
- "--sg=sky/skylet/providers/ibm/**"
files: "^(sky|tests|examples|llm|docs)/.*" # Only match these directories
# Second isort command
- id: isort
name: isort (IBM specific)
args:
- "--profile=black"
- "-l=88"
- "-m=3"
files: "^sky/skylet/providers/ibm/.*" # Only match IBM-specific directory

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991 # Match the version from requirements
hooks:
- id: mypy
args:
# From tests/mypy_files.txt
- "sky"
- "--exclude"
- "sky/benchmark|sky/callbacks|sky/skylet/providers/azure|sky/resources.py|sky/backends/monkey_patches"
pass_filenames: false
additional_dependencies:
- types-PyYAML
- types-requests<2.31 # Match the condition in requirements.txt
- types-setuptools
- types-cachetools
- types-pyvmomi

- repo: https://github.com/google/yapf
rev: v0.32.0 # Match the version from requirements
hooks:
- id: yapf
name: yapf
exclude: (build/.*|sky/skylet/providers/ibm/.*) # Matches exclusions from the script
args: ['--recursive', '--parallel'] # Only necessary flags
additional_dependencies: [toml==0.10.2]

- repo: https://github.com/pylint-dev/pylint
rev: v2.14.5 # Match the version from requirements
hooks:
- id: pylint
additional_dependencies:
- pylint-quotes==0.2.3 # Match the version from requirements
name: pylint-only-changed-files
entry: >
bash -c '
MERGEBASE=$(git merge-base origin/main HEAD);
changed_files=$(git diff --name-only --diff-filter=ACM "$MERGEBASE" -- "sky/*.py" "sky/*.pyi");
if [[ -n "$changed_files" ]]; then
echo "$changed_files" | tr "\n" "\0" | xargs -0 pylint --load-plugins=pylint_quotes --rcfile=.pylintrc;
else
echo "Pylint skipped: no files changed in sky/.";
fi'
pass_filenames: false
always_run: true
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ It has some convenience features which you might find helpful (see [Dockerfile](
- If relevant, add tests for your changes. For changes that touch the core system, run the [smoke tests](#testing) and ensure they pass.
- Follow the [Google style guide](https://google.github.io/styleguide/pyguide.html).
- Ensure code is properly formatted by running [`format.sh`](https://github.com/skypilot-org/skypilot/blob/master/format.sh).
- [Optional] You can also install pre-commit hooks by running `pre-commit install` to automatically format your code on commit.
- Push your changes to your fork and open a pull request in the SkyPilot repository.
- In the PR description, write a `Tested:` section to describe relevant tests performed.

Expand Down

0 comments on commit c3223a4

Please sign in to comment.