Skip to content

Share writer checks and remove model mixins - #897

Merged
tompollard merged 2 commits into
mainfrom
refactor/finish-explicit-writer
Dec 3, 2026
Merged

tompollard merged 2 commits into
mainfrom
refactor/finish-explicit-writer

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

Consolidate the duplicate header and signal validation left by #895 and #896. Record._write_checked_header now calls the explicit header function, its snapshot cohesion check calls the shared helper, and SignalMixin.check_sig_cohesion uses the explicit shape and range checker while keeping its direct-call checksum and initial-value checks. _WriteModel no longer inherits BaseRecord, HeaderMixin, or SignalMixin.

This is the first #866 cleanup step. The _legacy_* paths and parity test remain independent for this review. Removing them needs a separate checklist that maps each parity case to a fixed byte fixture or test. The model still projects channel fields to lists while preparing a write; that remaining metadata representation work is separate from inheritance removal.

Checks

  • 2,294 offline tests passed; 48 skipped and 23 network tests deselected.
  • Writer byte fixtures, old-versus-new parity, explicit header and .dat tests, and warning caller tests passed (417 passed, 44 expected skips in the focused run).
  • Direct Record.check_sig_cohesion calls matched pre-change main in 2,000 generated cases, including errors, printed diagnostics, and in-place samples-per-frame defaults.
  • Docs built with warnings treated as errors; Ruff and git diff --check passed.

No changelog entry because this changes only internal writer structure.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removing the mixins is the milestone #866 was aiming for. The consolidation keeps every write output unchanged, but it changes two public Record methods when n_sig is unset. Both are small fixes. The focused header, .dat, fixture, parity and warning tests pass (418 passed, 44 expected skips), as do the offline suite (2,295 passed, 48 skipped) and Ruff.

What I checked, against main (69fbc95)

  • _WriteModel no longer inherits anything. Its MRO is _WriteModel, object.

  • The modern writers are unchanged. I compared bytes and errors across versions:

    • every readable sample header through write_record and SignalRead.write;
    • 3,000 random sparse records;
    • 15 invalid header fields;
    • 480 flattened multi-segment writes across all four writers;
    • my 1,464-write SignalRead.write mutation check.

    Every outcome is identical.

  • The public Record writers are unchanged. Since Record._write_checked_header now goes through the shared function, I also compared:

    • rdrecord(..., m2s=True).wrsamp() on every sample header, both kinds and both layouts;
    • rdheader(...).wrheader() on every sample header;
    • wrheader() and wrsamp() on 3,000 malformed Records: list fields that are short, long or tuples, n_sig off by one or unset, mixed file names and offsets, out-of-range samples, and missing sig_len.

    All outcomes match main, including errors, files left behind, anything printed, and the record's state afterwards.

  • Performance is unchanged. All five writer and kind combinations on 100skew are within noise of main, with identical peak memory.

To fix: direct check_sig_cohesion and check_field_cohesion calls with n_sig=None

On the same 3,000 malformed records, I also called the two public cohesion methods directly. All 596 differences come from records whose n_sig is unset:

  • check_sig_cohesion (298 calls). main raised the existing ValueError, such as sig_len and n_sig do not match shape of d_signal or n_sig does not match the length of e_d_signal, or a TypeError. Now every one raises AttributeError: 'NoneType' object has no attribute 'n_channels'. _check_dat_values treats n_sig=None as "not given" and falls back to signal_snapshot.n_channels, but a direct Record call passes no snapshot. In 61 expanded calls, main had also filled samps_per_frame in place before raising, and that no longer happens.

    A minimal fix is to fall back only when there is a snapshot: if n_sig is None and signal_snapshot is not None:. Then None reaches the same comparisons as before and raises the same errors.

  • check_field_cohesion (298 calls). On main, if n_channels > 0 raised TypeError: '>' not supported between instances of 'NoneType' and 'int'. Now if self.n_sig == 0: return lets None through to the length check, which raises ValueError: The length of field: fmt must match field n_sig. (or, in 6 calls, a different TypeError). Keeping the old > 0 guard restores the previous behaviour.

These only affect direct calls on incomplete records, and the new check_field_cohesion message is arguably clearer. But this PR is meant to be behaviour-neutral, and the AttributeError is a worse message than before. Please restore both, and add a test that calls each method on a Record without n_sig, so the behaviour stays as intended.

@tompollard

Copy link
Copy Markdown
Member Author

Good catch. I fixed both cases in fe22e5d. _check_dat_values now falls back to the snapshot count only when a snapshot exists, so direct Record.check_sig_cohesion calls with n_sig=None retain their old error and, for expanded values, still fill samps_per_frame before raising. check_field_cohesion again evaluates n_sig > 0 before touching the signal fields, restoring its original TypeError.

I added tests for uniform and expanded signal checks and the header check. All three pass on pre-change main and on this branch. The focused writer and parity tests pass (413 passed, 44 expected skips), as does the full offline suite (2,298 passed, 48 skipped).

@tompollard
tompollard merged commit 76468a3 into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/finish-explicit-writer branch December 3, 2026 18:42
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.

1 participant