Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog.d/849.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added

- Enforced per-kind suppression comment and configuration waiver ceilings across `src/`, `tests/`, and `pyproject.toml` via `tokenize.COMMENT` inspection (`type: ignore`, per-rule `noqa`, `nosec`, `pragma: no cover`, `pragma: no mutate`, file-level directives, `ast-grep-ignore`, Ruff `per-file-ignores`, `[[tool.mypy.overrides]]`, and coverage `omit`), and lowered `DEFAULT_C901_SUPPRESSION_CEILING` from 65 to 51 (#849).
21 changes: 21 additions & 0 deletions docs/agent/tasks/task-849-suppression-comment-ceilings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Task: Suppression Comments May Not Grow Unnoticed (#849)

**Issue**: [#849](https://github.com/dan-petty/devops-cli/issues/849)
**Status**: Done
**Feasibility**: Verified against Python 3.14 `tokenize.generate_tokens` (C-accelerated tokenizer), `src/devops_cli/config/constants.py`, `src/devops_cli/config/defaults.py`, `pyproject.toml`, and `tests/test_architectural_invariants.py`; confirmed comment-token scanning with a raw-text pre-filter completes in ~0.48 s across all `src/` and `tests/` modules without false positives on string literals or `'nanosecond'` prose comments.

## Summary

- Defined suppression kind constants (`CONST_SUPPRESSION_KINDS`, `CONST_COMMENT_SUPPRESSION_KINDS`, `CONST_PYPROJECT_SUPPRESSION_KINDS`) and comment-token regular expressions in `src/devops_cli/config/constants.py`.
- Defined `DEFAULT_SUPPRESSION_CEILINGS` and `DEFAULT_NET_CONSTRUCT_SUPPRESSION_CEILING` in `src/devops_cli/config/defaults.py`, set to exact current counts across `src/`, `tests/`, and `pyproject.toml`, and lowered `DEFAULT_C901_SUPPRESSION_CEILING` from `65` to `51` to match the current count.
- Added `test_suppressions_stay_under_their_ceilings` and companion unit tests in `tests/test_architectural_invariants.py` that count `type: ignore`, per-rule-code `noqa`, `nosec`, `pragma: no cover`, `pragma: no mutate`, file-level directives, and `ast-grep-ignore` (`net-*` rule IDs in `src/`) from `tokenize.COMMENT` tokens alongside `per-file-ignores`, `[[tool.mypy.overrides]]`, and coverage `omit` entries from `pyproject.toml`.

## Acceptance Criteria

- [x] One test in `tests/test_architectural_invariants.py` counts, with `tokenize` (comment tokens only), over `src` and `tests`: `type: ignore`, `noqa` per rule code, `nosec`, `pragma: no cover`, `pragma: no mutate`, file-level directives, and `ast-grep-ignore` per `net-*` rule ID (`tests/test_architectural_invariants.py::test_suppressions_stay_under_their_ceilings`).
- [x] From `pyproject.toml` it also counts `per-file-ignores` entries, `[[tool.mypy.overrides]]` blocks, and coverage `omit` entries (`tests/test_architectural_invariants.py::test_every_comment_and_pyproject_suppression_kind_is_counted`).
- [x] The kinds live in `src/devops_cli/config/constants.py` and the ceilings in `src/devops_cli/config/defaults.py`, set to the counts at merge, with each check enforcing `count <= ceiling`.
- [x] Adding one `# type: ignore` to a module in `src` fails the test, naming the kind, the ceiling, and the file, and instructing to fix the cause or raise the ceiling with a reason recorded in the task file (`tests/test_architectural_invariants.py::test_adding_a_type_ignore_in_src_fails_naming_kind_ceiling_and_file`).
- [x] A fixture with `'nanosecond'` in a comment is not counted (`tests/test_architectural_invariants.py::test_nanosecond_comment_and_string_literals_are_not_counted`).
- [x] Removing a suppression keeps the test green (`tests/test_architectural_invariants.py::test_removing_a_suppression_keeps_the_ceiling_check_green`).
- [x] The test runs in at most 1 s, with no new dependency.
67 changes: 67 additions & 0 deletions src/devops_cli/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3112,6 +3112,73 @@
CONST_RUFF_C901_SUPPRESSION_COMMENT: Final[re.Pattern[str]] = re.compile(
r"#[^\S\n]*ruff[^\S\n]*:[^\S\n]*[\w-]+[^\S\n]*\[[^\]\n]*\bC901\b", re.IGNORECASE
)
# Suppression comment and configuration kinds tracked against ceilings (#849).
CONST_SUPPRESSION_KIND_TYPE_IGNORE: Final[str] = "type: ignore"
CONST_SUPPRESSION_KIND_NOQA_PREFIX: Final[str] = "noqa"
CONST_SUPPRESSION_KIND_NOSEC: Final[str] = "nosec"
CONST_SUPPRESSION_KIND_PRAGMA_NO_COVER: Final[str] = "pragma: no cover"
CONST_SUPPRESSION_KIND_PRAGMA_NO_MUTATE: Final[str] = "pragma: no mutate"
CONST_SUPPRESSION_KIND_FILE_DIRECTIVE: Final[str] = "file-level directive"
CONST_SUPPRESSION_KIND_AST_GREP_IGNORE: Final[str] = "ast-grep-ignore"
CONST_SUPPRESSION_KIND_PER_FILE_IGNORES: Final[str] = "per-file-ignores"
CONST_SUPPRESSION_KIND_MYPY_OVERRIDES: Final[str] = "[[tool.mypy.overrides]]"
CONST_SUPPRESSION_KIND_COVERAGE_OMIT: Final[str] = "coverage omit"
CONST_COMMENT_SUPPRESSION_KINDS: Final[tuple[str, ...]] = (
CONST_SUPPRESSION_KIND_TYPE_IGNORE,
CONST_SUPPRESSION_KIND_NOQA_PREFIX,
CONST_SUPPRESSION_KIND_NOSEC,
CONST_SUPPRESSION_KIND_PRAGMA_NO_COVER,
CONST_SUPPRESSION_KIND_PRAGMA_NO_MUTATE,
CONST_SUPPRESSION_KIND_FILE_DIRECTIVE,
CONST_SUPPRESSION_KIND_AST_GREP_IGNORE,
)
CONST_PYPROJECT_SUPPRESSION_KINDS: Final[tuple[str, ...]] = (
CONST_SUPPRESSION_KIND_PER_FILE_IGNORES,
CONST_SUPPRESSION_KIND_MYPY_OVERRIDES,
CONST_SUPPRESSION_KIND_COVERAGE_OMIT,
)
CONST_SUPPRESSION_KINDS: Final[tuple[str, ...]] = (
*CONST_COMMENT_SUPPRESSION_KINDS,
*CONST_PYPROJECT_SUPPRESSION_KINDS,
)
# Fast pre-filter over source text before tokenizing, and comment-token patterns (#849).
CONST_SUPPRESSION_COMMENT_PREFILTER_RE: Final[re.Pattern[str]] = re.compile(
r"#[^\n]*(?:type[^\S\n]*:[^\S\n]*ignore\b|noqa\b|nosec\b"
r"|pragma[^\S\n]*:[^\S\n]*no[^\S\n]+(?:cover|mutate)\b"
r"|(?:ruff|flake8|mypy|pyright|pylint|bandit)[^\S\n]*:|ast-grep-ignore\b)",
re.IGNORECASE,
)
CONST_TYPE_IGNORE_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*type[^\S\n]*:[^\S\n]*ignore\b", re.IGNORECASE
)
CONST_NOQA_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*noqa(?:[^\S\n]*:[^\S\n]*"
r"(?P<codes>[A-Z0-9]+(?:(?:[^\S\n]*,[^\S\n]*|[^\S\n]+)[A-Z0-9]+)*)|(?=$|[^\S\n]+#))",
re.IGNORECASE,
)
CONST_NOQA_RULE_CODE_RE: Final[re.Pattern[str]] = re.compile(r"[A-Z0-9]+")
CONST_NOSEC_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*nosec\b", re.IGNORECASE
)
CONST_PRAGMA_NO_COVER_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*pragma[^\S\n]*:[^\S\n]*no[^\S\n]+cover\b", re.IGNORECASE
)
CONST_PRAGMA_NO_MUTATE_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*pragma[^\S\n]*:[^\S\n]*no[^\S\n]+mutate\b", re.IGNORECASE
)
CONST_FILE_DIRECTIVE_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*(?:(?:ruff|flake8)[^\S\n]*:[^\S\n]*noqa\b"
r"|ruff[^\S\n]*:[^\S\n]*(?:file-ignore|disable|ignore)\b"
r"|(?:mypy|pyright)[^\S\n]*:|(?:pylint|bandit)[^\S\n]*:[^\S\n]*skip-file\b)",
re.IGNORECASE,
)
CONST_AST_GREP_IGNORE_COMMENT_RE: Final[re.Pattern[str]] = re.compile(
r"(?:^|[^\S\n])#[^\S\n]*ast-grep-ignore(?:[^\S\n]*:[^\S\n]*(?P<ids>[^\n#]+))?\b",
re.IGNORECASE,
)
CONST_AST_GREP_NET_RULE_ID_RE: Final[re.Pattern[str]] = re.compile(
r"\bnet-[a-z0-9-]+\b", re.IGNORECASE
)

