Saving np.nan to sqlite database Django 2.1 in unit test fails

88 views
Skip to first unread message

BK

unread,
Feb 13, 2020, 12:06:00 PM2/13/20
to Django users
I upgraded to django 2.1 and some of my unit tests (which use a sqlite database) are failing with the following:

File "/home/travis/virtualenv/python3.5.6/lib/python3.5/site-packages/django/db/models/query.py", line 268, in __iter__
2113 self._fetch_all()
2114 File "/home/travis/virtualenv/python3.5.6/lib/python3.5/site-packages/django/db/models/query.py", line 1186, in _fetch_all
2115 self._result_cache = list(self._iterable_class(self))
2116 File "/home/travis/virtualenv/python3.5.6/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1042, in apply_converters
2117 value = converter(value, expression, connection)
2118 File "/home/travis/virtualenv/python3.5.6/lib/python3.5/site-packages/django/db/backends/sqlite3/operations.py", line 254, in converter
2119 return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
2120TypeError: argument must be int of float



The unit test involves adding numpy.nan (among other things) to the database (in a models.DecimalField field) and at some point later in the test we are querying that data and when it try's to evaluate the query set the above error occurs.


The following code in sqlite/operations.py was changed from 2.0 to 2.1 which seems to be the source of the error.

FROM Django 2.0

    def convert_decimalfield_value(self, value, expression, connection):
        if value is not None:
            value = expression.output_field.format_number(value)
            # Value is not converted to Decimal here as it will be converted
            # later in BaseExpression.convert_value().
        return value



FROM Django 2.1

    def get_decimalfield_converter(self, expression):
        # SQLite stores only 15 significant digits. Digits coming from
        # float inaccuracy must be removed.
        create_decimal = decimal.Context(prec=15).create_decimal_from_float
        if isinstance(expression, Col):
            quantize_value = decimal.Decimal(1).scaleb(-expression.output_field.decimal_places)

            def converter(value, expression, connection):
                if value is not None:
                    return create_decimal(value).quantize(quantize_value, context=expression.output_field.context)
        else:
            def converter(value, expression, connection):
                if value is not None:
                    return create_decimal(value)
        return converter



Has anyone faced this issue....not sure what other details to provide...happy to provide more upon request. Python 3.5.9 (default, Nov 24 2019, 01:35:13) Type 'copyright', 'credits' or 'license' for more information IPython 7.0.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import sqlite3 In [2]: sqlite3.version Out[2]: '2.6.0' In [3]: sqlite3.sqlite_version Out[3]: '3.22.0'
Django==2.1.15
mysqlclient==1.3.9
NumPy==1.11.1
pandas==0.19.2

Reply all
Reply to author
Forward
0 new messages