]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Rename methods on the Range class to mimic the concrete operators impl
authorLele Gaifax <lele@metapensiero.it>
Wed, 26 Oct 2022 08:00:43 +0000 (10:00 +0200)
committerLele Gaifax <lele@metapensiero.it>
Wed, 26 Oct 2022 16:50:58 +0000 (18:50 +0200)
This follows the considerations made by Mike on the PR #8707.

One (new) test case now fails.

lib/sqlalchemy/dialects/postgresql/ranges.py
test/dialect/postgresql/test_dialect.py

index a5fb79f41302be80fb572877f870dd31965bb0dd..93ca39b5430777affb22fa18062b17d9a7ffbf95 100644 (file)
@@ -12,6 +12,7 @@ from typing import Any
 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
@@ -80,7 +81,7 @@ class Range(Generic[_T]):
     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:
@@ -110,7 +111,7 @@ class Range(Generic[_T]):
             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
@@ -154,10 +155,18 @@ class Range(Generic[_T]):
                 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):
index 8c6a3982f93ffd7d49dffd122769f38740efc9eb..3c36d3b787b7cc622b91023390509a125afe066e 100644 (file)
@@ -1228,13 +1228,13 @@ class TestRange(fixtures.TestBase):
         (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,)"),
@@ -1258,8 +1258,8 @@ class TestRange(fixtures.TestBase):
         (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))