Skip to content

B3: Names, images and pipes - #155

Open
Claptar wants to merge 7 commits into
mainfrom
fix/b3
Open

Claptar wants to merge 7 commits into
mainfrom
fix/b3

Conversation

@Claptar

@Claptar Claptar commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Part of #149 (wave 1, for 0.17.11). Closes #137.

Fixes F181, F182, F183, F184, F185 and F186. Each has a regression test whose docstring starts with its ref and says what used to go wrong. Every one of those tests fails on main, except a control showing that PNG, JPEG and TIFF still import, and two guards on the design: the CSV is still read in two passes, and a store writer whose messages lose their reader still exits 1.

What changes

  • F181: imports refuse before they copy. import dataframe, import dict and import image now go through the same pre-check as array and sparse. It runs after the output check and before staging copies the source.
    • The target is checked, and so is the input's shape against the source where the path sets one.
    • A CSV: import_dataframe is split into scan_dataframe and write_dataframe, with no change in behaviour or messages. The pre-check makes the CSV's first pass and the write reuses it, so the CSV is still read in two passes. Its header refusals come before the copy too.
    • With the copy now between a CSV's two passes, a CSV that changes in between is refused: the scan records the file's size, modification time and inode. Otherwise a value the scan never saw would be written as a gap.
    • A JSON file: import_json is split into plan_json and write_json the same way. The pre-check reads and plans the file, and the write reuses the plan.
    • An image: its shape is read from its header.
    • Copied in.h5ad → out.h5ad is printed once the import has succeeded.
    • _source_root still runs 0.17.9's check_zarr_contents.
  • F182: image inputs. Images are opened with Image.open(path, formats=[...]), limited to the formats the docs name: PNG, JPEG and TIFF for import image (formats/image.open_image), and PNG for Visium's tissue images. Any other file is refused with its name.
    • Visium checks each image's header while planning, so the refusal comes before the output is staged. The message points at --no-images.
    • Xenium reads no images, so nothing changed there.
  • F183, F186: control characters in names. literal() shows C0, DEL and C1 control characters as visible escapes (\x1b, \n, as Python spells them), so every message, tree and status line is covered. ls --plain writes its paths through the same escaping (visible()), so a name holding a newline stays on one line.
  • F184: closed pipes.
    • A BrokenPipeError reaching _fail ends the run quietly with exit 0, with stdout pointed at /dev/null, as export already did. That covers ls, view, export image, --version and any later command.
    • stdout's Rich console passes the error on, instead of exiting 1 itself (Rich's on_broken_pipe) and staying quiet for the rest of the process.
    • A message whose reader has gone (2>&1 | head) is dropped, and the command finishes, so its exit code says whether it wrote its output. Rich used to end the run at the first such message with exit 1, whether that came before the store was written or after it was in place. It also made the console quiet for the rest of the process. A test covers both outcomes.
    • main flushes stdout before the run ends, so short output still waiting in the buffer cannot make Python exit with 120.
    • A run started with stdout closed (>&-) is fine. That includes export ... -o file, which already failed on main: the review found that formats/common.stdout_reader_may_stop flushed a stdout that did not exist, and staging threw the finished file away.
  • F185: warnings. main shows Python warnings as one Warning: line through the console, escaped like any other message, and puts warnings.showwarning back afterwards.

Files beyond the issue's Owns line

Agreed with the coordinating session, which is adding them to #137:

  • formats/dataframe.py and formats/json_data.py: no batch owns either. Refusing what a CSV or JSON file holds before the copy needs the file read first. Without the split, the import would have read it once more after the copy. formats/table.py (B10's) and formats/validate.py (B6's) are called as they are.
  • conversion/visium.py, in _images() only: so that a non-PNG tissue image is refused while planning, as AGENTS.md's "validate before the output exists" asks, rather than during the write.
  • formats/common.py: two lines. The export helper flushed a stdout that a run started with >&- does not have. No batch owns the file.
  • AGENTS.md and docs/developers.md: only the lines on B3's rules. These are _fail on a closed pipe, Warning: lines, visible, and untrusted images through open_image. They are in the architecture and rules sections, outside the Releases sections B5 is editing.

