From 885068ec7b3fedd1052a7b1071fa84e81485dd6e Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sun, 27 Nov 2022 10:16:02 +0100 Subject: [PATCH] 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. --- lib/sqlalchemy/dialects/postgresql/ranges.py | 2 +- test/dialect/postgresql/test_types.py | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) 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",) -- 2.47.3