From: Mike Bayer Date: Fri, 13 Aug 2010 18:25:58 +0000 (-0400) Subject: - Added basic math expression coercion for X-Git-Tag: rel_0_6_4~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=252ab17c7dc5f1cfd1a5173fb6d9e4b819e1ebb7;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Added basic math expression coercion for Numeric->Integer, so that resulting type is Numeric regardless of the direction of the expression. --- 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