User docs: commands.md (the intro, ls -1, import, import image), the pipelines guide's exit codes section, the Visium page, troubleshooting (two new refusals, and the warnings section), and the testing map row.

Changelog entry (for 0.17.11, ### Fixed)

  • Every import refuses before it copies the store
    (#137). import dataframe, import dict and import image refused a target the layout
    forbids, a CSV of the wrong length, a JSON file that cannot be written
    there or an image of the wrong height only after copying the whole store,
    which takes as long, and as much room, as the store is large. Now every
    subcommand checks its target and its input first, as array and sparse
    already did; a CSV's first pass and a JSON file's parse simply come before
    the copy, so neither is read more than before. Copied in.h5ad → out.h5ad
    is printed only once the import has succeeded.
  • Images are read only as the formats the docs name
    (#137). Pillow picked
    a decoder for an image from among all of its plugins, whatever the file's
    name. import image now reads PNG, JPEG and TIFF, and Visium's tissue
    images are read as PNG; a file of any other format is refused, before
    anything is written.
  • Control characters in names print as escapes
    (#137). A store's
    member names, labels and attribute values reached the terminal as they
    were. A control character in one now prints as an escape such as \x1b
    or \n, in every message and in ls. ls -1 writes such a name on one
    line, where a newline used to make it two paths.
  • A reader that stops early ends any command quietly
    (#137). adata ls data.h5ad -1 | head -1 and adata export image ... | head printed
    Error: Broken pipe and exited 1, which fails a pipefail script. ls
    without -1, view and --version exited 1 as well, without a word.
    Only the other exports ended quietly (B2b: Table, dataframe, JSON and array codecs #91). Every command now ends
    quietly, with exit code 0.
  • Python warnings print as Warning: lines
    (#137). A warning
    from the storage layer or a library printed in Python's own format, with
    the tool's source file, line number and line of code. Each is now one
    Warning: line on stderr, like the CLI's own.

Repros

Each of the audit's repro scripts for these refs prints OK: on this branch; each printed DEFECT: on main.

Review

An independent review of the first commit found:

  • a crash after success with stdout closed;
  • the results console staying quiet for later embedded runs;
  • --version on a closed pipe;
  • a test that would have passed without part of the fix;
  • the JSON and Visium checks coming after the copy and the staging.

All are fixed. A second review of the commits after it found:

  • the CSV's two passes are now a copy apart;
  • the docs' wrong account of a store writer whose messages lose their reader;
  • the cases below.

The first two are fixed. So is the review's finding that exports to a file failed when started with stdout closed, which was already broken on main.

Tests

  • uv run pytest -m "not integration": 4184 passed, 9 skipped (on macOS, Python 3.12)
  • uv run pytest -m perf: 210 passed

Left as they are

  • A target inside an existing dataset (import dict in.h5ad uns/k/sub where uns/k is an array) still fails after the copy, with the library's own message. So does a .npy the import cannot read at all (Python objects, a file cut short), whose header the pre-check leaves to the import. Both are as on main. formats/array.py is B6's.

  • --help on a closed pipe still exits 1. It goes through typer's own Rich console, and Rich ends the process itself. In practice the help fits in the pipe's buffer, so --help | head exits 0.

  • ls -1 escapes control characters but leaves a backslash as it is. A name holding a real newline and one holding a backslash and n therefore print alike. Escaping backslashes would change every listed path that holds one. A raw -0 mode would put a hostile name's escape sequences on the terminal of anyone who runs it without a pipe.

  • formats/common.stdout_reader_may_stop, which each export still uses, now does what _fail does. It is not B3's file; it could go in a later cleanup.

🤖 Generated with Claude Code

Claptar and others added 5 commits December 4, 2026 22:25
- Every import subcommand checks its target, and its input's shape
  where the path sets one, before the source is copied: dataframe,
  dict and image refused a forbidden target, a CSV of the wrong
  length or an image of the wrong height only after a copy of the
  whole store. The copy is reported once the import has succeeded
  (F181).
- Image inputs are decoded only as the formats the docs name: PNG,
  JPEG and TIFF for import image, PNG for Visium's tissue images
  (F182).
- Control characters in names print as visible escapes, in every
  message and in ls --plain, which now writes one path a line
  (F183, F186).
- A reader that stops early (| head) ends any command quietly, with
  exit code 0, ls and view included (F184).
- Python warnings print as one Warning: line through the console
  (F185).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
import dataframe's pre-check read the CSV once more to count its rows,
on top of the import's own two passes. import_dataframe is split into
scan_dataframe (the target and the first pass, with every refusal the
file alone can show) and write_dataframe (the length against the store,
then the second pass), with no change in behaviour or messages. The
pre-check makes the first pass and the write reuses it, so a CSV is
read twice in all, and its header refusals also come before the copy.
formats/dataframe.py joins B3's files for this.

A closed pipe on a command's results now always ends it with exit 0:
every message goes through Rich's console, which itself ends a run
with exit 1 when stderr's reader has gone, so the stderr probe in
_reader_stopped only turned `ls -1 2>&1 | head` into exit 1. A test
shows that a store writer with no reader for its messages stops with
exit 1 and writes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A run started with stdout closed (`>&-`) has no sys.stdout; flushing
  it before the run ended raised AttributeError after the work was
  done. _flush_stdout and _silence now allow for it.
- The results console no longer makes itself quiet for the rest of the
  process on a closed pipe: it drops what could not go out and passes
  the error on, so a caller embedding main() can run again.
- --version, printed outside any command, ends quietly too.
- view through a real pipe replaces a test that raised the error
  itself and would have passed without the console's fix.
- The docs say which reader's stopping ends a command with exit 0, and
  that a store writer whose messages lose their reader stops with exit 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- import_json is split into plan_json (the target, the file read and
  planned whole, every refusal the file alone can show) and write_json,
  with no change in behaviour or messages. The pre-check plans the JSON
  before the store is copied, and the write reuses the plan: a JSON
  array for uns, a file of null, a key no element can be named and a
  file cut short were refused only after the copy.
- Visium's _images() opens each tissue image's header as PNG while
  planning (conversion/spatial.open_tissue_image, through
  formats.image.open_image), so a non-PNG is refused before the output
  is staged, not part way through writing it.
- AGENTS.md and docs/developers.md: B3's rules (a closed pipe in _fail,
  Python warnings as Warning: lines, visible for text written without
  Rich, untrusted images through open_image).

formats/json_data.py, conversion/visium.py (_images only), AGENTS.md and
docs/developers.md join B3's files for this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The CSV's first pass now comes a whole copy before its second, so a
  value the scan never saw (a new category) would be written as a gap,
  with exit 0. The scan records the file's size, modification time and
  inode, and the write refuses the file if they changed, before and
  after its pass.
- A message whose reader has gone (`2>&1 | head`) is dropped and the
  command finishes, so its exit code says whether it wrote its output.
  Rich ended the run at the first such message, with exit 1, whether
  that came before the store was written or after it was in place, and
  made the console quiet for the rest of the process.
- The docs say so, and that the import's target is checked against the
  layout before the copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings December 6, 2026 14:53

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. Try requesting a review at a lower effort level.

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
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T14:55:17.647495Z 8ef61c6 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.

Claptar and others added 2 commits December 6, 2026 14:56
`adata export dataframe in.h5ad obs -o obs.csv >&-` ended with
`'NoneType' object has no attribute 'flush'` and exit 1, and staging
threw the finished file away; `export dict` and `export array` did the
same. Started with stdout closed, Python has no sys.stdout, and
stdout_reader_may_stop flushed it even for a file. It now skips the
flush when there is none, and _silence_stdout allows for it. Found by
review; already broken on main. formats/common.py joins B3's files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
831bd24 rewrote test_a_run_with_stdout_closed_still_succeeds and, with
it, deleted _messages_unread, which the next test uses: that test
failed with NameError on the pushed commit.

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

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Test Results (py3.13)

3 986 tests   3 973 ✅  10m 34s ⏱️
    1 suites     13 💤
    1 files        0 ❌

Results for commit f8264d5.

@github-actions

github-actions Bot commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Test Results (py3.12)

3 986 tests   3 973 ✅  12m 22s ⏱️
    1 suites     13 💤
    1 files        0 ❌

Results for commit f8264d5.

This branch has not been deployed

No deployments
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.

B3: Names, images and pipes

2 participants