From: Mike Bayer Date: Thu, 19 Nov 2015 20:10:46 +0000 (-0500) Subject: - Fixed the ``.python_type`` attribute of :class:`.postgresql.INTERVAL` X-Git-Tag: rel_1_1_0b1~84^2~70^2~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ed36c2eed34b23560abe3cdf373c720b24d3b30;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed the ``.python_type`` attribute of :class:`.postgresql.INTERVAL` to return ``datetime.timedelta`` in the same way as that of :obj:`.types.Interval.python_type`, rather than raising ``NotImplementedError``. fixes #3571 (cherry picked from commit 29d6f6e19b014bb5ce79032bd8803e32b4da0e5e) --- diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst index 7dadc445fd..c800647a69 100644 --- a/doc/build/changelog/changelog_10.rst +++ b/doc/build/changelog/changelog_10.rst @@ -18,6 +18,16 @@ .. changelog:: :version: 1.0.10 + .. change:: + :tags: bug, postgresql + :versions: 1.1.0b1 + :tickets: 3571 + + Fixed the ``.python_type`` attribute of :class:`.postgresql.INTERVAL` + to return ``datetime.timedelta`` in the same way as that of + :obj:`.types.Interval.python_type`, rather than raising + ``NotImplementedError``. + .. change:: :tags: bug, mssql :pullreq: github:213 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index ef870a177b..8de5bede78 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -603,6 +603,8 @@ a new version. """ from collections import defaultdict import re +import datetime as dt + from ... import sql, schema, exc, util from ...engine import default, reflection @@ -709,6 +711,10 @@ class INTERVAL(sqltypes.TypeEngine): def _type_affinity(self): return sqltypes.Interval + @property + def python_type(self): + return dt.timedelta + PGInterval = INTERVAL diff --git a/test/dialect/postgresql/test_types.py b/test/dialect/postgresql/test_types.py index 6ed90c76d6..49a8cfabdd 100644 --- a/test/dialect/postgresql/test_types.py +++ b/test/dialect/postgresql/test_types.py @@ -589,6 +589,14 @@ class NumericInterpretationTest(fixtures.TestBase): ) +class PythonTypeTest(fixtures.TestBase): + def test_interval(self): + is_( + postgresql.INTERVAL().python_type, + datetime.timedelta + ) + + class TimezoneTest(fixtures.TestBase): __backend__ = True @@ -1419,6 +1427,16 @@ class TimestampTest(fixtures.TestBase, AssertsExecutionResults): result = connection.execute(s).first() eq_(result[0], datetime.datetime(2007, 12, 25, 0, 0)) + def test_interval_arithmetic(self): + # basically testing that we get timedelta back for an INTERVAL + # result. more of a driver assertion. + engine = testing.db + connection = engine.connect() + + s = select([text("timestamp '2007-12-25' - timestamp '2007-11-15'")]) + result = connection.execute(s).first() + eq_(result[0], datetime.timedelta(40)) + class SpecialTypesTest(fixtures.TestBase, ComparesTables, AssertsCompiledSQL):