meant the result type would coerce to ``Decimal()``. In particular,
this would emit a confusing warning on SQLite::
- float_value = connection.scalar(
- select([literal(4.56)]) # the "BindParameter" will now be
- # Float, not Numeric(asdecimal=True)
- )
+
+ float_value = connection.scalar(
+ select([literal(4.56)]) # the "BindParameter" will now be
+ # Float, not Numeric(asdecimal=True)
+ )
+
+* Math operations between :class:`.Numeric`, :class:`.Float`, and
+ :class:`.Integer` will now preserve the :class:`.Numeric` or :class:`.Float`
+ type in the resulting expression's type, including the ``asdecimal`` flag
+ as well as if the type should be :class:`.Float`::
+
+ # asdecimal flag is maintained
+ expr = column('a', Integer) * column('b', Numeric(asdecimal=False))
+ assert expr.type.asdecimal == False
+
+ # Float subclass of Numeric is maintained
+ expr = column('a', Integer) * column('b', Float())
+ assert isinstance(expr.type, Float)
+
:ticket:`4017`
import array
-class _DateAffinity(object):
+class _LookupExpressionAdapter(object):
- """Mixin date/time specific expression adaptations.
+ """Mixin expression adaptations based on lookup tables.
- Rules are implemented within Date,Time,Interval,DateTime, Numeric,
- Integer. Based on http://www.postgresql.org/docs/current/static
- /functions-datetime.html.
+ These rules are currenly used by the numeric, integer and date types
+ which have detailed cross-expression coercion rules.
"""
def _adapt_expression(self, op, other_comparator):
othertype = other_comparator.type._type_affinity
- return (
- op, to_instance(
- self.type._expression_adaptations.
- get(op, self._blank_dict).
- get(othertype, NULLTYPE))
- )
+ lookup = self.type._expression_adaptations.get(
+ op, self._blank_dict).get(
+ othertype, NULLTYPE)
+ if lookup is othertype:
+ return (op, other_comparator.type)
+ elif lookup is self.type._type_affinity:
+ return (op, self.type)
+ else:
+ return (op, to_instance(lookup))
comparator_factory = Comparator
super(UnicodeText, self).__init__(length=length, **kwargs)
-class Integer(_DateAffinity, TypeEngine):
+class Integer(_LookupExpressionAdapter, TypeEngine):
"""A type for ``int`` integers."""
__visit_name__ = 'big_integer'
-class Numeric(_DateAffinity, TypeEngine):
+class Numeric(_LookupExpressionAdapter, TypeEngine):
"""A type for fixed precision numbers, such as ``NUMERIC`` or ``DECIMAL``.
else:
return None
- @util.memoized_property
- def _expression_adaptations(self):
- return {
- operators.mul: {
- Interval: Interval,
- Numeric: self.__class__,
- },
- operators.div: {
- Numeric: self.__class__,
- },
- operators.truediv: {
- Numeric: self.__class__,
- },
- operators.add: {
- Numeric: self.__class__,
- },
- operators.sub: {
- Numeric: self.__class__,
- }
- }
-
-class DateTime(_DateAffinity, TypeEngine):
+class DateTime(_LookupExpressionAdapter, TypeEngine):
"""A type for ``datetime.datetime()`` objects.
@util.memoized_property
def _expression_adaptations(self):
+
+ # Based on http://www.postgresql.org/docs/current/\
+ # static/functions-datetime.html.
+
return {
operators.add: {
Interval: self.__class__,
}
-class Date(_DateAffinity, TypeEngine):
+class Date(_LookupExpressionAdapter, TypeEngine):
"""A type for ``datetime.date()`` objects."""
@util.memoized_property
def _expression_adaptations(self):
+ # Based on http://www.postgresql.org/docs/current/\
+ # static/functions-datetime.html.
+
return {
operators.add: {
Integer: self.__class__,
}
-class Time(_DateAffinity, TypeEngine):
+class Time(_LookupExpressionAdapter, TypeEngine):
"""A type for ``datetime.time()`` objects."""
@util.memoized_property
def _expression_adaptations(self):
+ # Based on http://www.postgresql.org/docs/current/\
+ # static/functions-datetime.html.
+
return {
operators.add: {
Date: DateTime,
return processors.int_to_boolean
-class Interval(_DateAffinity, TypeDecorator):
+class Interval(_LookupExpressionAdapter, TypeDecorator):
"""A type for ``datetime.timedelta()`` objects.
@util.memoized_property
def _expression_adaptations(self):
+ # Based on http://www.postgresql.org/docs/current/\
+ # static/functions-datetime.html.
+
return {
operators.add: {
Date: DateTime,
from sqlalchemy.testing.util import round_decimal
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import mock
+from sqlalchemy.sql import column
+import operator
class AdaptTest(fixtures.TestBase):
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):
)
assert isinstance(expr.type, types.Numeric)
+ def test_asdecimal_int_to_numeric(self):
+ expr = column('a', Integer) * column('b', Numeric(asdecimal=False))
+ is_(expr.type.asdecimal, False)
+
+ expr = column('a', Integer) * column('b', Numeric())
+ is_(expr.type.asdecimal, True)
+
+ expr = column('a', Integer) * column('b', Float())
+ is_(expr.type.asdecimal, False)
+ assert isinstance(expr.type, Float)
+
+ def test_asdecimal_numeric_to_int(self):
+ expr = column('a', Numeric(asdecimal=False)) * column('b', Integer)
+ is_(expr.type.asdecimal, False)
+
+ expr = column('a', Numeric()) * column('b', Integer)
+ is_(expr.type.asdecimal, True)
+
+ expr = column('a', Float()) * column('b', Integer)
+ is_(expr.type.asdecimal, False)
+ assert isinstance(expr.type, Float)
+
def test_null_comparison(self):
eq_(
str(column('a', types.NullType()) + column('b', types.NullType())),