From be25a0b1034dcea9478190fa43ad16e488962f8b Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 2 Oct 2021 19:22:26 +0200 Subject: [PATCH] Modify range bounds to make them consistent with inf bounds Without doing so, there could be a range '[,]' which would still be reported as having not lower_inc or upper_inc. --- psycopg/psycopg/types/range.py | 7 +++++++ tests/types/test_range.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/psycopg/psycopg/types/range.py b/psycopg/psycopg/types/range.py index 06b81a6fc..f8429072a 100644 --- a/psycopg/psycopg/types/range.py +++ b/psycopg/psycopg/types/range.py @@ -56,6 +56,13 @@ class Range(Generic[T]): self._lower = lower self._upper = upper + + # Make bounds consistent with infs + if lower is None and bounds[0] == "[": + bounds = "(" + bounds[1] + if upper is None and bounds[1] == "]": + bounds = bounds[0] + ")" + self._bounds = bounds else: self._lower = self._upper = None diff --git a/tests/types/test_range.py b/tests/types/test_range.py index 2860cac8f..916668046 100644 --- a/tests/types/test_range.py +++ b/tests/types/test_range.py @@ -668,6 +668,24 @@ class TestRangeObject: result = str(r) assert result == expected + def test_exclude_inf_bounds(self): + r = Range(None, 10, "[]") + assert r.lower is None + assert not r.lower_inc + assert r.bounds == "(]" + + r = Range(10, None, "[]") + assert r.upper is None + assert not r.upper_inc + assert r.bounds == "[)" + + r = Range(None, None, "[]") + assert r.lower is None + assert not r.lower_inc + assert r.upper is None + assert not r.upper_inc + assert r.bounds == "()" + def test_no_info_error(conn): with pytest.raises(TypeError, match="range"): -- 2.47.2