From 771a5d1a4b517fe56d79caf254e10c12024295ef Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Sun, 27 Nov 2022 14:44:13 -0500 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. 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 | 10 ++++++++-- lib/sqlalchemy/dialects/postgresql/ranges.py | 2 +- test/dialect/postgresql/test_types.py | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/doc/build/changelog/unreleased_20/8765.rst b/doc/build/changelog/unreleased_20/8765.rst index a210fb3486..1f7fe144b6 100644 --- a/doc/build/changelog/unreleased_20/8765.rst +++ b/doc/build/changelog/unreleased_20/8765.rst @@ -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. 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.2