]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add properties to new PG Range class for compatibility with other implementations 8927/head
authorLele Gaifax <lele@metapensiero.it>
Sun, 4 Dec 2022 17:37:11 +0000 (18:37 +0100)
committerLele Gaifax <lele@metapensiero.it>
Sun, 4 Dec 2022 18:08:14 +0000 (19:08 +0100)
lib/sqlalchemy/dialects/postgresql/ranges.py
test/dialect/postgresql/test_types.py

index e772777bf31c5090b065b93d639f3272dce8abdf..c136acd76a3eef1864dc12d1488bcaee37b963f1 100644 (file)
@@ -85,6 +85,40 @@ class Range(Generic[_T]):
     def __bool__(self) -> bool:
         return not self.empty
 
+    @property
+    def isempty(self):
+        "Compability accessor to this range emptiness."
+
+        return self.empty
+
+    @property
+    def lower_inc(self):
+        "Check whether the lower bound is inclusive or not."
+
+        return self.bounds[0] == "["
+
+    @property
+    def lower_inf(self):
+        """Check whether this range is not empty and its lower bound is
+        infinite or not.
+        """
+
+        return not self.empty and self.lower is None
+
+    @property
+    def upper_inc(self):
+        "Check whether the upper bound is inclusive or not."
+
+        return self.bounds[1] == "]"
+
+    @property
+    def upper_inf(self):
+        """Check whether this range is not empty and its upper bound is
+        infinite or not.
+        """
+
+        return not self.empty and self.upper is None
+
     @property
     def __sa_type_engine__(self):
         return AbstractRange()
index ec9bcbae92a104b9c7d94d77f8572598da6cac6d..dcde497b46326292f269deea13802db91b3d2ec2 100644 (file)
@@ -3996,6 +3996,24 @@ class _RangeComparisonFixtures(_RangeTests):
 
         is_false(range_.contains(values["rh"]))
 
+    def test_compatibility_accessors(self):
+        range_ = self._data_obj()
+
+        is_true(range_.lower_inc)
+        is_false(range_.upper_inc)
+        is_false(Range(lower=range_.lower, bounds="()").lower_inc)
+        is_true(Range(upper=range_.upper, bounds="(]").upper_inc)
+
+        is_false(range_.lower_inf)
+        is_false(range_.upper_inf)
+        is_false(Range(empty=True).lower_inf)
+        is_false(Range(empty=True).upper_inf)
+        is_true(Range().lower_inf)
+        is_true(Range().upper_inf)
+
+        is_false(range_.isempty)
+        is_true(Range(empty=True).isempty)
+
     def test_contains_value(
         self, connection, bounds_obj_combinations, value_combinations
     ):