From 563180f7d46b24bd334d227104c90bf8cdb81158 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 26 Jun 2017 16:50:24 -0400 Subject: [PATCH] Coerce to float for Float with all native decimal backends The result processor for the :class:`.Float` type now unconditionally runs values through the ``float()`` processor if the dialect specifies that it also supports "native decimal" mode. While most backends will deliver Python ``float`` objects for a floating point datatype, the MySQL backends in some cases lack the typing information in order to provide this and return ``Decimal`` unless the float conversion is done. Change-Id: I638f1480fb00a507036efaf0e0080f26893d98ad Fixes: #4020 --- doc/build/changelog/changelog_12.rst | 16 ++++++++++++++++ doc/build/changelog/migration_12.rst | 9 +++++++++ lib/sqlalchemy/sql/sqltypes.py | 2 ++ lib/sqlalchemy/testing/suite/test_types.py | 1 - 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/build/changelog/changelog_12.rst b/doc/build/changelog/changelog_12.rst index 043f5f1ac2..7060d04dbf 100644 --- a/doc/build/changelog/changelog_12.rst +++ b/doc/build/changelog/changelog_12.rst @@ -29,6 +29,22 @@ :ref:`change_floats_12` + .. change:: 4020 + :tags: bug, sql, mysql + :tickets: 4020 + + The result processor for the :class:`.Float` type now unconditionally + runs values through the ``float()`` processor if the dialect + specifies that it also supports "native decimal" mode. While most + backends will deliver Python ``float`` objects for a floating point + datatype, the MySQL backends in some cases lack the typing information + in order to provide this and return ``Decimal`` unless the float + conversion is done. + + .. seealso:: + + :ref:`change_floats_12` + .. change:: 4017 :tags: bug, sql :tickets: 4017 diff --git a/doc/build/changelog/migration_12.rst b/doc/build/changelog/migration_12.rst index a1aeaf1f24..b5594f07ce 100644 --- a/doc/build/changelog/migration_12.rst +++ b/doc/build/changelog/migration_12.rst @@ -801,9 +801,18 @@ if the application is working with plain floats. expr = column('a', Integer) * column('b', Float()) assert isinstance(expr.type, Float) +* The :class:`.Float` datatype will apply the ``float()`` processor to + result values unconditionally if the DBAPI is known to support native + ``Decimal()`` mode. Some backends do not always guarantee that a floating + point number comes back as plain float and not precision numeric such + as MySQL. :ticket:`4017` +:ticket:`4018` + +:ticket:`4020` + Key Behavioral Changes - ORM ============================ diff --git a/lib/sqlalchemy/sql/sqltypes.py b/lib/sqlalchemy/sql/sqltypes.py index 5b53f390ed..6838baa5f1 100644 --- a/lib/sqlalchemy/sql/sqltypes.py +++ b/lib/sqlalchemy/sql/sqltypes.py @@ -702,6 +702,8 @@ class Float(Numeric): return processors.to_decimal_processor_factory( decimal.Decimal, self._effective_decimal_return_scale) + elif dialect.supports_native_decimal: + return processors.to_float else: return None diff --git a/lib/sqlalchemy/testing/suite/test_types.py b/lib/sqlalchemy/testing/suite/test_types.py index de32e77a4c..a345454be3 100644 --- a/lib/sqlalchemy/testing/suite/test_types.py +++ b/lib/sqlalchemy/testing/suite/test_types.py @@ -431,7 +431,6 @@ class NumericTest(_LiteralRoundTripFixture, fixtures.TestBase): filter_=lambda n: n is not None and round(n, 5) or None ) - @testing.fails_on("mysql", "until we do #4020") def test_float_coerce_round_trip(self): expr = 15.7563 -- 2.39.5