Option C Tools
Loading...
Searching...
No Matches
waivers.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# oct/core/waivers.py
4
5"""
6Purpose
7-------
8OI-540 — shared inline-waiver parsing for OCT-LINT comments.
9
10Extracted from :mod:`oct.linter.oct_lint` so that both the linter and
11the quality gate (:mod:`oct.git.quality_gate`) can import the same
12parser without the quality gate depending on the linter.
13
14Responsibilities
15----------------
16- Host :data:`_WAIVER_RE` — the canonical regex for
17 ``# OCT-LINT: disable=<rule> [reason="..."]`` comments.
18- Provide :func:`parse_inline_waivers` — returns a mapping of
19 line numbers to ``{"rule": str, "reason": str|None}`` dicts.
20
21Diagnostics
22-----------
23Domain: OCT-CORE
24Levels:
25 (No diagnostics emitted — pure parsing helper.)
26
27Contracts
28---------
29- Must not import :mod:`oct.linter.oct_lint` (no circular dependency).
30- Return shape is stable — both linter and quality gate depend on it.
31
32Dependencies
33------------
34"""
35
36from __future__ import annotations
37
38import re
39
40
41_WAIVER_RE = re.compile( # OCT-LINT: disable=no_hardcoded_secrets reason="Waiver regex pattern, not a secret"
42 r'#\s*OCT-LINT:\s*disable\s*=\s*(?P<rule>[a-z_]+)(?:\s+(?P<reason>.*))?',
43 re.IGNORECASE,
44)
45
46
47def parse_inline_waivers(text: str) -> dict[int, dict]:
48 """Parse ``# OCT-LINT: disable=<rule> [reason]`` comments from source.
49
50 Returns a dict mapping line numbers (1-based) to
51 ``{"rule": str, "reason": str|None}``.
52 """
53 waivers: dict[int, dict] = {}
54 for lineno, line in enumerate(text.splitlines(), 1):
55 m = _WAIVER_RE.search(line)
56 if m:
57 reason = (m.group("reason") or "").strip() or None
58 waivers[lineno] = {"rule": m.group("rule").lower(), "reason": reason}
59 return waivers
dict[int, dict] parse_inline_waivers(str text)
Definition waivers.py:47