From: Lele Gaifax Date: Sun, 27 Nov 2022 09:16:02 +0000 (+0100) Subject: Rectify PG Range.__bool__, inverting previous logic X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=885068ec7b3fedd1052a7b1071fa84e81485dd6e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Rectify PG Range.__bool__, inverting previous logic 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. --- diff --git a/lib/sqlalchemy/dialects/postgresql/ranges.py b/lib/sqlalchemy/dialects/postgresql/ranges.py index a4c39d0639..b1f418e415 100644 --- a/lib/sqlalchemy/dialects/postgresql/ranges.py +++ b/lib/sqlalchemy/dialects/postgresql/ranges.py @@ -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`." diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index a7b32f6076..ec9bcbae92 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -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",)