Fix IndexError in check_table_values_for_dict on empty tables (fixes #712)#715
Open
Leonard013 wants to merge 1 commit into
Open
Fix IndexError in check_table_values_for_dict on empty tables (fixes #712)#715Leonard013 wants to merge 1 commit into
Leonard013 wants to merge 1 commit into
Conversation
An empty DynamicTable/EventsTable can contain columns whose data is zero-length. check_table_values_for_dict evaluated `column.data[0]` in its column-skip condition with no empty-length guard, so any NWB file containing an empty table crashed nwbinspector with IndexError (issue NeurodataWithoutBorders#712, diagnosed by @oruebel). Add `len(column.data) == 0` to the skip condition, ordered so Python's `or` short-circuits before `column.data[0]` is accessed. Only empty columns are skipped; non-empty columns are scanned exactly as before. Add a regression test that an empty column no longer raises; the existing dict-string tests continue to confirm that non-empty columns are still checked (the guard does not over-skip). This change was written with the assistance of Claude (AI). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NX8ygWTRa86kE8k8Yn9bpu
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.
Motivation
check_table_values_for_dictcrashes withIndexError: list index out of rangewhenever it is run on a table that has a column with zero rows. Empty
DynamicTable/EventsTableobjects are legitimate NWB constructs, so nwbinspector currently crasheson any file that contains one.
Root cause (diagnosed by @oruebel in #712), in
src/nwbinspector/checks/_tables.py,the per-column skip condition:
column.data[0]is evaluated with no empty-length guard, so a zero-length columnraises
IndexError.What
Add a
len(column.data) == 0guard to the skip condition, placed before thecolumn.data[0]access so Python'sorshort-circuits on empty columns:The multi-line form matches the sibling check
check_col_not_nandirectly below in thesame file.
black --line-length 120andruffboth pass.Why this does not over-skip non-empty columns
orevaluates left-to-right and stops at the first truthy operand:len(column.data) == 0isTrue): short-circuits andcontinues,so
column.data[0]is never evaluated — the crash is avoided.len(column.data) == 0isFalse): evaluation falls through tonot isinstance(column.data[0], str), exactly as before. The new term isFalseforevery non-empty column, so it changes nothing about which non-empty columns get
scanned, and the dict-detection logic is untouched.
Only genuinely empty columns are skipped.
Tests
Added
test_check_table_values_for_dict_empty_columntotests/unit_tests/test_tables.py:IndexErrorat the diagnosed line.The existing
test_check_table_values_for_dict_*_failtests continue to pass, confirminga non-empty column containing a dict-string still triggers the check (no over-skip).
pytest tests/unit_tests/test_tables.py→ 48 passed; broad slice → 340 passed, 7 skipped.Follow-up (out of scope for #712)
Scoped to
check_table_values_for_dictper @oruebel's diagnosis. Three sibling checks inthe same file carry the same latent empty-table
IndexErrorand would still crash on afull run over an empty table — worth a separate follow-up PR if desired:
check_column_binary_capability—column.data[0]check_col_not_nan—column[0]check_table_time_columns_are_not_negative—table[column_name][0]Happy to open that follow-up.
CHANGELOG
Added an entry under
# v0.7.3 (Upcoming)→### Fixes.