# Who adjudicated a recorded finding when nothing says. The feedback exporter defaulted to
# "human", which routed every finding the verifier never reached into the human
Expand Down
30 changes: 29 additions & 1 deletion src/devops_cli/config/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,35 @@
DEFAULT_MAX_COMPLEXITY: int = 10
# The C901 markers src/ and tests/ may carry: the functions over the cap when Ruff began
# enforcing it (#586). It only goes down; a function over the cap is decomposed instead.
DEFAULT_C901_SUPPRESSION_CEILING: int = 65
DEFAULT_C901_SUPPRESSION_CEILING: int = 51
# The `net-*` rule ids listed in `# ast-grep-ignore:` comments under src/ (#849, #1107).
DEFAULT_NET_CONSTRUCT_SUPPRESSION_CEILING: int = 0
# Ceilings for every suppression comment kind in src/ and tests/ and configuration waiver in
# pyproject.toml (#849). Each check is count <= ceiling; any unlisted noqa rule code has a
# ceiling of 0.
DEFAULT_SUPPRESSION_CEILINGS: dict[str, int] = {
"src type: ignore": 91,
"src noqa: C901": DEFAULT_C901_SUPPRESSION_CEILING,
"src noqa: E402": 2,
"src noqa: F401": 22,
"src noqa: SIM115": 1,
"src noqa: UP036": 1,
"src nosec": 14,
"src pragma: no cover": 1,
"src pragma: no mutate": 0,
"src file-level directive": 0,
"src ast-grep-ignore": DEFAULT_NET_CONSTRUCT_SUPPRESSION_CEILING,
"tests type: ignore": 228,
"tests noqa: F401": 4,
"tests noqa: N803": 1,
"tests nosec": 17,
"tests pragma: no cover": 0,
"tests pragma: no mutate": 0,
"tests file-level directive": 0,
"per-file-ignores": 0,
"[[tool.mypy.overrides]]": 2,
"coverage omit": 0,
}
DEFAULT_MAX_NESTING_DEPTH: int = 5
DEFAULT_QUANTIZATION_BITS: int = 16

Expand Down
Loading
Loading