Skip to content

refactor: enforce template-python numeric lint limits - #233

Merged
furedea merged 6 commits into
mainfrom
refactor/apply-numeric-lint
Oct 18, 2026
Merged

furedea merged 6 commits into
mainfrom
refactor/apply-numeric-lint

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Adopt template-python's numeric ruff rules: max-args = 3, max-nested-blocks = 2 (PLR1702, preview-only via explicit-preview-rules), C901 with max-complexity = 10, and drop the PLR0911/PLR0912/PLR0913/PGH004 ignores.
  • Add a lefthook file-length check (500 lines) for hand-written code: py, sh, bash, bats, zsh, lua, js, ts. Nix modules, data, and docs are excluded.
  • Fix every violation with behavior-preserving splits (helper functions, guard clauses, small frozen dataclasses such as audit_log.Refusal, check_selection.Selection, session_gate.VerificationRun). No noqa was added.

Decisions to review

  • tests/** ignores PLR0917 in addition to the template's PLR0913. In ruff 0.16.3, PLR0917 (positional args) is stable and follows max-args, so pytest fixtures would add 83 test-only violations. The template is still on ruff 0.15 (where PLR0917 is preview) and has not hit this yet.
  • Nix is excluded from file-length because nix/home/default.nix is 727 lines. Splitting it belongs in a separate change.
  • CI does not run lefthook, so file-length is enforced locally only. Ruff limits are enforced in CI through the existing ruff check step.

Notable API changes (internal)

  • audit_log.blocked / escalated now take a single Refusal. The unused value/reason arguments are gone; they were never logged.
  • session_store.register(..., resuming=True) is now session_store.resume(...).
  • lint_engine.diagnostics now takes pre-filtered targets from supported_targets, and the unused readonly parameter is removed.

Tests

Before splitting, I added characterization tests and confirmed they pass on the original code:

  • guard/policy: test_forbidden_command, test_command_policy, test_guard_git, test_guard_file, test_git_safety
  • check selection: empty bats directory, vitest full suite
  • session gate: invalid test configuration
  • replay commands
  • verify_page

Verification

  • uv run --frozen pytest -m 'not integration': 1440 passed
  • lefthook run pre-commit --all-files: all commands pass, including file-length
  • bats tests/agents/bootstrap_server.bats tests/moshi/update_moshi_hook.bats: pass
  • bats tests/esa/zsh.bats: hangs at test 11 in the local sandbox. It hangs identically with the original zsh/esa.py, so the cause is the environment, not this change. CI should confirm.

Closes #227

🤖 Generated with Claude Code

@furedea
furedea merged commit dcb0709 into main Oct 18, 2026
11 checks passed
@furedea
furedea deleted the refactor/apply-numeric-lint branch October 18, 2026 17:10
furedea added a commit that referenced this pull request Nov 10, 2026
## 概要

dotfiles の ruff 設定を template-python に揃える (Refs
furedea/agent-config#74,cfg-align の dotfiles 行).

## 設定の変更 (`pyproject.toml`)

- `select` をテンプレートの順序にそろえ,`DOC` を追加 (違反 0).`CPY` は template-python
側でも削除済みのため入れない.
- `ignore` をテンプレートの一覧に置換し,下表の 18 件を削除.
- `PLR0917` は tests 限定の ignore からグローバルの `ignore` に移動
(ユーザー判断).理由はコメントで明記.
- プロジェクト固有の ignore は無い.

## 削除した ignore の元の理由

元の理由は `git log -S` で追跡.18 件はすべて 8428059 (2026-05,`build(python):
bootstrap uv project for hook and script tests`)
で,当時のスキル用スクリプトを温存するコメント付きで追加された.faa537f (#2)
でスクリプトが移設されたときにコメントが落ち,de295d1 (#52) で理由なしで再追加された.保護対象のスクリプト
(`agents/skills/**/scripts/*.py`,`run_audit.py` など) は faa537f と 83d1a89
(#98) で削除済みで,現在の Python は `github/`,`herdr/`,`zsh/`,`tests/` のみ.

削除後,全 18 規則を有効にして全体を検査した結果,違反が出たのは I001,E501,ARG001 のみで,下記のとおり修正済み.

| 規則 | 検査内容 | 元の理由 (8428059 のコメント) | 現在は当てはまらない理由 |
| -------------- | ------------------------------------- |
-----------------------------------------------------------------------------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| ARG001 | 未使用の関数引数 | "Patch schema keeps current_description for
compatibility" | 対象の `apply_patches.py` は削除済み.違反はテストの 1
件のみで,`usefixtures` に移して修正. |
| B007 | 未使用のループ変数 | "os.walk tuple names document positional contents
in existing code" | `os.walk` を使うコードは存在しない (grep で 0). |
| C401 | `set(generator)` を内包表記へ | "Existing aggregation code favors
explicit generator layout" | 対象の集計コード (`collect_transcripts.py` など)
は削除済み.違反 0. |
| DTZ005 | tz 無しの `datetime.now()` | "Timestamped local workspace names
intentionally use local time" | ローカル時刻の作業ディレクトリ名を作る `run_audit.py`
は削除済み.`datetime.now()` の使用 0. |
| E402 | import がファイル先頭にない | "run_audit adjusts sys.path before
importing sibling scripts" | `run_audit.py` は削除済み.`sys.path`
を操作するコードは存在しない (grep で 0). |
| E501 | 行が 119 桁超 | "Embedded HTML/JS templates intentionally preserve
long source strings" | `generate_report.py` の HTML/JS テンプレートは削除済み.残りの 6
行はテスト内の fake スクリプトで,分割して修正. |
| F541 | プレースホルダーの無い f-string | "Existing report/changelog writes
include harmless f-string literals" | レポート出力コードは削除済み.違反 0. |
| FURB110 | `x if x else y` を `x or y` へ | "Existing JSON output code
keeps explicit empty-list fallback branches" | 対象の JSON 出力コードは削除済み.違反 0.
|
| FURB122 | ループ内の `write` を `writelines` へ | "Existing changelog writes
favor straightforward loops" | changelog を書くコードは削除済み.違反 0. |
| FURB162 | 末尾 `Z` の手動置換 | "Existing timestamp parser normalizes
trailing Z explicitly" | transcript のタイムスタンプパーサーは削除済み.違反 0. |
| FURB171 | 1 要素のタプルでの `in` | "Existing message-type checks use tuple
style consistently" | message-type 判定のコードは削除済み.違反 0. |
| I001 | import の並び順 | "Keep script import blocks stable while sys.path
bootstrapping is present" | `sys.path` を操作するスクリプトが無くなり,理由が消えた.15 ファイルを
`ruff --fix` で整列. |
| PERF401 | ループでの `append` を内包表記へ | "Existing batching code favors
stepwise mutation for readability" | バッチ処理コードは削除済み.違反 0. |
| PTH | `os.path` 等を `pathlib` へ | "Legacy scripts use os.path; Path
migration is a separate refactor" | `os.path` を使うレガシースクリプトは削除済み.現在のコードは
`pathlib` で,違反 0. |
| PLW2901 | ループ変数の上書き | "Existing JSONL parser reuses line after
stripping whitespace" | JSONL パーサーは削除済み.違反 0. |
| RUF005 | リスト連結を展開構文へ | "Existing path reconstruction code is clearer
with list concatenation" | 対象のパス再構築コードは削除済み.違反 0. |
| UP015 | 冗長な `open` のモード指定 | "Explicit read/write modes are kept for
CLI-script clarity" | CLI スクリプトは削除済み.違反 0. |
| UP017 | `timezone.utc` を `datetime.UTC` へ | "Existing code imports
timezone explicitly" | `timezone` を import するコードは存在しない (grep で 0). |
| PLR0917 (移動) | 位置引数の数 (`max-args` に従う) | dcb0709 (#233): "In ruff
0.16.3, `PLR0917` (positional args) is stable and follows `max-args`, so
pytest fixtures would add 83 test-only violations." | 理由 (pytest
のフィクスチャ) は今も有効なので,無視は維持.位置引数の数は全引数数以下で,PLR0913 (max-args = 3)
が既に上限を課すため,個別の ignore はグローバルに移動して統一. |

元の理由がまだ成り立つ項目は無かったため,復元した ignore は無い.

## コード修正 (別コミット)

| 規則 | 修正前 | 修正後 | 対応 |
| ------ | ----------------------------------------------- | ------ |
------------------------------------------------------------------------------------
|
| I001 | 15 ファイル | 0 | `ruff --fix --select I001` |
| E501 | 6 行 (テスト内の fake スクリプトと文字列) | 0 | 変数の導入・括弧での分割.抑制なし |
| ARG001 | 1 (`tests/github/test_repo_cli.py` の `remote`) | 0 |
副作用目的のフィクスチャを `@pytest.mark.usefixtures("remote")` に移し,引数から削除 |

非自明な修正の設計上の理由:`remote` は fake `gh`
のリモート環境を用意する副作用のためだけに要求されていたので,usefixtures に移すのが pytest の意図に沿う (未使用引数を
`_` で隠すより正しい).

## 確認

- `ruff check` / `ruff format --check` 通過,`pytest` 164 passed,`ty` 通過
(pre-commit)
- ruff の `select` はテンプレートと同じ並び.`ignore` はテンプレートの一覧に `PLR0917` (理由コメント付き)
を加えたもの

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: furedea <[email protected]>
Co-authored-by: Claude Sonnet 5.5 <[email protected]>
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.

dotfiles にも数値の lint を入れて違反を直す

1 participant