Repository navigation
fix(xlsx): do not create empty cells while finding tables - #5024
DanielNg0729 merged 5 commits into
Conversation
|
✅ DCO Check Passed Thanks @chdre, all your commits are properly signed off. 🎉 |
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
AI triage (advisory)Checked commit The PR fixes a memory leak in CI safety: needs maintainer care before you approve CI.
1 related pull request(s) in the same code
The |
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the full diff at 7dbc2abc65. It read the code and did not run it.
I checked table detection in MsExcelDocumentBackend for sparse sheets, coordinate traversal order, and merged-cell fallback handling. The change avoids creating empty Cell objects in sheet._cells and keeps table extraction results intact.
A maintainer makes the merge decision. This review never approves the pull request.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
AI first review (advisory): no blocker found
This review covers the changes since 7dbc2abc65 at 9840b009e2. It read the code and did not run it.
The incremental change adds one test (test_threaded_comment_fallback_without_xml) for the threaded-comment XML-absent fallback path. The test logic is correct: cells with values are already in _cells after load, comments are injected in-memory, and _parse_threaded_comments correctly returns empty because no XML part exists in the saved file. The earlier review found no blockers; the original change has no new behavioral code in this diff. The human reviewer should confirm that Codecov's 5 uncovered lines do not include critical branches from this PR's new code paths.
A maintainer makes the merge decision. This review never approves the pull request.
ceberam
left a comment
There was a problem hiding this comment.
Thanks @chdre for this improvement. The performance fix is correct and well-benchmarked.
I only have a minor observations, mainly on the tests and the documentation.
test_missing_merge_anchor_inside_table_keeps_spandoes not fail onmain. On themainbranch the test passes because the old code usessheet.cell(), which re-creates the missing cell on the fly. Either:- Remove the test if its only purpose is to validate internal correctness of the new
_cells.get()path, or - Reframe it as an end-to-end test using
DocumentConverteron a file with a missing merge anchor (i.e. trigger the same condition through the public API, which would fail onmainbefore the fix only if the oldsheet.cell()path were also changed).
- Remove the test if its only purpose is to validate internal correctness of the new
test_threaded_comment_fallback_without_xmldoes not fail onmaineither. This test exercises the existing threaded-comment fallback path that was present before this PR. Running it onmainproduces the expected result. A test that already passed before the change provides no regression signal and gives no evidence that the PR leaves the fallback intact. Remove or reframe it as above; if it is intended to document a contract that was previously untested, note that in the PR description and make clear it is a coverage addition, not a regression test.test_sparse_merged_anchor_and_comment_only_cell_tablespasses onmain. For the same reason as above, this test passes onmain. It is a useful correctness check, but it should be labelled as a coverage test, not a regression test. The PR description implies it demonstrates behavior that the fix preserves; that framing is accurate, but the claim that the test "fails on main" is not.- Docstring for
contains_atsays "without a cell object". The phrase "without a cell object" describes the call context, not the method's contract. A reader who calls it with a coordinate that does have a cell object gets the same result. See my suggestion. sorted()in the outer loop has O(N log N) cost for dense sheets. For a dense sheet whereN = R × C, thesorted()call allocates a list of allNcoordinate tuples and sorts it. The PR benchmark shows dense sheets are slightly faster overall, which is encouraging. The cost is acceptable. Worth noting as a trade-off in a comment alongside thesorted()call for future readers who might remove it thinking it is unnecessary.
|
Thanks for the review. Addressed in the latest commit: removed the threaded comment fallback test (it only covered code this PR does not touch), labelled the two tests that pass on main as coverage tests in both the code and the description, kept the missing-anchor test because it is the only one reaching the new |
|
Sorry @chdre to bother you again, but I've just seen a conflict that needs to be resolved. Could you please rebase to |
Table detection used sheet.iter_rows and sheet.cell, which make openpyxl create a Cell for every empty position in the data bounds. Read sheet._cells directly instead, so memory is proportional to the filled cells. Output is unchanged. Signed-off-by: Christer Dreierstad <[email protected]>
Signed-off-by: Christer Dreierstad <[email protected]>
Remove the threaded comment fallback test, which exercises code this change does not touch. Label the two tests that pass on main as coverage tests. Tighten the contains_at docstring and note the sorted() trade-off. Signed-off-by: Christer Dreierstad <[email protected]>
tests/test_backend_msexcel.py grew past the 1500 line limit after rebasing onto main. Signed-off-by: Christer Dreierstad <[email protected]>
77513f8 to
270d0f1
Compare
|
Rebased onto main and resolved the conflict with the number-format change (a missing cell now yields empty text before |
|
Thanks @chdre for fixing the issue.
I think it's better to add this test module to the exception list |
Add the file to .github/max-lines-ignore instead of splitting it. Signed-off-by: Christer Dreierstad <[email protected]>
|
Good point, done. |
Table detection in
MsExcelDocumentBackendcalledsheet.iter_rows(...)andsheet.cell(...)over the whole data bounds. In openpyxl both create aCellfor every position they visit and keep it insheet._cells. A sheet with a few values in a large rectangle therefore grew by one cell object per empty position, about 200 MB per million cells, and that memory was not released.This change reads
sheet._cellsdirectly in_find_data_tablesand_find_table_bounds:_MergedCellIndex.contains_at.The output does not change.
_cellswas already read directly in_find_true_data_boundsand_value_bounds.Checklist
Review evidence
Measured with
DocumentConverter().convert()on generated workbooks, median of 3, tracemalloc (Python allocations only), Windows, Intel Core Ultra 9 285HX.Dense sheets are unchanged, as expected.
New tests in
tests/test_backend_msexcel.py:test_find_data_tables_does_not_materialize_empty_cells: the sheet's cell count is unchanged by table detection, and peak memory stays under 50 MiB. It is the regression test and fails on main.test_sparse_merged_anchor_and_comment_only_cell_tables: coverage test, output equality for merges and comments in a sparse sheet. It also passes on main.test_missing_merge_anchor_inside_table_keeps_span: coverage test for thecontains_atfallback, a merge whose anchor cell is missing keeps its span. It passes on main, which recreates the cell instead.Old and new
_find_data_tablesoutput was also compared on all workbooks intests/dataand on 300 random sheets (values, merges, comments, gap tolerance 0 to 3), with 0 differences.Commands run:
uv run pytest tests/test_backend_msexcel.py tests/test_backend_msexcel_charts.py: 43 passed, 2 skipped.uv run prek run --files ...: all hooks passed.Not run:
test_e2e_excel_conversionspasses unchanged.Not included:
ExcelCellandTableCell.sheet.cell(); it is already capped byMAX_CHART_TABLE_CELLS.