Skip to content

fix: make cached mapping indices read-only - #85

Merged
paulmueller merged 1 commit into
DC-analysis:mainfrom
paulmueller-agent:fix-mapping-cache-readonly
Nov 23, 2026
Merged

paulmueller merged 1 commit into
DC-analysis:mainfrom
paulmueller-agent:fix-mapping-cache-readonly

Conversation

@paulmueller-agent

Copy link
Copy Markdown
Contributor

get_mapping_indices caches its result for integer and slice mappings (functools.lru_cache in _get_mapping_indices_cached), so every caller gets the same array object. A caller that modified the returned array in place changed the result of all later calls with that mapping:

a = get_mapping_indices(10)
a[0] = 99
get_mapping_indices(10)[:3]  # main: [99, 1, 2]

No code in dcnum modifies these arrays, but code outside dcnum, or future code, could silently corrupt the mapping of unrelated HDF5Data instances.

Fix

The cached arrays are made read-only (setflags(write=False)). An in-place modification now raises ValueError: assignment destination is read-only. This is cheaper than copying on every call, which matters for large arange mappings. The docstring now has a "Returns" section that mentions this.

Tests

  • New test_mapping_indices_cache_not_modifiable[10|slice(2, 8)|slice(1, 9, 2)]. It fails on main and passes here.
  • The full suite passes, except 3 torch tests (torch is not installed locally; they fail on main too). All 74 pipeline tests pass, run one per process.
  • flake8 is clean, and ruff shows no new errors.

🤖 Generated with Claude Code

`get_mapping_indices` returns the same cached array for the same
integer or slice mapping. A caller that modified the returned array
in place changed the result of all later calls with that mapping.
The cached arrays are read-only now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.26%. Comparing base (c7a8797) to head (f1cd0ea).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
dcnum/read/mapped.py 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #85      +/-   ##
==========================================
+ Coverage   88.23%   88.26%   +0.02%     
==========================================
  Files          63       63              
  Lines        4539     4541       +2     
==========================================
+ Hits         4005     4008       +3     
+ Misses        534      533       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@paulmueller
paulmueller merged commit dd52e14 into DC-analysis:main Nov 23, 2026
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants