from typing import Generic
from typing import Optional
from typing import TypeVar
+from typing import Union
from ... import types as sqltypes
from ...util import py310
def __bool__(self) -> bool:
return self.empty
- def contains_value(self, value: _T) -> bool:
+ def _contains_value(self, value: _T) -> bool:
"Check whether this range contains the given `value`."
if self.empty:
else value <= self.upper
)
- def issubset(self, other) -> bool:
+ def _contained_by(self, other: Range) -> bool:
"Determine whether this range is a contained by `other`."
# Any range contains the empty one
upper_side = other.upper == self.upper
return upper_side
- def issuperset(self, other) -> bool:
- "Determine whether this range contains `other`."
+ def contains(self, value: Union[_T, Range]) -> bool:
+ "Determine whether this range contains `value`."
- return other.issubset(self)
+ if isinstance(value, Range):
+ return value._contained_by(self)
+ else:
+ return self._contains_value(value)
+
+ def contained_by(self, other: Range) -> bool:
+ "Determine whether this range is a contained by `other`."
+
+ return self._contained_by_range(other)
class AbstractRange(sqltypes.TypeEngine):
(Range(1, 4, bounds="()"), "(1,4)"),
argnames="r,rrepr",
)
- def test_range_contains_value(self, connection, r, rrepr, v):
+ def test_contains_value(self, connection, r, rrepr, v):
q = text(f"select {v} <@ '{rrepr}'::int4range")
- eq_(r.contains_value(v), connection.scalar(q))
+ eq_(r.contains(v), connection.scalar(q))
@testing.combinations(
(Range(empty=True), "empty"),
- (Range(1, 2, bounds="()"), "(1,2)"),
+ (Range(1, 2, bounds="(]"), "(1,2]"),
(Range(None, None, bounds="()"), "(,)"),
(Range(None, 1, bounds="[)"), "[,1)"),
(Range(1, None, bounds="[)"), "[1,)"),
(Range(0, 6, bounds="[)"), "[0,6)"),
argnames="r1,r1repr",
)
- def test_is_sub_super_set(self, connection, r1, r1repr, r2, r2repr):
- q = text(f"select '{r1repr}'::int4range <@ '{r2repr}'::int4range")
- eq_(r1.issubset(r2), connection.scalar(q))
+ def test_contains_range(self, connection, r1, r1repr, r2, r2repr):
+ q = text(f"select '{r1repr}'::int4range @> '{r2repr}'::int4range")
+ eq_(r1.contains(r2), connection.scalar(q))
q = text(f"select '{r2repr}'::int4range @> '{r1repr}'::int4range")
- eq_(r2.issuperset(r1), connection.scalar(q))
+ eq_(r2.contains(r1), connection.scalar(q))