Model/infrastructure/s3/gzip_csv_s3_client.py
Khalim Conn-Kowlessar 246834fac0 Map shard rows to HistoricEpc field-by-field so every column is pyright-checked 🟩
The row→domain mapper now names all 93 constructor arguments explicitly
instead of splatting a lowercased dict, takes a plain Mapping (a
DataFrame.to_dict("records") row) instead of a pandas Series, and ignores
columns the domain type doesn't know. A missing/renamed CSV column fails
loudly as a KeyError at the row. Both iterrows() call sites move to
to_dict("records") — pandas-stubs types iterrows' Series unparameterized,
which strict mode rejects. pandas-stubs + boto3-stubs[s3] make the stack
check clean: pyright strict is now 0 errors across the PR's files.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 14:58:09 +00:00

26 lines
1 KiB
Python

from __future__ import annotations
from io import BytesIO
import pandas as pd
from infrastructure.s3.s3_client import S3Client
class GzipCsvS3Client(S3Client):
"""Reads a gzipped-CSV S3 object into a pandas DataFrame.
The S3-facing half of the historic-EPC read: an :class:`S3Client` (injected
boto client + bucket) plus the gzip/CSV decode, so a Repo can depend on this
instead of the ``utils.s3`` free functions. ``low_memory=False`` so the wide,
mixed-type historic-EPC columns infer a dtype from the whole column rather
than per-chunk (which would otherwise split one column across object/float).
"""
def read_csv_gz(self, key: str) -> pd.DataFrame:
raw = self.get_object(key)
# pandas-stubs' read_csv overloads carry bare generics of their own, so
# strict mode flags the member access; the call and return are typed.
return pd.read_csv( # pyright: ignore[reportUnknownMemberType]
BytesIO(raw), compression="gzip", low_memory=False
)