From: Lele Gaifax Date: Sun, 27 Nov 2022 19:44:13 +0000 (-0500) Subject: Rectify PG Range.__bool__, inverting previous logic X-Git-Tag: rel_2_0_0b4~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=771a5d1a4b517fe56d79caf254e10c12024295ef;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. Closes: #8885 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8885 Pull-request-sha: 5670cdb920692a62f77b7b6ea312784033de83d9 Change-Id: I6f4a40168b2f037c578e84f7550370411bd42160 --- 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",)