"""Shared parsing and application rules for explicit delivery waivers.""" from __future__ import annotations from typing import Any WAIVABLE_STATUSES = frozenset({"WARN", "SKIP"}) def parse_waivers(values: list[str]) -> dict[str, str]: """Parse CHECK=REASON values and reject malformed or duplicate entries.""" waivers: dict[str, str] = {} for value in values: name, separator, reason = value.partition("=") name = name.strip() reason = reason.strip() if not separator or not name or not reason: raise ValueError(f"Invalid waiver {value!r}; expected CHECK=REASON") if name in waivers: raise ValueError(f"Duplicate waiver for {name!r}") waivers[name] = reason return waivers def apply_waivers(checks: list[dict[str, Any]], waivers: dict[str, str]) -> None: """Apply waivers to WARN/SKIP checks and add FAIL checks for invalid targets. Passing a namespace-specific subset lets an earlier pipeline stage reject misspelled, unused, PASS, FAIL, or NOT_APPLICABLE targets before expensive downstream work begins. Final verification calls the same function with the complete waiver map, so both stages have identical semantics. """ by_name = {str(check.get("name", "")): check for check in checks} for name, reason in waivers.items(): check = by_name.get(name) if check is None: checks.append( { "name": f"waiver:{name}", "status": "FAIL", "detail": f"Unused or misspelled waiver: {name}", } ) continue status = str(check.get("status", "FAIL")) if status not in WAIVABLE_STATUSES: checks.append( { "name": f"waiver:{name}", "status": "FAIL", "detail": f"Waivers may only acknowledge WARN/SKIP checks; {name} is {status}", } ) continue check["original_status"] = status check["status"] = "PASS" check["waiver_reason"] = reason check["detail"] = f"WAIVED ({reason}): {check.get('detail', '')}"