From b3b4e7691b9359f6caafa7e04c31b46df9d49e2f Mon Sep 17 00:00:00 2001 From: Aleksandr Meshchryakov Date: Mon, 5 Oct 2026 15:22:14 +0200 Subject: [PATCH] refactor: keep FSSP normalization within lint limits --- src/apps/parsers/fssp_csv.py | 39 ++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/apps/parsers/fssp_csv.py b/src/apps/parsers/fssp_csv.py index 0cd363a..b0fa512 100644 --- a/src/apps/parsers/fssp_csv.py +++ b/src/apps/parsers/fssp_csv.py @@ -42,7 +42,7 @@ def valid_legal_inn(value: str) -> bool: return False weights = (2, 4, 10, 3, 5, 9, 4, 6, 8) return sum( - int(digit) * weight for digit, weight in zip(value, weights) + int(digit) * weight for digit, weight in zip(value, weights, strict=False) ) % 11 % 10 == int(value[-1]) @@ -99,6 +99,26 @@ def _termination(payload: dict, warnings: set[str]) -> None: warnings.add("unparsed_termination") +def _balances(raw: dict, warnings: set[str]) -> tuple[Decimal | None, Decimal | None]: + debt = _money(raw.get("Debt remaining balance"), warnings, signed=True) + claim = _money(raw.get("Amount due"), warnings, signed=False) + if debt is not None and debt < 0: + warnings.add("negative_debt_balance") + if debt is not None and claim is not None and debt > claim: + warnings.add("debt_exceeds_claim_amount") + return debt, claim + + +def _dates(payload: dict, comparison_date: date, warnings: set[str]) -> None: + for field in ("initiated_on", "document_date"): + payload[field] = _date(payload[field], warnings) + if ( + payload["document_date"] + and date.fromisoformat(payload["document_date"]) > comparison_date + ): + warnings.add("future_document_date") + + def iter_fssp_records( handle: TextIO, *, @@ -126,24 +146,13 @@ def iter_fssp_records( for row_number, values in enumerate(reader, 1): if len(values) != len(headers): raise SnapshotValidationError("invalid_fssp_csv_row_width") - raw = dict(zip(headers, values)) + raw = dict(zip(headers, values, strict=True)) payload = { field: raw[key].strip() or None for key, field in FSSP_HEADERS.items() } warnings: set[str] = set() - for field in ("initiated_on", "document_date"): - payload[field] = _date(payload[field], warnings) - if ( - payload["document_date"] - and date.fromisoformat(payload["document_date"]) > comparison_date - ): - warnings.add("future_document_date") - debt = _money(raw.get("Debt remaining balance"), warnings, signed=True) - claim = _money(raw.get("Amount due"), warnings, signed=False) - if debt is not None and debt < 0: - warnings.add("negative_debt_balance") - if debt is not None and claim is not None and debt > claim: - warnings.add("debt_exceeds_claim_amount") + _dates(payload, comparison_date, warnings) + debt, claim = _balances(raw, warnings) payload.update( { "origin_kind": "open_data_csv",