Skip to content

build(format): ruff formatting and import order, checked in CI and pre-commit (#324) - #441

Open
amar-python wants to merge 6 commits into
mainfrom
feat/324-ruff-format
Open

amar-python wants to merge 6 commits into
mainfrom
feat/324-ruff-format

Conversation

@amar-python

Copy link
Copy Markdown
Owner

Refs #324. That issue is closed, but its formatter criterion ("Adopt ruff (lint + format), or black + isort, with a one-off formatting commit recorded in .git-blame-ignore-revs") was never done. .pre-commit-config.yaml on main said so: "No formatter yet". This PR does it.

Please merge with a merge commit, not squash or rebase. .git-blame-ignore-revs names the formatting commit 20cd32e8e8a203e9db27091cd74f861c5509e994. Squash or rebase would give it a different id, and blame would no longer skip it.

What changes

Formatter ruff format: black's style at 120 columns, the same as flake8's max-line-length. Configured in pyproject.toml [tool.ruff]. .py files only, because ruff would also reformat the Python examples in docs/*.md.
Import order ruff's isort rule (select = ["I"]) only. flake8 stays the lint, with the same options in .flake8 and scripts/lint.sh, plus one addition (below).
CI scripts/lint.sh, which the free-tier job already runs, now also runs ruff format --check and ruff check. It never rewrites anything. No workflow file changed.
Local make format rewrites. pre-commit gains the ruff-check --fix and ruff-format hooks, at the version requirements-dev.lock installs, which a test keeps equal.
Dependencies ruff>=0.17.6 in requirements-dev.txt. Re-locked with scripts/lock_requirements.sh: the only new lock entry is ruff==0.17.6.
Blame .git-blame-ignore-revs lists the formatting commit. GitHub reads it already; README gives the one-line git config for local blame.
csv_loader/ Excluded for now (extend-exclude), as you chose, so #431–#435 don't all conflict. Format it once they merge, then delete that line. A test fails if the excluded folder disappears.

Commits (review them one at a time)

  1. df7fceb config. The changes that let the formatting commit be purely mechanical:
    • flake8 ignores E203 (whitespace before :), in .flake8 and lint.sh alike. black-style slices like x[a + 1 :] trip it, and flake8's own docs call E203 incompatible with black.
    • # isort: split in 8 files. ruff's import sort treats blank lines between import groups as nothing. Without the marker it moved import api_client above from tests.unit import streamlit_harness, which puts streamlit_app/ on the path, and 7 test modules failed to import. The 8th is e2e/conftest.py, so preflight keeps its place.
    • Three statements reshaped so a comment stays on the line it applies to:
      • A # nosec B608: the SQL went into a variable (migration 0006).
      • A lambda carrying # noqa: E731 became a def.
      • An f-string's dict comprehension became a variable.
  2. 20cd32e the formatting. Output of make format only: 216 files.
  3. 1c92306 .git-blame-ignore-revs.
  4. 51b5e31 five functions split (dynamic_loader, originals, staged_import). The formatter spread them past [SE-09][Medium] Loader functions too long, with duplicated logic (SRP/DRY): _do_upload 274 lines, upload_te 240 #330's 80-line limit; _do_upload went from 75 to 109 lines. Each one now calls named helpers, and its statements run in the same order. Details are in the commit message.
  5. A merge of main, which brought in only the dependency pins and a plan document.

Evidence (local, PostgreSQL 16, all four environments deployed as CI does)

  • Formatting changes layout only. I parsed every Python file before and after commit 2 and compared the syntax trees. Apart from the order and grouping of imports, they are identical in 215 of the 216 files. The exception is tests/unit/test_config.py, where ruff re-indented the module docstring; nothing reads that docstring.
  • Import moves checked. A second script listed every import that moved ahead of an earlier blank-line group: 33 files.
    • All but the cases above are standard-library or typing imports moving above third-party ones, which can't change behaviour.
    • The ones that could are the streamlit_harness imports and e2e/conftest.py, and those are now held in place by # isort: split.
  • Full suite: pytest tests/ -m "not accessibility" → 1881 passed, 0 failed, 0 skipped. That includes refactor(loaders): split the loaders behind shared helpers, no function over 80 lines (#330) #414's golden test, which compares 28 upload responses byte for byte; it shows the function splits changed nothing either.
  • Coverage gate (make coverage, after merging main): passes.
    • dynamic_loader.py 91.9 %, te_loader.py 97.9 %, load_common.py 100 %.
    • api/ 95.9 %, streamlit_app/ 97.4 %.
  • Lint: scripts/lint.sh clean (flake8, bandit, ruff), and mypy clean (53 files).
  • Free tier: 1324 passed after the splits.
  • Lock tests: tests/unit/test_reproducible_builds.py 36 passed after the merge.
  • New tests in tests/unit/test_tool_config.py (they fail when one copy of a setting drifts from another):
    • ruff's line length equals flake8's.
    • ruff selects only I, uses force-exclude, and includes only *.py; the format hook skips Markdown.
    • lint.sh runs both ruff checks.
    • pre-commit's ruff matches the locked ruff.
    • .git-blame-ignore-revs lists only full commit ids.

For open PRs

Any open PR that changes Python files outside csv_loader/ will conflict on layout after this merges. The fix is to merge main into it and run make format on its own changes.


Generated by Claude Code

…d in CI and pre-commit (#324)

ruff formats (black's style, 120 columns, the flake8 line length) and orders imports, over .py files
only. flake8 stays the lint. scripts/lint.sh, which the free-tier job runs, now also checks
formatting and import order; `make format` rewrites. pre-commit gains the ruff-check and ruff-format
hooks, at the version requirements-dev.lock installs.

Made ready for the formatting commit that follows, so that commit changes layout only:
- flake8 ignores E203 (whitespace before ':'), in .flake8 and scripts/lint.sh alike. It is incompatible
  with black-style slices such as x[a + 1 :], as flake8's own documentation says.
- Two tests no longer depend on where a line breaks: an f-string's dict comprehension moved into a
  variable, and a lambda carrying '# noqa: E731' became a def. The formatter would have split the
  noqa onto another line.

csv_loader/ is excluded until #431-#435 merge, so those PRs do not all conflict.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011T6WqmGuQidunwiFfwKeBs
Mechanical: `make format` (ruff 0.17.6, pyproject.toml [tool.ruff]) over every tracked .py file except
csv_loader/. Nothing else changed. Each file's syntax tree is the same before and after, apart from the
order and grouping of its imports. The one exception is a module docstring's indentation in
tests/unit/test_config.py, and nothing reads that docstring.

Listed in .git-blame-ignore-revs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011T6WqmGuQidunwiFfwKeBs
…ines (#324, #330)

Formatting spread these over more lines, so tests/unit/test_function_length.py (#330's limit) failed:
_do_upload 109, store_upload 85, effective_rows 84, import_upload 84, upload_dynamic 83. Each now
calls helpers, and the statements run in the same order as before:
- dynamic_loader: _lost_race and _database_error (upload_dynamic's two error responses);
  _prepare_version (lock, versions, types, table), _cast_and_log and _flag_earlier_versions
  (_do_upload's steps)
- originals: _store_row_flags
- staged_import: _validate_row (effective_rows' per-row check); _refuse_unless_importable and
  _audit_import (import_upload)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011T6WqmGuQidunwiFfwKeBs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011T6WqmGuQidunwiFfwKeBs

Copy link
Copy Markdown
Owner Author

The amd64 / arm64 image scan and SBOM failures are not from this PR.


Generated by Claude Code

@amar-python
amar-python marked this pull request as ready for review November 28, 2026 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants