]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed the ``.python_type`` attribute of :class:`.postgresql.INTERVAL`
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Nov 2015 20:10:46 +0000 (15:10 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Nov 2015 20:11:37 +0000 (15:11 -0500)
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)

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_types.py

index 7dadc445fdbe80fb73110b052f1c4bb2066d58ec..c800647a6972a6bb8a3492911f62598819d8b631 100644 (file)
 .. 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
index ef870a177b7e6af6d03e632e75d6ab88b72627a0..8de5bede7840312118cf1215e9dcf55c345080ff 100644 (file)
@@ -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
 
 
index 6ed90c76d62ccebc2d04c0201b759c3871fb7fb6..49a8cfabdd4e4164dea905f2b767779651915e18 100644 (file)
@@ -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):