Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(spreadsheet): stop .xls integer rendering fabricating digits above 2^53 (#3704)

Tejas Choprae8a510982a1e
diff --git a/headroom/transforms/spreadsheet_ingest.py b/headroom/transforms/spreadsheet_ingest.py
index ca77b0c4d..cd4fd62e0 100644
--- a/headroom/transforms/spreadsheet_ingest.py
+++ b/headroom/transforms/spreadsheet_ingest.py
@@ -77,6 +77,10 @@ def _xls_cell(cell: object, datemode: int) -> object:
so a whole number arrives as ``12.0``. Left alone, the two loaders disagree
about the same workbook -- ``45292.0`` here against ``2024-01-01 00:00:00``
there -- and the date is not recoverable from the text.
+
+ Sub-second precision is dropped: ``xldate_as_tuple`` returns whole seconds, so
+ ``datetime(2024, 1, 1, 12, 34, 56, 500000)`` renders as ``2024-01-01 12:34:56``
+ where openpyxl keeps the microseconds.
"""
import xlrd
@@ -153,5 +157,5 @@ def load_spreadsheet(path: str | Path) -> dict[str, str]:
if suffix == ".xlsx":
return _load_xlsx(p)
if suffix == ".xls":
- return _load_xls(p) # pragma: no cover - legacy .xls path, see _load_xls
+ return _load_xls(p)
raise ValueError(f"Unsupported spreadsheet format '{suffix}'. Supported: .xlsx, .xls")
diff --git a/tests/test_transforms_tabular.py b/tests/test_transforms_tabular.py
index 496e47782..78123e181 100644
--- a/tests/test_transforms_tabular.py
+++ b/tests/test_transforms_tabular.py
@@ -609,6 +609,59 @@ def test_load_xls_and_xlsx_agree_on_the_same_values(tmp_path) -> None:
assert load_spreadsheet(xls_path) == load_spreadsheet(xlsx_path)
+class _StubXlsCell:
+ """The whole surface ``_xls_cell`` reads: xlrd's ``ctype`` and ``value``."""
+
+ def __init__(self, ctype: int, value: object) -> None:
+ self.ctype = ctype
+ self.value = value
+
+
[email protected]("value", [12.0, 1e15, float(2**53 - 1), float(2**53), float(-(2**53))])
+def test_xls_cell_converts_exact_whole_numbers_to_int(value: float) -> None:
+ """At or below 2**53 every integer is representable, so ``int()`` loses nothing.
+
+ The bound is inclusive at both ends: +/-2**53 is exactly representable, and
+ openpyxl reads the same value from an .xlsx as an ``int``, so excluding it
+ would make the two loaders disagree at exactly the boundary.
+ """
+ xlrd = pytest.importorskip("xlrd")
+
+ from headroom.transforms.spreadsheet_ingest import _xls_cell
+
+ rendered = _xls_cell(_StubXlsCell(xlrd.XL_CELL_NUMBER, value), 0)
+
+ assert isinstance(rendered, int)
+ assert rendered == int(value)
+
+
+ "value",
+ [float(2**53 + 2), float(-(2**53) - 2), 1e16, 1e20, 123456789012345678.0],
+)
+def test_xls_cell_keeps_numbers_past_2_53_as_floats(value: float) -> None:
+ """Past 2**53 ``int()`` would fabricate digits the workbook never held.
+
+ ``2**53 + 2`` is the first whole number above the boundary (``2**53 + 1``
+ is not representable at all), and ``-(2**53) - 2`` its negative mirror.
+
+ xlrd hands back a double, and above 2**53 consecutive integers are no longer
+ representable, so ``int()`` renders the double's exact value rather than the
+ number that was typed: a cell holding 123456789012345678 prints as
+ 123456789012345680 -- an identifier that reads as exact and is wrong in its
+ last two digits. The float repr says "approximate" out loud, and is also what
+ the .xlsx loader shows for the same workbook.
+ """
+ xlrd = pytest.importorskip("xlrd")
+
+ from headroom.transforms.spreadsheet_ingest import _xls_cell
+
+ rendered = _xls_cell(_StubXlsCell(xlrd.XL_CELL_NUMBER, value), 0)
+
+ assert isinstance(rendered, float)
+ assert rendered == value
+
+
def test_load_spreadsheet_rejects_unknown_extension(tmp_path) -> None:
from headroom.transforms.spreadsheet_ingest import load_spreadsheet