Repository navigation
Extract write field validation from Record - #893
Conversation
tompollard
left a comment
There was a problem hiding this comment.
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_fieldwith explicit inputs.self.<field>becameitem, and the two cross-field reads (file_nameandfmtfor the file-name rules) go throughlookup.Record.check_fieldpassesgetattr(self, name), orheader_fieldswhen given. The model passes its ownvars(self)orheader_fields. - The public
Record.check_fieldis unchanged. This is a ~200-line rule block, so I compared it withmain(b6ed4d1) directly. I calledcheck_fieldfor every field inALLOWED_TYPESwith 52 values, under fourfmt/file_name/n_sigcontexts, withrequired_channelsset 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 withNonegaps, 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.
|
Good catch. I replaced the monkeypatch test in 04a9577 with a guard that |
Summary
Move the field validation rules out of
Record.check_fieldinto one private function that takes an explicit field value and a lookup for related fields.Record.check_fieldremains the public wrapper and validates its current attributes._WriteModelvalidates its own field values without borrowing theRecordmethod.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
_WriteModelowns a distinct validator; it fails on pre-change main and passes here. Another checks the public method still validates currentRecordattributes.No changelog entry because this only extracts internal validation logic.