summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-11-19 15:10:46 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2015-11-19 15:11:37 -0500
commit9ed36c2eed34b23560abe3cdf373c720b24d3b30 (patch)
treee387738957531c49c24745158884847ee837ee83
parentc7f9aa281857eeeaf7963c370bda43d2eb4746f5 (diff)
downloadsqlalchemy-9ed36c2eed34b23560abe3cdf373c720b24d3b30.tar.gz
- 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)
-rw-r--r--doc/build/changelog/changelog_10.rst10
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py6
-rw-r--r--test/dialect/postgresql/test_types.py18
3 files changed, 34 insertions, 0 deletions
diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst
index 7dadc445f..c800647a6 100644
--- a/doc/build/changelog/changelog_10.rst
+++ b/doc/build/changelog/changelog_10.rst
@@ -19,6 +19,16 @@
: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
:versions: 1.1.0b1
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index ef870a177..8de5bede7 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 6ed90c76d..49a8cfabd 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):