From 252ab17c7dc5f1cfd1a5173fb6d9e4b819e1ebb7 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 13 Aug 2010 14:25:58 -0400 Subject: [PATCH] - Added basic math expression coercion for Numeric->Integer, so that resulting type is Numeric regardless of the direction of the expression. --- CHANGES | 5 +++++ lib/sqlalchemy/types.py | 5 +++++ test/sql/test_types.py | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/CHANGES b/CHANGES index 24bd97b288..da9f3dca0e 100644 --- a/CHANGES +++ b/CHANGES @@ -77,6 +77,11 @@ CHANGES the outer query. [ticket:1852] - sql + - Added basic math expression coercion for + Numeric->Integer, + so that resulting type is Numeric regardless + of the direction of the expression. + - Changed the scheme used to generate truncated "auto" index names when using the "index=True" flag on Column. The truncation only takes diff --git a/lib/sqlalchemy/types.py b/lib/sqlalchemy/types.py index 91eb352751..777353714a 100644 --- a/lib/sqlalchemy/types.py +++ b/lib/sqlalchemy/types.py @@ -1024,20 +1024,25 @@ class Numeric(_DateAffinity, TypeEngine): operators.mul:{ Interval:Interval, Numeric:Numeric, + Integer:Numeric, }, # Py2K operators.div:{ Numeric:Numeric, + Integer:Numeric, }, # end Py2K operators.truediv:{ Numeric:Numeric, + Integer:Numeric, }, operators.add:{ Numeric:Numeric, + Integer:Numeric, }, operators.sub:{ Numeric:Numeric, + Integer:Numeric, } } diff --git a/test/sql/test_types.py b/test/sql/test_types.py index 91e4ec177a..a80e761d73 100644 --- a/test/sql/test_types.py +++ b/test/sql/test_types.py @@ -904,6 +904,30 @@ class ExpressionTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL): expr = func.current_date() - column('foo', types.TIMESTAMP) eq_(expr.type._type_affinity, types.Interval) + def test_numerics_coercion(self): + from sqlalchemy.sql import column + import operator + + for op in ( + operator.add, + operator.mul, + operator.truediv, + operator.sub + ): + for other in (Numeric(10, 2), Integer): + expr = op( + column('bar', types.Numeric(10, 2)), + column('foo', other) + ) + assert isinstance(expr.type, types.Numeric) + expr = op( + column('foo', other), + column('bar', types.Numeric(10, 2)) + ) + assert isinstance(expr.type, types.Numeric) + + + def test_expression_typing(self): expr = column('bar', Integer) - 3 -- 2.47.2