From 11da342dc85b98ffdddf69d1126306c031d035b4 Mon Sep 17 00:00:00 2001 From: Lele Gaifax Date: Wed, 26 Oct 2022 10:00:43 +0200 Subject: [PATCH] Rename methods on the Range class to mimic the concrete operators impl This follows the considerations made by Mike on the PR #8707. One (new) test case now fails. --- lib/sqlalchemy/dialects/postgresql/ranges.py | 19 ++++++++++++++----- test/dialect/postgresql/test_dialect.py | 14 +++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/sqlalchemy/dialects/postgresql/ranges.py b/lib/sqlalchemy/dialects/postgresql/ranges.py index a5fb79f413..93ca39b543 100644 --- a/lib/sqlalchemy/dialects/postgresql/ranges.py +++ b/lib/sqlalchemy/dialects/postgresql/ranges.py @@ -12,6 +12,7 @@ from typing import Any from typing import Generic from typing import Optional from typing import TypeVar +from typing import Union from ... import types as sqltypes from ...util import py310 @@ -80,7 +81,7 @@ class Range(Generic[_T]): def __bool__(self) -> bool: return self.empty - def contains_value(self, value: _T) -> bool: + def _contains_value(self, value: _T) -> bool: "Check whether this range contains the given `value`." if self.empty: @@ -110,7 +111,7 @@ class Range(Generic[_T]): else value <= self.upper ) - def issubset(self, other) -> bool: + def _contained_by(self, other: Range) -> bool: "Determine whether this range is a contained by `other`." # Any range contains the empty one @@ -154,10 +155,18 @@ class Range(Generic[_T]): upper_side = other.upper == self.upper return upper_side - def issuperset(self, other) -> bool: - "Determine whether this range contains `other`." + def contains(self, value: Union[_T, Range]) -> bool: + "Determine whether this range contains `value`." - return other.issubset(self) + if isinstance(value, Range): + return value._contained_by(self) + else: + return self._contains_value(value) + + def contained_by(self, other: Range) -> bool: + "Determine whether this range is a contained by `other`." + + return self._contained_by_range(other) class AbstractRange(sqltypes.TypeEngine): diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index 8c6a3982f9..3c36d3b787 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -1228,13 +1228,13 @@ class TestRange(fixtures.TestBase): (Range(1, 4, bounds="()"), "(1,4)"), argnames="r,rrepr", ) - def test_range_contains_value(self, connection, r, rrepr, v): + def test_contains_value(self, connection, r, rrepr, v): q = text(f"select {v} <@ '{rrepr}'::int4range") - eq_(r.contains_value(v), connection.scalar(q)) + eq_(r.contains(v), connection.scalar(q)) @testing.combinations( (Range(empty=True), "empty"), - (Range(1, 2, bounds="()"), "(1,2)"), + (Range(1, 2, bounds="(]"), "(1,2]"), (Range(None, None, bounds="()"), "(,)"), (Range(None, 1, bounds="[)"), "[,1)"), (Range(1, None, bounds="[)"), "[1,)"), @@ -1258,8 +1258,8 @@ class TestRange(fixtures.TestBase): (Range(0, 6, bounds="[)"), "[0,6)"), argnames="r1,r1repr", ) - def test_is_sub_super_set(self, connection, r1, r1repr, r2, r2repr): - q = text(f"select '{r1repr}'::int4range <@ '{r2repr}'::int4range") - eq_(r1.issubset(r2), connection.scalar(q)) + def test_contains_range(self, connection, r1, r1repr, r2, r2repr): + q = text(f"select '{r1repr}'::int4range @> '{r2repr}'::int4range") + eq_(r1.contains(r2), connection.scalar(q)) q = text(f"select '{r2repr}'::int4range @> '{r1repr}'::int4range") - eq_(r2.issuperset(r1), connection.scalar(q)) + eq_(r2.contains(r1), connection.scalar(q)) -- 2.47.3