Skip to content

B5: Element fidelity, legacy stores and HDF5 safety - #117

Merged
Claptar merged 5 commits into
mainfrom
fix/b5
Nov 2, 2026
Merged

Claptar merged 5 commits into
mainfrom
fix/b5

Conversation

@Claptar

@Claptar Claptar commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #95. Part of #87. For 0.17.5: merge after 0.17.4 is tagged.

Every element anndata or scanpy writes now survives every copy path (HDF5 to HDF5, HDF5 to Zarr, Zarr to HDF5, Zarr v2 and v3), checked by anndata reading each copy back (and scanpy, for rank_genes_groups), on stores anndata itself wrote, including real anndata 0.7.8 files.

What changed, by ref

  • F041 (security): hardening for crafted HDF5 inputs. open_store checks an HDF5 file's layout when it opens it, the copy paths check what they copy, and convert checks its HDF5 sources; the input is refused before anything is read.
  • F017: record arrays (rec-array, scanpy's rank_genes_groups) are copied field by field: text fields as variable-length UTF-8 to HDF5 and as <U of the longest value to Zarr, as anndata writes them; other fields kept. rec-array 0.2.0 added to spec.
  • F021: the HDF5 fill value is carried only for a numeric dtype with a non-zero fill, so a record's fill (a value per field, text included) no longer reaches h5py, which crashed the process.
  • F006: HDF5-to-HDF5 copies use expand_refs=True, so anndata 0.7 dataframes nested in raw, obsm or uns keep live references (HDF5 repoints them at the copies). MuData modalities too.
  • F072: a legacy categorical's ordered is read from __categories/<key>, where anndata 0.7 kept it (read.is_ordered(col, parent)), in every copy.
  • F164: a store in anndata 0.7's layout (untagged root, 0.1.0 obs/var) opens without the "missing or invalid AnnData attrs" warning.
  • F130: copy_attrs decodes bytes at any depth, writes h5py.Empty as JSON null (and null back as h5py.Empty), and refuses a reference-valued attribute naming the element and attribute.
  • F126: h5py's variable-length numbers and object references are not text any more: a copy to Zarr refuses them with a message naming the element; between HDF5 files they are copied (references repointed).
  • F125: float32/float16 render at their own precision (0.1, not 0.10000000149011612), matching pandas' to_csv.
  • F071: create_dataset converts a carried fill value to the new dtype, or leaves it to HDF5's default when the conversion would change it; recast to bool of a float h5ad matrix works (CSR, CSC, dense).
  • F134: Zarr v2 codecs are read through compressors, not the deprecated compressor, and the bare except Exception blocks around zarr attributes are gone.
  • F135: an uncompressed v2 array stays uncompressed (an explicit None is forwarded).
  • F136: only a tagged null is written as a null. An untagged empty dataspace is copied as itself between HDF5 files, and left out of Zarr with a warning.
  • F133: the pickle path writes text with a missing value as a categorical (code -1), which anndata 0.8 to 0.13 all read, never a nullable-string-array.
  • F087: anndata 0.7.8 joins the version matrix (read only, Python 3.10). Every release's stack is pinned exactly, and the external-reader oracles (scanpy, PyTables, loompy, mudata, squidpy, geopandas, pyarrow, anndata) are pinned in reference_stores.ORACLES. Two stores written by anndata 0.7.8 are committed under tests/fixtures/anndata-0.7.8/ (118 KB) so the unit suite reads the real legacy layout.
  • F090: the plain string-array encoding is tested under pandas 3: concat of plain, nullable and mixed inputs against anndata.concat. It passes on main (B3a fixed the path); the gap was the test.
  • F086: the version suite now reads back convert (both backends) and split + concat outputs with the release that wrote the input; the reference store gained a categorical in raw/var and rank_genes_groups. The docs' claim says which outputs are read back, and troubleshooting has an entry for older releases refusing nullable-string-array or null.
  • F154: spec-zarr.md and spec-h5ad.md: the legacy Zarr categories path is relative (__categories/<key>), ordered lives on the labels, dict is current rather than legacy, Zarr string scalars are variable-length UTF-8, a rec-array section, and what 0.7 files tag.
  • F156: testing.md and reference_stores.py: 0.11 is the first release that reads a nullable-string-array, 0.12 the first that reads a null.
  • F163: removed strip_string_filters, write._backend_of and _target_zarr_format; storage takes its encoding names and versions from elements.spec (the adata.elements package now loads its submodules lazily, which removes the import cycle).

Also changed, outside the batch's files

  • pyproject.toml / uv.lock: zarr floor 3.1.5 -> 3.2.0, lock 3.1.5 -> 3.4.1 (adds msgspec). zarr 3.2 changed how a v3 record dtype is spelt (struct), and 3.1 cannot open a store that newer zarr wrote with one: anndata 0.13.4 on zarr 3.4 writes rank_genes_groups that way, and the integration suite's view failed on it. 3.2.0 reads both spellings.
  • commands/convert.py: one call checking the HDF5 source (and --raw), since most converters open their HDF5 with h5py directly.
  • core/concat.py (4 lines, _hashable) and core/subset.py (2 lines, _growable_like): review findings 2 and 4 below; B3b and B4 have been told.
  • formats/json_data.py: 3 lines; export dict of an h5ad holding rank_genes_groups failed ("bytes is not JSON serializable"). A record array now exports as {field: [values]}.
  • tests/test_external_readers.py: _run maps bare package names through the pins, and a scanpy oracle test for rank_genes_groups across backends.
  • tests/test_legacy_copies.py, tests/test_storage_root_attrs.py, tests/test_pandas_frame.py: regression tests. AGENTS.md, docs/index.md, docs/developers.md, docs/troubleshooting.md: the version claims and one troubleshooting entry.

How it was tested

  • New tests/test_element_fidelity.py: each source written by anndata (or the committed 0.7.8 stores), copied by convert and subset to h5ad, zarr2 and zarr3, read back by anndata. The --compression copy of a rec-array runs in a subprocess, so a crash fails the test rather than the run.
  • Regression tests per ref in test_storage.py, test_elements.py, test_pandas_frame.py, test_legacy_copies.py, test_storage_root_attrs.py, test_anndata_roundtrip.py. Each docstring names its ref. Run against main, 84 of them fail there; the others are controls (same-backend copies, float64) and F090's test.
  • COLUMNS=80 uv run pytest -m "not integration" --cov=adata --cov-fail-under=90 (after the review fixes, 3f5efa1): 2778 passed, 1 skipped; coverage 95.92%
  • The unit suite at the dependency floors (CI's floors recipe: zarr 3.2.0, numcodecs 0.14.0, anndata 0.13.0), before the review fixes: 2768 passed, 1 skipped
  • ADATA_REQUIRE_VERSION_FIXTURES=1 uv run pytest tests/test_anndata_versions.py -m integration: together with tests/test_concat_versions.py, which shares the releases, after the review fixes: 280 passed, 14 skipped (anndata 0.7 cannot read what adata writes, so its read-backs skip, as does the nullable column it does not write)
  • tests/test_external_readers.py -k rank_genes_groups (scanpy 1.12.5), after the review fixes: 4 passed.
  • uv run pytest tests/test_docs_are_accurate.py: passed, also after merging main (0.17.4) at c6e7c06.

Review

A fresh subagent reviewed the diff against the audit entries and AGENTS.md, reproducing what it reported. Seven findings:

  1. Two gaps in the F041 hardening (security; details withheld until release). Fixed in 3f5efa1: the layout check covers them, with tests.
  2. concat --uns-merge same|unique dropped text rec-arrays whenever an input was h5ad. The value comparison used the record's raw bytes, which for h5py's text are heap pointers. Fixed: records are compared field by field (4 lines in core/concat.py, B3b's file; B3b told). Tested against anndata.concat.
  3. A reference leading out of the element copied was handled two ways. Copied whole, HDF5 brought its target along as a stray ~obj_pointed_by_<n> at the output's root (all of X, in the repro); copied dataset by dataset, it was refused. Now a reference is copied only inside an element HDF5 copies whole, and refused otherwise, on both paths. A target copied ahead of its place in the tree no longer leaves a duplicate root link, which also happened for a 0.7 column named before __categories ("Phase"). The spec pages say so.
  4. F071 did not reach subset._growable_like, which creates its HDF5 datasets directly: recast --dtype bool --layout csr of a dense float X with a non-zero fill still failed. Fixed with a shared storage.fit_fill (2 lines in core/subset.py, B4's file; B4 told).
  5. A record field of variable-length numbers was stringified (h5ad) or failed with zarr's "ambiguous" error. Now copied between h5ad files, and refused by name for Zarr.
  6. A related hardening case outside HDF5. Not in this batch's scope; reported to the coordinator as a follow-up.
  7. Checked and holding: streaming, Store.close, staging, Zarr version and codecs kept, imports; test_performance passes; the new tests fail on main and use anndata as the oracle; the zarr floor's justification reproduces (3.1.5 cannot read a struct dtype 3.4 writes).

Changelog entry

Fixed

  • Hardening for crafted HDF5 inputs (#95). (F041, security.) Details follow once this release is out.
  • scanpy's marker genes cross backends (#95). rank_genes_groups (record arrays in uns) failed every conversion and subset between h5ad and Zarr, and convert --compression of an h5ad holding them crashed the process. They are now copied field by field, as anndata writes them, concat --uns-merge same keeps them (it dropped every text one read from an h5ad), and export dict writes them as JSON.
  • anndata 0.7 files copy whole (#95). An h5ad copy nulled the references of 0.7 dataframes nested in raw, obsm or uns (anndata could not open the output), every copy made an ordered categorical unordered, and every command warned that such a file's root attributes were invalid. CI now reads stores written by anndata 0.7.8.
  • Elements other writers leave behind (#95). Variable-length numbers and object references were turned into text on their way to Zarr; they are now refused there by name, and copied between h5ad files. An attribute holding nothing, or a 2-D array of bytes, no longer fails a conversion to Zarr, and an untagged empty dataspace is no longer tagged null (which anndata 0.8 to 0.11 refuse).
  • Zarr v2 keeps its compression (#95). An uncompressed v2 array came out Blosc-compressed, and so could a compressed one when zarr's deprecated API failed.
  • recast to bool on h5ad (#95). It failed on every float matrix with "no appropriate function for conversion path".
  • float32 exports as written (#95). export dataframe printed 0.1 in a float32 column as 0.10000000149011612; it now writes what pandas writes.
  • pycisTopic tables open in older anndata (#95). A model table's text with a missing value is a categorical, which anndata 0.8 to 0.10 read, not a nullable-string-array, which they refuse.
  • zarr 3.2 or later is required (#95). zarr 3.1 cannot open a v3 store that newer zarr wrote with a record array, as anndata 0.13 does for rank_genes_groups.

🤖 Generated with Claude Code

Every element anndata or scanpy writes survives every copy path, read
back by anndata (and scanpy for rank_genes_groups):

- record arrays copied field by field across backends (F017), and no
  record fill value handed to h5py (F021)
- anndata 0.7 frames nested in raw/obsm/uns keep their references in
  HDF5 copies (F006), legacy `ordered` read from __categories (F072),
  no root-attribute warning for the 0.7 layout (F164)
- attributes: nested bytes, h5py.Empty, references (F130); vlen numbers
  and references not taken for text (F126); float32 at its own
  precision (F125); a carried fill converted with the dtype (F071)
- Zarr v2: compressors via the current API, None kept (F134, F135); an
  untagged empty dataspace not made a null (F136); missing text in
  pickle tables as a categorical (F133)
- hardening for crafted HDF5 inputs (F041, security)
- zarr floor 3.2.0 (lock 3.4.1): 3.1 cannot read a v3 record dtype
  newer zarr writes
- fixtures written by anndata 0.7.8, 0.7.8 in the version matrix, every
  release and oracle pinned exactly (F087); plain-string fixtures under
  pandas 3 (F090); convert and split/concat read back by each release
  (F086); spec and testing docs corrected (F154, F156); dead code and
  duplicated spec constants removed (F163)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 17:10
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-30T17:10:47.434280Z
We couldn’t complete this review. Comment @codex review to try again.
ce8516b PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Test Results (py3.12)

3 130 tests  +115   3 126 ✅ +115   11m 5s ⏱️ + 5m 0s
    1 suites ±  0       4 💤 ±  0 
    1 files   ±  0       0 ❌ ±  0 

Results for commit 4bf0ef0. ± Comparison against base commit ad44381.

♻️ This comment has been updated with latest results.

- the HDF5 layout check follows object references too, and walks into
  linked folders of a convert source (F041, security)
- a reference is copied only inside an element HDF5 copies whole, on
  both copy paths, with no stray root link left behind
- concat compares record arrays field by field, so --uns-merge same
  keeps rank_genes_groups read from an h5ad
- subset's growable datasets get a fill fit for their dtype (F071)
- a record field of variable-length numbers is not taken for text

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

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Test Results (py3.13)

3 130 tests  +115   3 126 ✅ +115   12m 35s ⏱️ + 1m 19s
    1 suites ±  0       4 💤 ±  0 
    1 files   ±  0       0 ❌ ±  0 

Results for commit 4bf0ef0. ± Comparison against base commit ad44381.

♻️ This comment has been updated with latest results.

Claptar and others added 2 commits November 2, 2026 10:21
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Claptar
Claptar merged commit be38de4 into main Nov 2, 2026
11 checks passed
@Claptar Claptar mentioned this pull request Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

B5: Element fidelity, legacy stores and HDF5 safety

2 participants