Skip to content

Commit

Permalink
Fixed type check for range type contains
Browse files Browse the repository at this point in the history
  • Loading branch information
runfalk committed Jun 8, 2017
1 parent 81f0fb7 commit a74da15
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
1 change: 1 addition & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Released on <unreleased>
- Fixed issue with :meth:`~spans.types.Range.contains` for scalars on unbounded
ranges
- Fixed type check for :meth:`~spans.types.Range.right_of`
- Fixed type check for :meth:`~spans.settypes.RangeSet.contains`
- Fixed type check for :meth:`~spans.settypes.RangeSet.union`
- Fixed type check for :meth:`~spans.settypes.RangeSet.intersection`
- Fixed type check for :meth:`~spans.settypes.RangeSet.difference`
Expand Down
23 changes: 14 additions & 9 deletions spans/settypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,11 @@ def is_valid_rangeset(cls, obj):

@classmethod
def is_valid_range(cls, obj):
return isinstance(obj, cls.type)
return cls.type.is_valid_range(obj)

@classmethod
def is_valid_scalar(cls, obj):
return cls.type.is_valid_scalar(obj)

def _test_rangeset_type(self, item):
if not self.is_valid_rangeset(item):
Expand Down Expand Up @@ -321,16 +325,17 @@ def contains(self, item):
.. versionadded:: 0.2.0
"""

# All range sets contain the empty range. However, we must verify the
# type of what is being passed as well to make sure we indeed got an
# empty set of the correct type.
if not item and self.is_valid_range(item):
# Verify the type here since contains does not validate the type unless
# there are items in self._list
if not self.is_valid_range(item) and not self.is_valid_scalar(item):
msg = "Unsupported item type provided '{}'"
raise ValueError(msg.format(item.__class__.__name__))

# All range sets contain the empty range
if not item:
return True

for r in self._list:
if r.contains(item) is True:
return True
return False
return any(r.contains(item) for r in self._list)

def add(self, item):
"""
Expand Down
10 changes: 9 additions & 1 deletion tests/test_rangeset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pickle
import pytest

from spans import floatrange, intrange, intrangeset
from spans import floatrange, floatrangeset, intrange, intrangeset


def test_empty():
Expand Down Expand Up @@ -60,6 +60,14 @@ def test_contains_empty(rset):
assert rset.contains(intrange.empty())


def test_contains_type_check():
with pytest.raises(ValueError):
intrangeset([]).contains(1.0)

with pytest.raises(ValueError):
intrangeset([]).contains(floatrangeset([]))


def test_add():
rset = intrangeset([intrange(1, 15)])
rset.add(intrange(5, 15))
Expand Down

0 comments on commit a74da15

Please sign in to comment.