Skip to content

Extract write field validation from Record - #893

Merged
tompollard merged 2 commits into
mainfrom
refactor/explicit-write-field-validation
Dec 3, 2026
Merged

tompollard merged 2 commits into
mainfrom
refactor/explicit-write-field-validation

Conversation

@tompollard

@tompollard tompollard commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Move the field validation rules out of Record.check_field into one private function that takes an explicit field value and a lookup for related fields. Record.check_field remains the public wrapper and validates its current attributes. _WriteModel validates its own field values without borrowing the Record method.

This is the next small step toward #866. It does not intentionally change validation, the public API, or written files. The in-place ADC branch noted in the #892 review is independent of this change.

Checks

  • 2,290 offline tests passed; 48 skipped and 23 network tests deselected.
  • Writer byte fixtures and header parity tests passed, including invalid-header error comparisons (412 passed, 44 expected skips in the focused run).
  • A new test asserts that _WriteModel owns a distinct validator; it fails on pre-change main and passes here. Another checks the public method still validates current Record attributes.
  • The docs build passed with warnings treated as errors. Ruff check and format check passed.

No changelog entry because this only extracts internal validation logic.

@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.

The extraction keeps every validation rule, and it can merge. One of the new tests can't fail and should be changed. The validation, fixture and parity tests pass (412 passed, 44 expected skips), as do the offline suite (2,290 passed, 48 skipped), Ruff and all 19 CI checks.

What I checked

  • The body is the old check_field with explicit inputs. self.<field> became item, and the two cross-field reads (file_name and fmt for the file-name rules) go through lookup. Record.check_field passes getattr(self, name), or header_fields when given. The model passes its own vars(self) or header_fields.
  • The public Record.check_field is unchanged. This is a ~200-line rule block, so I compared it with main (b6ed4d1) directly. I called check_field for every field in ALLOWED_TYPES with 52 values, under four fmt/file_name/n_sig contexts, with required_channels set to "all", [0] and []. The values were: None; ints and floats of each sign; NumPy scalars; booleans; empty, whitespace-padded, invalid and valid names and formats; file names including ~; dates, times and datetimes; lists with None gaps, duplicates and mixed types; and uniform and expanded arrays of the wrong dimension or dtype. All 19,344 outcomes match: 1,912 pass, and the rest raise the same exception type and message.

test_write_model_validates_without_record_check_field passes on main too

Before this PR, _WriteModel bound the method in its class body (check_field = Record.check_field). Monkeypatching wfdb.Record.check_field later replaces the attribute on Record but not the function object the model already holds. So the model never reaches the patched function, either before or after this change. I copied the new test file into a main checkout, and both tests pass there. It's the same problem as the calibration test in #887.

To test what the docstring describes, assert that the model defines its own method: "check_field" in vars(_WriteModel) and _WriteModel.check_field is not Record.check_field. That fails on main, where the two are the same function object, and passes here. Nothing depends on it at runtime, but since #866 is about removing these borrowed methods, a guard that can't fail doesn't protect anything.

@tompollard

Copy link
Copy Markdown
Member Author

Good catch. I replaced the monkeypatch test in 04a9577 with a guard that _WriteModel defines its own check_field method and that it is a different function from Record.check_field. I ran that test against pre-change main, where it fails, and against this PR, where it passes. The focused writer fixture and header parity tests still pass (412 passed, 44 expected skips). I also corrected the PR description.

@tompollard
tompollard merged commit dbe4195 into main Dec 3, 2026
19 checks passed
@tompollard
tompollard deleted the refactor/explicit-write-field-validation branch December 3, 2026 17:54
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