]> 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 19:44:13 +0000 (14:44 -0500)
committersqla-tester <sqla-tester@sqlalchemy.org>
Sun, 27 Nov 2022 19:44:13 +0000 (14:44 -0500)
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.

Closes: #8885
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8885
Pull-request-sha: 5670cdb920692a62f77b7b6ea312784033de83d9

Change-Id: I6f4a40168b2f037c578e84f7550370411bd42160

doc/build/changelog/unreleased_20/8765.rst
lib/sqlalchemy/dialects/postgresql/ranges.py
test/dialect/postgresql/test_types.py

index a210fb348676e9cf9e9b2373ae02cf4ef4a2890f..1f7fe144b69f68d8a2fac4c9691157f039aeef85 100644 (file)
@@ -6,5 +6,11 @@
     ``adjacent_to()``, ``difference()``, ``union()``, etc., were added to the
     PG-specific range objects, bringing them in par with the standard
     operators implemented by the underlying
-    :attr:`_postgresql.AbstractRange.comparator_factory`. Pull request
-    courtesy Lele Gaifax.
+    :attr:`_postgresql.AbstractRange.comparator_factory`.
+
+    In addition, the ``__bool__()_`` method of the class has been corrected to
+    be consistent with the common Python *containers* behavior as well as how
+    other popular PostgreSQL drivers do: it now tells whether the range
+    instance is *not* empty, rather than the other way around.
+
+    Pull request courtesy Lele Gaifax.
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",)