]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Modify range bounds to make them consistent with inf bounds
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 2 Oct 2021 17:22:26 +0000 (19:22 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 4 Oct 2021 12:44:13 +0000 (14:44 +0200)
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
tests/types/test_range.py

index 06b81a6fc6800d2c3d2858b18ca218cc9b7d6d72..f8429072ae999b085920780ab96a269b6e0008a5 100644 (file)
@@ -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
index 2860cac8f6f1472e925b1f58afec74176d2de852..916668046fd333de470d7b5d231203abae79686f 100644 (file)
@@ -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"):