Repository: joke2k/django-environ
Pull request #318 (2021-09-04 15:50:17)

Add support for negative float strings
Replaces https://github.com/joke2k/django-environ/pull/276
Closes https://github.com/joke2k/django-environ/issues/160


Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 3b251fa..5888609 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -7,6 +7,11 @@ and this project adheres to `Semantic Versioning <http://semver.org/spec/v2.0.0.
 
 `v0.7.0`_ - 00-Unreleased-2021
 ------------------------------
+Added
++++++
+  - Added support for negative float strings
+    (`#160 <https://github.com/joke2k/django-environ/issues/160>`_)
+
 Changed
 +++++++
  - Added 'Funding' and 'Say Thanks!' project urls on pypi.
diff --git a/environ/environ.py b/environ/environ.py
index 9561f19..29b5f7f 100644
--- a/environ/environ.py
+++ b/environ/environ.py
@@ -413,10 +413,10 @@ class Env:
             value = tuple([x for x in val if x])
         elif cast is float:
             # clean string
-            float_str = re.sub(r'[^\d,\.]', '', value)
+            float_str = re.sub(r'[^\d,.-]', '', value)
             # split for avoid thousand separator and different
             # locale comma/dot symbol
-            parts = re.split(r'[,\.]', float_str)
+            parts = re.split(r'[,.]', float_str)
             if len(parts) == 1:
                 float_str = parts[0]
             else:


Test patch:
diff --git a/tests/fixtures.py b/tests/fixtures.py
index a476e29..2de0c39 100644
--- a/tests/fixtures.py
+++ b/tests/fixtures.py
@@ -36,6 +36,7 @@ class FakeEnv:
                     FLOAT_COMMA_VAR='33,3',
                     FLOAT_STRANGE_VAR1='123,420,333.3',
                     FLOAT_STRANGE_VAR2='123.420.333,3',
+                    FLOAT_NEGATIVE_VAR='-1.0',
                     BOOL_TRUE_VAR='1',
                     BOOL_TRUE_VAR2='True',
                     BOOL_FALSE_VAR='0',
diff --git a/tests/test_env.py b/tests/test_env.py
index c4807de..b85a539 100644
--- a/tests/test_env.py
+++ b/tests/test_env.py
@@ -75,13 +75,19 @@ class TestEnv:
     def test_int_with_none_default(self):
         assert self.env('NOT_PRESENT_VAR', cast=int, default=None) is None
 
-    def test_float(self):
-        assert_type_and_value(float, 33.3, self.env('FLOAT_VAR', cast=float))
-        assert_type_and_value(float, 33.3, self.env.float('FLOAT_VAR'))
-
-        assert_type_and_value(float, 33.3, self.env('FLOAT_COMMA_VAR', cast=float))
-        assert_type_and_value(float, 123420333.3, self.env('FLOAT_STRANGE_VAR1', cast=float))
-        assert_type_and_value(float, 123420333.3, self.env('FLOAT_STRANGE_VAR2', cast=float))
+    @pytest.mark.parametrize(
+        'value,variable',
+        [
+            (33.3, 'FLOAT_VAR'),
+            (33.3, 'FLOAT_COMMA_VAR'),
+            (123420333.3, 'FLOAT_STRANGE_VAR1'),
+            (123420333.3, 'FLOAT_STRANGE_VAR2'),
+            (-1.0, 'FLOAT_NEGATIVE_VAR'),
+        ]
+    )
+    def test_float(self, value, variable):
+        assert_type_and_value(float, value, self.env.float(variable))
+        assert_type_and_value(float, value, self.env(variable, cast=float))
 
     def test_bool_true(self):
         assert_type_and_value(bool, True, self.env('BOOL_TRUE_VAR', cast=bool))
diff --git a/tests/test_env.txt b/tests/test_env.txt
index f00ce49..711639f 100644
--- a/tests/test_env.txt
+++ b/tests/test_env.txt
@@ -17,6 +17,7 @@ FLOAT_VAR=33.3
 FLOAT_COMMA_VAR=33,3
 FLOAT_STRANGE_VAR1=123,420,333.3
 FLOAT_STRANGE_VAR2=123.420.333,3
+FLOAT_NEGATIVE_VAR=-1.0
 PROXIED_VAR=$STR_VAR
 EMPTY_LIST=
 INT_VAR=42