from __future__ import annotations import csv from pathlib import Path def read_required_csv(path: Path, required_headers: list[str]) -> tuple[list[str], list[dict[str, str]]]: if not path.exists(): raise AssertionError(f"missing CSV file: {path}") with path.open("r", encoding="utf-8", newline="") as stream: reader = csv.DictReader(stream) headers = list(reader.fieldnames or []) missing = [name for name in required_headers if name not in headers] if missing: raise AssertionError(f"{path.name} missing headers: {', '.join(missing)}") rows = list(reader) return headers, rows def as_int(row: dict[str, str], key: str) -> int: try: return int(row[key]) except Exception as exc: raise AssertionError(f"invalid integer in {key}: {row.get(key)!r}") from exc def as_float(row: dict[str, str], key: str) -> float: try: return float(row[key]) except Exception as exc: raise AssertionError(f"invalid float in {key}: {row.get(key)!r}") from exc