From b73010bb366b18b1297b67de25e7e07e2a737534 Mon Sep 17 00:00:00 2001 From: Shamil Date: Mon, 16 Mar 2026 01:22:27 +0300 Subject: [PATCH] feat: add ruff linter & formatter (#22576) * chore: add ruff linter with base config * ci: add ruff pre-commit hook * ci: add ruff github actions workflow * ci: run ruff only on changed files --- .github/workflows/ruff.yml | 49 ++++++++++++++++++++++++++++++++++++++ .pre-commit-config.yaml | 8 +++++++ pyproject.toml | 27 +++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 .github/workflows/ruff.yml create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml new file mode 100644 index 0000000000..bfbb892959 --- /dev/null +++ b/.github/workflows/ruff.yml @@ -0,0 +1,49 @@ +name: Ruff + +on: + push: + branches: + - main + - dev + paths: + - 'backend/**' + - 'pyproject.toml' + pull_request: + branches: + - main + - dev + paths: + - 'backend/**' + - 'pyproject.toml' + +jobs: + ruff: + name: 'Lint & Format Backend' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Get changed Python files + id: changed + run: | + if [ "${{ github.event_name }}" = "pull_request" ]; then + BASE=${{ github.event.pull_request.base.sha }} + else + BASE=${{ github.event.before }} + fi + FILES=$(git diff --name-only --diff-filter=ACMR "$BASE" HEAD -- 'backend/**.py' | xargs) + echo "files=$FILES" >> "$GITHUB_OUTPUT" + + - name: Ruff check + if: steps.changed.outputs.files != '' + uses: astral-sh/ruff-action@v3 + with: + args: check ${{ steps.changed.outputs.files }} + + - name: Ruff format + if: steps.changed.outputs.files != '' + uses: astral-sh/ruff-action@v3 + with: + args: format --check ${{ steps.changed.outputs.files }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..052336f8e7 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,8 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.15.5 + hooks: + - id: ruff + args: [--fix, backend] + - id: ruff-format + args: [backend] diff --git a/pyproject.toml b/pyproject.toml index b60d14439b..64e69032f3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -213,4 +213,31 @@ ignore-words-list = 'ans' [dependency-groups] dev = [ "pytest-asyncio>=1.0.0", + "ruff>=0.15.5", ] + +[tool.ruff] +line-length = 120 + +[tool.ruff.format] +quote-style = "single" +docstring-code-format = false + +[tool.ruff.lint] +select = [ + "E", # pycodestyle errors + "F", # pyflakes + "W", # pycodestyle warnings + "I", # isort + "UP", # pyupgrade + "C90", # mccabe + "Q", # flake8-quotes + "ICN", # flake8-import-conventions +] + +# Plugin configs: +flake8-import-conventions.banned-from = [ "ast", "datetime" ] +flake8-import-conventions.aliases = { datetime = "dt" } +flake8-quotes.inline-quotes = "single" +mccabe.max-complexity = 10 +pydocstyle.convention = "google"