From 8b9e7b7e3345673b43aeabd7ec88b88dc3cfa7eb Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sun, 4 Dec 2022 18:37:11 +0100 Subject: [PATCH] Add properties to new PG Range class for compatibility with other implementations --- lib/sqlalchemy/dialects/postgresql/ranges.py | 34 ++++++++++++++++++++ test/dialect/postgresql/test_types.py | 18 +++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/sqlalchemy/dialects/postgresql/ranges.py b/lib/sqlalchemy/dialects/postgresql/ranges.py index e772777bf3..c136acd76a 100644 --- a/lib/sqlalchemy/dialects/postgresql/ranges.py +++ b/lib/sqlalchemy/dialects/postgresql/ranges.py @@ -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() diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index ec9bcbae92..dcde497b46 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -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 ): -- 2.47.3