From: MajorDallas <79329882+MajorDallas@users.noreply.github.com> Date: Tue, 22 Jun 2021 19:34:09 +0000 (-0400) Subject: Add impl property to PostgreSQL / Oracle INTERVAL class X-Git-Tag: rel_1_4_19~3^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac20c3d34bf0127972acae8c712b98b1187b98f9;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add impl property to PostgreSQL / Oracle INTERVAL class Fixed issue where the ``INTERVAL`` datatype on PostgreSQL and Oracle would produce an ``AttributeError`` when used in the context of a comparison operation against a ``timedelta()`` object. Pull request courtesy MajorDallas. Fixes: #6649 Closes: #6650 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/6650 Pull-request-sha: dd217a975e5f0d3157e81c731791225b6a32889f Change-Id: I773caf2673294fdb3c92b42895ad714e944d1bf8 --- diff --git a/doc/build/changelog/unreleased_14/6649.rst b/doc/build/changelog/unreleased_14/6649.rst new file mode 100644 index 0000000000..70ec64216d --- /dev/null +++ b/doc/build/changelog/unreleased_14/6649.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, postgresql, oracle + :tickets: 6649 + + Fixed issue where the ``INTERVAL`` datatype on PostgreSQL and Oracle would + produce an ``AttributeError`` when used in the context of a comparison + operation against a ``timedelta()`` object. Pull request courtesy + MajorDallas. diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index 57f55c1824..a72088a461 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -702,6 +702,9 @@ class INTERVAL(sqltypes.NativeForEmulated, sqltypes._AbstractInterval): day_precision=self.day_precision, ) + def coerce_compared_value(self, op, value): + return self + class ROWID(sqltypes.TypeEngine): """Oracle ROWID type. diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index deb6243296..4c78981329 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1655,6 +1655,9 @@ class INTERVAL(sqltypes.NativeForEmulated, sqltypes._AbstractInterval): def python_type(self): return dt.timedelta + def coerce_compared_value(self, op, value): + return self + PGInterval = INTERVAL diff --git a/test/dialect/oracle/test_types.py b/test/dialect/oracle/test_types.py index 4b11960257..70b00c06f2 100644 --- a/test/dialect/oracle/test_types.py +++ b/test/dialect/oracle/test_types.py @@ -183,6 +183,10 @@ class DialectTypesTest(fixtures.TestBase, AssertsCompiledSQL): def test_interval(self, type_, expected): self.assert_compile(type_, expected) + def test_interval_coercion_literal(self): + expr = column("bar", oracle.INTERVAL) == datetime.timedelta(days=1) + eq_(expr.right.type._type_affinity, sqltypes.Interval) + class TypesTest(fixtures.TestBase): __only_on__ = "oracle" diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index c83fe0f8a1..3fdb31b7cb 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -2427,6 +2427,10 @@ class TimestampTest(fixtures.TestBase, AssertsExecutionResults): eq_(expr.type._type_affinity, types.Interval) assert isinstance(expr.type, postgresql.INTERVAL) + def test_interval_coercion_literal(self): + expr = column("bar", postgresql.INTERVAL) == datetime.timedelta(days=1) + eq_(expr.right.type._type_affinity, types.Interval) + class SpecialTypesCompileTest(fixtures.TestBase, AssertsCompiledSQL): __dialect__ = "postgresql"