feat: append mode for MetricWriter.open#54
Draft
msto wants to merge 9 commits into
Draft
Conversation
Contributor
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
msto
force-pushed
the
feat/metric-writer-append-mode
branch
from
June 9, 2026 12:11
e63ef72 to
4bd59b7
Compare
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add mode="a" to open(): on a missing/empty file the header is written (append-or-create); on a non-empty file the existing header is validated against the metric class's fields and skipped. mode defaults to "w", so existing callers are unaffected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Lift the read/validate/decide logic into _append_writes_header and collapse open()'s nested conditional into a single branch on mode. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Replace the hand-rolled `readline()` + one-shot `csv.reader` + empty-string check in `_read_existing_header` with `csv.DictReader.fieldnames` — the symmetric counterpart to the `DictWriter` the writer emits with. It parses the first row identically to how it was written and natively returns `None` for an empty file, so the header read needs no manual empty handling. Also note the `utf-8` (writer) vs `utf-8-sig` (reader) default asymmetry in the `open()` docstring. No behavior change. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
`MetricWriter.open(mode="a")` promises append-or-create: an empty file gets a fresh header. For a zero-byte `.bz2`/`.xz` file this raised EOFError instead — the header-validation read handed the empty file to xopen, and the bz2/xz decompressors reject a truncated stream (plaintext and gzip tolerate it). Check the file size before decompressing: a zero-byte file of any format has no header, so we skip the read and write one. Genuine corruption in a non-empty file still surfaces rather than being silently swallowed. Cover append-to-empty across all four formats (the previously-broken case), and pin the documented trailing-newline concatenation behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Replace the try/except stat in _read_existing_header with an explicit exists()/st_size check, narrow its return type to list[str] | None so the header comparison is list-to-list, and rename _append_writes_header to _append_needs_header — it reports whether a header is needed rather than writing one. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
msto
force-pushed
the
feat/metric-writer-append-mode
branch
from
June 24, 2026 23:56
9339458 to
dfbe49f
Compare
`MetricWriter.open(path, mode="a")` previously raised unless the first row of an existing file exactly matched the metric's header. It now also accepts a headerless data file: if the first row validates as a record of the metric class (via `model_validate`, the same path `MetricReader` uses to parse a row), rows are appended without injecting a header into the middle of the file. A `ValueError` is raised only when the first row is neither the expected header nor a valid record. A row with the wrong number of columns cannot be a record, so it is rejected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #47
This PR adds a
modeargument toMetricWriter.open()so callers can append metric rows to an existing delimited file without duplicating the header (e.g. resumable steps, sharded writers, incremental logging, any other append-or-create context).open(...)callmode="w"(default)mode="a"mode="a"mode="a"mode="a"ValueErroron entry