Skip to content

Make chop() a no-op for degenerate ranges - #165

Open
36taransingh5-dotcom wants to merge 2 commits into
chaimleib:masterfrom
36taransingh5-dotcom:fix-chop-degenerate-range
Open

36taransingh5-dotcom wants to merge 2 commits into
chaimleib:masterfrom
36taransingh5-dotcom:fix-chop-degenerate-range

Conversation

@36taransingh5-dotcom

Copy link
Copy Markdown

Summary

chop(begin, end) with begin >= end used to mutate the tree instead of doing nothing:

  • chop(x, x) split every interval containing x in two (acting like slice(x)), so the original intervals silently disappeared and could no longer be removed.
  • chop(9, 5) turned (0, 10) into the overlapping pieces (0, 9) and (5, 10), double-covering the middle and inflating len().

Nothing overlaps an empty range, so there is nothing to trim and nothing enveloped to remove. This PR returns early when begin >= end, matching the docstring ("Like remove_envelop(), but trims back Intervals hanging into the chopped area so that nothing overlaps") and Python slice semantics for empty ranges.

Reproducer

from intervaltree import IntervalTree, Interval

t = IntervalTree([Interval(0, 10, 'x')])
t.chop(9, 5)
print(sorted(t))  # before: [(0, 9, 'x'), (5, 10, 'x')]; after: [(0, 10, 'x')]

Tests

  • New test_chop_degenerate_range covers chop(5, 5), chop(9, 5), and the empty tree.
  • Full suite passes locally: 103 passed (pytest, Python 3.10).

Developed with AI assistance.

… >= end used to split intervals containing the range endpoints (chop(x, x) acted like slice(x)) or, for begin > end, produce overlapping duplicate pieces. Nothing overlaps an empty range, so nothing should be trimmed: return early instead.

Add a guard clause to prevent processing invalid intervals.
Add test for chopping degenerate ranges in IntervalTree.
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