Repository: python-babel/babel (python)

Issue:
Repeated suggestions in parse_decimal strict mode 
When you enter 0.00 in languages which use commas for delimiters using the strict mode of `parse_decimal`, you get two times the same suggestion.

<pre><samp>babel.numbers.NumberFormatError: '0.00' is not a properly formatted decimal number. Did you mean '0'? Or maybe '0'?</samp></pre>

Related to [issue 1177](https://github.com/liberapay/liberapay.com/issues/1177) of Liberapay.

Pull request description:
don't repeat suggestions in parse_decimal strict
Don't repeat suggestions for `0.00` in languages
which use commas as delimiters combined with strict
mode.

fixes #596 

Patch:
diff --git a/babel/numbers.py b/babel/numbers.py
index d3d2238..4306e93 100644
--- a/babel/numbers.py
+++ b/babel/numbers.py
@@ -689,6 +689,11 @@ def parse_decimal(string, locale=LC_NUMERIC, strict=False):
         ...
     NumberFormatError: '30.00' is not a properly formatted decimal number. Did you mean '3.000'? Or maybe '30,00'?
 
+    >>> parse_decimal('0.00', locale='de', strict=True)
+    Traceback (most recent call last):
+        ...
+    NumberFormatError: '0.00' is not a properly formatted decimal number. Did you mean '0'?
+
     :param string: the string to parse
     :param locale: the `Locale` object or locale identifier
     :param strict: controls whether numbers formatted in a weird way are
@@ -717,10 +722,16 @@ def parse_decimal(string, locale=LC_NUMERIC, strict=False):
                 ), suggestions=[proper])
             else:
                 proper_alt = format_decimal(parsed_alt, locale=locale, decimal_quantization=False)
-                raise NumberFormatError((
-                    "%r is not a properly formatted decimal number. Did you mean %r? Or maybe %r?" %
-                    (string, proper, proper_alt)
-                ), suggestions=[proper, proper_alt])
+                if proper_alt == proper:
+                    raise NumberFormatError((
+                            "%r is not a properly formatted decimal number. Did you mean %r?" %
+                            (string, proper)
+                    ), suggestions=[proper])
+                else:
+                    raise NumberFormatError((
+                        "%r is not a properly formatted decimal number. Did you mean %r? Or maybe %r?" %
+                        (string, proper, proper_alt)
+                    ), suggestions=[proper, proper_alt])
     return parsed
 
 


Test patch:
diff --git a/tests/test_numbers.py b/tests/test_numbers.py
index 9c3896c..d0f24bd 100644
--- a/tests/test_numbers.py
+++ b/tests/test_numbers.py
@@ -182,6 +182,10 @@ class NumberParsingTestCase(unittest.TestCase):
         with self.assertRaises(numbers.NumberFormatError) as info:
             numbers.parse_decimal('0,,000', locale='en_US', strict=True)
         assert info.exception.suggestions == ['0']
+        # Return only suggestion for 0 on strict
+        with self.assertRaises(numbers.NumberFormatError) as info:
+            numbers.parse_decimal('0.00', locale='de', strict=True)
+        assert info.exception.suggestions == ['0']
         # Properly formatted numbers should be accepted
         assert str(numbers.parse_decimal('1.001', locale='de', strict=True)) == '1001'
         # Trailing zeroes should be accepted