Skip to content

Commit

Permalink
build: use pre-commit + ruff
Browse files Browse the repository at this point in the history
This removes the need for black, autopep8, and isort

Also includes the configuration for pre-commit ci, if
it is decided that it should be added.
  • Loading branch information
JasonGrace2282 committed Jun 18, 2024
1 parent 1d1ff53 commit 7f643b8
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 85 deletions.
63 changes: 5 additions & 58 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,67 +47,15 @@
},
{
"name": "Set up packages",
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n flake8 pylint pylint-django pylint-plugin-utils isort black autopep8 \\\n coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n pre-commit coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
},
{
"name": "Copy secret.py",
"run": "cp intranet/settings/ci_secret.py intranet/settings/secret.py"
},
{
"name": "Run flake8",
"run": "flake8 --max-line-length 150 --exclude=*/migrations/* intranet/ scripts/ docs/ *.py"
},
{
"name": "Run pylintx",
"run": "pylint --jobs=0 --disable=fixme,broad-exception-caught,broad-exception-raised,unsupported-binary-operation,global-statement,attribute-defined-outside-init,cyclic-import,consider-using-f-string --django-settings-module=intranet.settings intranet/"
}
]
},
"formatting": {
"runs-on": "ubuntu-latest",
"strategy": {
"matrix": {
"python-version": [
3.8
]
},
"fail-fast": false
},
"steps": [
{
"name": "Set up repo",
"uses": "actions/checkout@v2"
},
{
"name": "Set up Python ${{ matrix.python-version }}",
"uses": "actions/setup-python@v2",
"with": {
"python-version": "${{ matrix.python-version }}"
}
},
{
"name": "Set up pip cache",
"uses": "actions/cache@v2",
"with": {
"path": "~/.cache/pip",
"key": "pip-${{ matrix.python-version }}"
}
},
{
"name": "Set up packages",
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n flake8 pylint pylint-django pylint-plugin-utils isort black autopep8 \\\n coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
},
{
"name": "Copy secret.py",
"run": "cp intranet/settings/ci_secret.py intranet/settings/secret.py"
},
{
"name": "Format code",
"run": "./scripts/build_ensure_no_changes.sh ./scripts/format.sh"
},
{
"name": "Format static files and templates",
"run": "./scripts/build_ensure_no_changes.sh ./scripts/static_templates_format.sh"
"name": "Run pre-commit",
"run": "pre-commit run --all-files"
}
]
},
Expand Down Expand Up @@ -146,7 +94,7 @@
},
{
"name": "Set up packages",
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n flake8 pylint pylint-django pylint-plugin-utils isort black autopep8 \\\n coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n pre-commit coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
},
{
"name": "Copy secret.py",
Expand Down Expand Up @@ -251,7 +199,7 @@
},
{
"name": "Set up packages",
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n flake8 pylint pylint-django pylint-plugin-utils isort black autopep8 \\\n coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
"run": "set -e\n\npip install -U pip setuptools wheel\npip install -U \\\n pre-commit coveralls pyyaml pytest-django\npip install -U -r requirements.txt\n\necho \"PATH=$PATH\" >> $GITHUB_ENV\n"
},
{
"name": "Copy secret.py",
Expand Down Expand Up @@ -328,7 +276,6 @@
"finish_success": {
"needs": [
"linting",
"formatting",
"build",
"tests"
],
Expand Down
43 changes: 43 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
default_stages: [commit, push]
fail_fast: false
exclude: ^(intranet/middleware|intranet/apps/.*/migrations|intranet/static/.*vendor/.*|intranet/.*/stubs|Ion.egg-info|\.github)

# This is for pre-commit ci
# After adding pre-commit ci, the "linting" stage of the tests
# can be safely deleted
ci:
autofix_commit_msg: |
chore: auto fixes from pre-commit hooks
for more information, see https://pre-commit.ci
autoupdate_commit_msg: 'build: pre-commit autoupdate'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-ast
name: Validate Python
- id: trailing-whitespace
- id: mixed-line-ending
args: ["--fix=lf"]
- id: check-toml
- id: check-yaml
- id: detect-private-key
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
hooks:
- id: ruff
args: [ "--fix", "--exit-non-zero-on-fix" ]
name: ruff lint
files: ^intranet/apps/.*
- id: ruff-format
files: ^intranet/apps/.*
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
hooks:
- id: codespell
files: ^.*\.(py|md|rst)$
# TODO: Remove after python version >= 3.11
additional_dependencies:
- tomli
1 change: 1 addition & 0 deletions Ion.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
.gitattributes
.gitignore
.isort.cfg
.pre-commit-config.yaml
.pylintrc
.style.yapf
CONTRIBUTING.md
Expand Down
30 changes: 3 additions & 27 deletions ci/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ env:
pip install -U pip setuptools wheel
pip install -U \
flake8 pylint pylint-django pylint-plugin-utils isort black autopep8 \
coveralls pyyaml pytest-django
pre-commit coveralls pyyaml pytest-django
pip install -U -r requirements.txt
echo "PATH=$PATH" >> $GITHUB_ENV
Expand All @@ -83,30 +82,8 @@ jobs:
- *setup_packages
- *copy_secret

- name: Run flake8
run: 'flake8 --max-line-length 150 --exclude=*/migrations/* intranet/ scripts/ docs/ *.py'
- name: Run pylintx
run: pylint --jobs=0 --disable=fixme,broad-exception-caught,broad-exception-raised,unsupported-binary-operation,global-statement,attribute-defined-outside-init,cyclic-import,consider-using-f-string --django-settings-module=intranet.settings intranet/

formatting:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: *python_versions
fail-fast: false

steps:
- *repo_setup
- *python_setup
- *setup_pip_cache
- *setup_packages
- *copy_secret

- name: Format code
run: ./scripts/build_ensure_no_changes.sh ./scripts/format.sh
- name: Format static files and templates
run: ./scripts/build_ensure_no_changes.sh ./scripts/static_templates_format.sh
- name: Run pre-commit
run: 'pre-commit run --all-files'

build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -256,7 +233,6 @@ jobs:
finish_success:
needs:
- linting
- formatting
- build
- tests

Expand Down
90 changes: 90 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,93 @@ exclude = '''
| stubs
)/
'''

[tool.ruff]
extend-exclude = [
"Ion.egg-info",
"build",
".env",
"env",
"migrations",
]

# show fixes made in stdout
# show-fixes = true

line-length = 150

target-version = "py38"

[tool.ruff.lint]
select = [
# flake8-bugbear
"B",
# flake8-comprehensions
"C4",
# flake8-django
"DJ",
# pycodestyle
"E",
# Pyflakes
"F",
# isort
"I",
# flake8-no-pep420
"INP",
# Pylint
"PL",
# ruff
"RUF",
# pyupgrade
"UP",
]
ignore = [
# default arguments for timezone.now()
"B008",
# null=True on CharField/TextField
"DJ001",
# No __str__ method on Model
"DJ008",
# Django order of model methods
"DJ012",
# ambiguous variable name
"E741",
# as recommended by https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"E111",
"E114",
"E117",
"E501",
# branching
"PLR09",
# avoid magic numbers
"PLR2004",
# loop variables overwritten by assignment
"PLW2901",
# Use ternary operator (x if cond else y)
"RUF005",
# mutable class attrs annotated as typing.ClassVar
"RUF012",
# implicit Optional
"RUF013",
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = [
"F401",
"F403",
]

[tool.ruff.lint.flake8-pytest-style]
fixture-parentheses = false
mark-parentheses = false
parametrize-names-type = "tuple"
parametrize-values-type = "tuple"

[tool.ruff.format]
docstring-code-format = true
line-ending = "lf"

[tool.codespell]
write-changes = true
skip = "*.pyi,*/vendor,*/migrations,*media"
ignore-words-list = "num,ans,intranet"
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ pylint-django==2.5.5
pylint-plugin-utils==0.8.2
names==0.3.0
whitenoise==6.6.0
pre-commit==3.5.0

0 comments on commit 7f643b8

Please sign in to comment.