#34014: django.forms.fields.DecimalValidator fails to validate 0 in scientific
notation (0E+1 or 0E+2)
-------------------------------------+-------------------------------------
     Reporter:  Shiplu Mokaddim      |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Forms                |                  Version:  2.0
     Severity:  Normal               |               Resolution:
     Keywords:  DecimalValidator,    |             Triage Stage:
  Decimal, DecimalField              |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by Shiplu Mokaddim:

Old description:

> In django 1.11 DecimalValidator treats `0E+1` as valid decimal
>
> {{{
> >>> from django.forms.fields import DecimalField
> >>> DecimalField(max_digits=8, decimal_places=7).clean('0E+1')
> Decimal('0E+1')
> }}}
>
> But in django 2.0.13 it raises validator error.
> {{{
> >>> DecimalField(max_digits=8, decimal_places=7).clean('0E+1')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/Users/amokaddim/e/fh/lib/python3.8/site-
> packages/django/forms/fields.py", line 150, in clean
>     self.run_validators(value)
>   File "/Users/amokaddim/e/fh/lib/python3.8/site-
> packages/django/forms/fields.py", line 141, in run_validators
>     raise ValidationError(errors)
> django.core.exceptions.ValidationError: <exception str() failed>
> }}}
>
> This was introduced in
> https://github.com/django/django/commit/7c6590af5f199c9ede970758877b5c1eb7e9b3a6
> #diff-d9609d8dc8482b30eac30df16213cba134562949fd62c97573927b89e880f85b
>
> Is there any way I can prevent this validation error from happening in
> Django 2.0? Any flag or option that will prevent this?
>
> Calling `Decimal('0E+1').normalize()` inside `to_python` method solves
> the problem. But that's a workaround!
>
> Here is a reproducible test case.
>
> {{{#!python
> from unittest import TestCase
> from decimal import Decimal
> from django.core.validators import DecimalValidator
> from django.core.exceptions import ValidationError
>
> class Test(TestCase):
>     def test(self):
>         value = '0E+1'
>         validator = DecimalValidator(8, 7)
>         try:
>             validator(Decimal(value))
>         except ValidationError:
>             self.fail("'{}' is an invalid decimal".format(value))
> }}}

New description:

 In '''1.11.29''' `DecimalValidator` treats `0E+1` as valid decimal

 {{{
 >>> from django.forms.fields import DecimalField
 >>> DecimalField(max_digits=8, decimal_places=7).clean('0E+1')
 Decimal('0E+1')
 }}}

 But in '''2.0.13''' it raises `ValidatorError`.
 {{{
 >>> DecimalField(max_digits=8, decimal_places=7).clean('0E+1')
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/Users/amokaddim/e/fh/lib/python3.8/site-
 packages/django/forms/fields.py", line 150, in clean
     self.run_validators(value)
   File "/Users/amokaddim/e/fh/lib/python3.8/site-
 packages/django/forms/fields.py", line 141, in run_validators
     raise ValidationError(errors)
 django.core.exceptions.ValidationError: <exception str() failed>
 }}}

 This was introduced in
 
https://github.com/django/django/commit/7c6590af5f199c9ede970758877b5c1eb7e9b3a6
 #diff-d9609d8dc8482b30eac30df16213cba134562949fd62c97573927b89e880f85b

 Is there any way I can prevent this validation error from happening in
 Django 2.0? Any flag or option that will prevent this?

 Calling `Decimal('0E+1').normalize()` inside `to_python` method solves the
 problem. But that's a workaround!

 Here is a reproducible test case.

 {{{#!python
 from unittest import TestCase
 from decimal import Decimal
 from django.core.validators import DecimalValidator
 from django.core.exceptions import ValidationError

 class Test(TestCase):
     def test(self):
         value = '0E+1'
         validator = DecimalValidator(8, 7)
         try:
             validator(Decimal(value))
         except ValidationError:
             self.fail("'{}' is an invalid decimal".format(value))
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34014#comment:2>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018342aaa57a-4f84ac7c-b815-49e2-b883-0a050535e3fc-000000%40eu-central-1.amazonses.com.

Reply via email to