Skip to content

fix(cli): --zero breaks open-ended column ranges - #1354

Open
VXNCXNX wants to merge 1 commit into
wireservice:masterfrom
VXNCXNX:fix/zero-open-ended-range
Open

fix(cli): --zero breaks open-ended column ranges#1354
VXNCXNX wants to merge 1 commit into
wireservice:masterfrom
VXNCXNX:fix/zero-open-ended-range

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 15, 2026

Copy link
Copy Markdown

What's broken

--zero breaks open-ended column ranges.

$ printf 'a,b,c\n1,2,3\n' > abc.csv

$ csvcut -c 1: abc.csv
a,b,c

$ csvcut --zero -c 0: abc.csv
ColumnIdentifierError: Column 3 is invalid. The last column is 'c' at index 2.

Same intent, expressed in the offset the flag asks for, and it errors. The other end is wrong too, silently:

$ csvcut --zero -c :1 abc.csv
b                      <- should be a,b

-C/--not-columns shares the code and fails the same way:

$ csvcut --zero -C 1: abc.csv
ColumnIdentifierError: Column 3 is invalid...

The fix

In _resolve_column_identifier, the implicit ends of an open-ended range were hardcoded for 1-based identifiers:

a = int(a) if a else 1
b = int(b) + 1 if b else len(column_names) + 1

Both bounds live in identifier space, so both depend on column_offset. Under --zero the upper bound overshot by one and tripped the validity guard, and the lower bound started at the second column.

All six shapes after the change:

-c 1:          a,b,c        --zero -c 0:    a,b,c
-c :2          a,b          --zero -c :1    a,b
-c 2:3         b,c          --zero -c 1:2   b,c
-C 2:          a            --zero -C 1:    a

Not undoing the recent exclusion fix

The Unreleased changelog has fix: -C/--not-columns now excludes the last column of an open-ended range (e.g. 2-), which is what the + 1 on the upper bound is for. Under the default column_offset=1, len + column_offset is exactly the old len + 1, so that behaviour is untouched and only the column_offset=0 case changes. Its guard tests, test_exclude_open_ended_range and test_exclude_ignores_unknown_names_but_reports_invalid_ranges, still pass, and -C 2: still yields a.

Verification

test_range_notation_open_ended tested only column_offset=1, while its sibling test_range_notation covers both offsets. The missing offset-0 rows are added, for :3, 1,4:, 0:, and both offsets of the open-ended exclusion form.

Reverting either bound alone fails the test, so both halves are independently covered.

Suite unchanged: 351 passed under en_US.UTF-8 before and after. Under de_DE.UTF-8 it is 4 failed before and after, all pre-existing and in other utilities.

Changelog entry added under Unreleased.

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