Skip to content

ci(lint): Ruff PLR1702 caps nesting at 4 blocks and replaces the hook - #2602

Merged
dan-petty merged 4 commits into
mainfrom
ci/1876-ruff-nesting-cap
Oct 29, 2026
Merged

dan-petty merged 4 commits into
mainfrom
ci/1876-ruff-nesting-cap

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

Ruff's PLR1702 replaces the structural-invariants pre-commit hook and the nesting metric that the project wrote.

  • pyproject.toml selects PLR1702 with max-nested-blocks = 4, through lint preview with explicit-preview-rules, so no other preview rule runs. The formatter stays out of preview.
  • At 4, Ruff fails five nested if, for, while, with or except blocks, as the hook did, and it also counts a try body. The 12 functions with five blocks under that count each move one block into a helper.
  • The hook, the nesting leg of the whole-tree test and the nesting code of core/code_metrics.py are deleted. devops scan complexity drops --max-indent, and devops ai spec drops its indentation rule.
  • Ruff does not count match blocks or an assert. No code relies on either gap today.
  • refactor(ci): record keep-port decision #2600 decides the McCabe port that stays.

Closes #1876

Change: openspec/changes/archive/2026-10-29-1876-ruff-nesting-cap/

…licit lint preview

`pyproject.toml` selects PLR1702 with `max-nested-blocks = 4`. PLR1702 is a
preview rule in Ruff 0.16, so `[tool.ruff.lint]` sets `preview = true` with
`explicit-preview-rules = true`; the formatter keeps its stable style. At 4,
Ruff fails five nested `if`, `for`, `while`, `with` or `except` blocks, as
the `structural-invariants` hook did, and it also counts a `try` body. The
12 functions with five blocks once a `try` body counts each move one block
into a helper, with the same behavior. `tests/test_ruff_complexity_cap.py`
holds the cap, the preview settings and the formatter's style, and five
nested `if` blocks fail while four pass.
…e that PLR1702 replaces

The pre-commit `structural-invariants` hook, the nesting leg of the
whole-tree test, and `nesting_depth`, `breaches`, `main` and the
`max_nesting_depth` field of `core/code_metrics.py` are deleted, with
`DEFAULT_MAX_NESTING_DEPTH`. Ruff gives no nesting figure for each function
and is a development dependency, so `devops scan complexity` drops
`--max-indent` and its nesting findings, the MCP tool drops `max_indent`,
and `devops ai spec` drops the `max_indentation` rule and keeps one
forbidden-import rule for each file. The documents name PLR1702 at 4.
@dan-petty

Copy link
Copy Markdown
Owner Author

Evidence (Ruff 0.16.8; base origin/main c96dbfe3, after #2010):

Both tools on src/ once, as #1876 asks for a replaced check:

Tool Base Branch
structural-invariants hook, --max-depth 5 0 breaches deleted
Ruff PLR1702, max-nested-blocks = 5 0 —
Ruff PLR1702, max-nested-blocks = 4 13 findings in 12 functions 0
ruff check --config pyproject.toml . with lint preview, every selected rule 0 (after #2010) 0

Generated samples, Ruff at 4 against the hook at 5:

Innermost statement inside 4 blocks 5 blocks
if, for, while, with, except both pass both fail
try bodies both pass Ruff fails, the hook passes
match cases both pass the hook fails, Ruff passes

Tests:

  • The new checks in tests/test_ruff_complexity_cap.py (cap, preview settings, formatter style, five weakened tables, five-versus-four negative control) failed on the base settings and pass.
  • The new scan, spec-verifier and docs-resolver tests failed on the base and pass.
  • Full uv run devops ci before the last rebase: every gate passed except one test, test_ruff_tid251_detects_banned_socket_apis. It read Ruff's text output, which names a rule (banned-api) under lint preview. It now reads the JSON output. 12,578 other tests passed.
  • The tests of the 12 changed modules: 5,083 passed.

Cap choice: 4, not the 5 of the owner's 2026-10-22 decision, because 5 allows a fifth if, for, while or with block that the hook refused.

Follow-up filed: #2600 (the McCabe port, decision 4 of #1876).

Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).

@dan-petty
dan-petty marked this pull request as ready for review October 29, 2026 02:41
@dan-petty
dan-petty merged commit 31b82cd into main Oct 29, 2026
16 checks passed
@dan-petty
dan-petty deleted the ci/1876-ruff-nesting-cap branch October 29, 2026 02:41
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.

ci(lint): Ruff PLR1702 caps nesting and the structural-invariants hook is deleted

1 participant