auto-delimiter detection: fix IndexError on short files, raise a typed error#606
Merged
Conversation
…d error
find_delimiter_and_start split its sample window into a header part and a
data part, but computed the data-part start from the nominal window size
(checking_length_whole - checking_length_header) rather than the number of
lines actually read. A file shorter than that offset left the data slice
empty, and _find_separator died on list({})[0] with a bare
"IndexError: list index out of range" - naming neither the file nor the cause.
With the real callers (AutoLoader: header=100, whole=200) the split sat at
line 100, so any file under 102 lines failed: neware_txt_zero with 100 data
rows crashed, 150 loaded. A short vendor file - a truncated run, a quick
sanity export, a small fixture - is perfectly normal.
_find_separator now clamps the header window to the actual line count
(min(header, n-1)), always leaving at least one data row, and drops the
possibly-truncated last line only when a data row can still be spared. For any
file that already worked (n >= 102 at header=100) the inspected window is
byte-identical, so existing files are unaffected. A header line plus a single
data row is now enough. Genuine failures - empty/blank-only files, no
consistent delimiter, an unlocatable header - raise a typed LoaderError that
names the file, replacing both the IndexError and the old untyped IOError.
Regression tests cover 1/2/5/20/100/101/150 data rows across four delimiters,
preamble handling, the minimal two-line file, and both error paths. This also
removes the reason the #560 synthetic-parity fixtures had to be 300 rows.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
jepegit
enabled auto-merge (squash)
July 21, 2026 07:20
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.
What
find_delimiter_and_start(cellpy/readers/instruments/base.py) failed to load short files with a bareIndexError: list index out of range— an error naming neither the file nor the cause. A short vendor file (a truncated run, a quick sanity export, a small fixture) is perfectly normal.Root cause
The function split its sample window into a header part and a data part, but computed the data-part start from the nominal window size rather than the lines actually read:
When the file was shorter than that offset,
last_partwas empty and_find_separatorranlist({})[0]→IndexError.Threshold (bisected, not assumed)
The real callers (
AutoLoader._auto_formatter, and the same incustom.py) usechecking_length_header=100, checking_length_whole=200, putting the split at line 100 — so any file under 102 lines failed. Forneware_txt_zero(1 header + D data rows): D=100 → 101 lines crashes, D=101 → 102 lines works, which is exactly the reported "100 fails / 150 works" bracket. (The module defaultheader=30puts the boundary at 171 rows; both confirmed by bisection.)Fix
_find_separatorclamps the header window to the actual line count —min(checking_length_header, n-1)— always leaving ≥1 data row, and drops the possibly-truncated last line only when a data row can still be spared. For any file that already worked (n ≥ 102 at header=100), the inspected windowlines[100:-1]is byte-identical → zero behavior change on existing files.LoaderErrornaming the file — empty/blank-only files, no consistent delimiter, or an unlocatable header — replacing both theIndexErrorand the old untypedIOError.Verification
neware_txtloader: D = 1, 2, 5, 20, 100, 300 all load (previously 1–100 crashed).tests/test_find_delimiter_and_start.py— 35 tests: sizes 1/2/5/20/100/101/150 across four delimiters, preamble handling, the minimal 2-line file, and both typed-error paths.neware_uio.csvparity tests and theloader_customgolden (both exercise auto-detection) stay green.Context
Found while extending the #560 loader-port parity coverage (PR #602); split out to keep that branch scoped. This also removes the reason the synthetic-parity fixtures had to be 300 rows.
🤖 Generated with Claude Code