]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Rectify PG Range.__bool__, inverting previous logic
authorLele Gaifax <lele@metapensiero.it>
Sun, 27 Nov 2022 09:16:02 +0000 (10:16 +0100)
committerLele Gaifax <lele@metapensiero.it>
Sun, 27 Nov 2022 09:16:02 +0000 (10:16 +0100)
The boolness of the range was defined to be equal to its emptiness. As
this has been identified as a typo rather than the intended, this
inverts the logic, to match common Python behaviour as well as how other
popular PG drivers do.

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

index a4c39d0639f6282acf3d120678b5ab5d1c87d345..b1f418e415ba27901925b05c21b94fb6baad6b32 100644 (file)
@@ -82,7 +82,7 @@ class Range(Generic[_T]):
             )
 
     def __bool__(self) -> bool:
-        return self.empty
+        return not self.empty
 
     def _contains_value(self, value: _T) -> bool:
         "Check whether this range contains the given `value`."
index a7b32f607666adc5b0aba21a7526776df7ea4578..ec9bcbae92a104b9c7d94d77f8572598da6cac6d 100644 (file)
@@ -4410,6 +4410,10 @@ class _RangeComparisonFixtures(_RangeTests):
             f"{r1} != {r2}: got {r1 != r2}, expected {different}",
         )
 
+    def test_bool(self):
+        is_false(bool(Range(empty=True)))
+        is_true(bool(Range(1, 2)))
+
 
 class _RangeTypeRoundTrip(_RangeComparisonFixtures, fixtures.TablesTest):
     __requires__ = ("range_types",)