Repository: sloria/environs

Issue:
Inconsistent behavior with `env.list`
When `MY_ENV_VAR` is __not__ defined,

```py
env.int("MY_ENV_VAR", default=None)
```

returns `None` (as expected), but:

```py
env.list("MY_ENV_VAR", subcast=str, default=None)
```

yields the following Exception:

```py
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/environs/__init__.py", line 122, in method
    value = preprocess(value, **preprocess_kwargs)
  File "/usr/local/lib/python3.10/site-packages/environs/__init__.py", line 206, in _preprocess_list
    return typing.cast(str, value).split(delimiter) if value != "" else []
AttributeError: 'NoneType' object has no attribute 'split'
```

I believe the second example should return `None` as well.

Thoughts?

Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 37487c8..b5264fd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,11 @@ Features:
 - `Env.__repr__` and `Env.__str__` no longer display environment variables, to prevent accidental exfiltration of sensitive data ([#292](https://github.com/sloria/environs/issues/292)).
   Thanks [mro-rhansen2](https://github.com/mro-rhansen2) for the suggestion.
 
+Bug fixes:
+
+- Fix passing `None` as default to `env.list` ([#298](https://github.com/sloria/environs/issues/298)).
+  Thanks [lucas-bremond](https://github.com/lucas-bremond) for reporting.
+
 ## 10.2.0 (2024-01-09)
 
 Features:
diff --git a/environs/__init__.py b/environs/__init__.py
index b52774d..3fc16ca 100644
--- a/environs/__init__.py
+++ b/environs/__init__.py
@@ -201,7 +201,7 @@ def _make_list_field(*, subcast: typing.Optional[type], **kwargs) -> ma.fields.L
 def _preprocess_list(
     value: typing.Union[str, typing.Iterable], *, delimiter: str = ",", **kwargs
 ) -> typing.Iterable:
-    if ma.utils.is_iterable_but_not_string(value):
+    if ma.utils.is_iterable_but_not_string(value) or value is None:
         return value
     return typing.cast(str, value).split(delimiter) if value != "" else []
 


Test patch:
diff --git a/tests/test_environs.py b/tests/test_environs.py
index 95484e9..e3248eb 100644
--- a/tests/test_environs.py
+++ b/tests/test_environs.py
@@ -82,12 +82,16 @@ class TestCasting:
         set_env({"LIST": "1,2,3"})
         assert env.list("LIST") == ["1", "2", "3"]
 
-    def test_list_with_default_from_string(self, set_env, env):
+    def test_list_with_default_from_string(self, env):
         assert env.list("LIST", "1,2") == ["1", "2"]
 
-    def test_list_with_default_from_list(self, set_env, env):
+    def test_list_with_default_from_list(self, env):
         assert env.list("LIST", ["1"]) == ["1"]
 
+    # https://github.com/sloria/environs/issues/298
+    def test_list_with_default_none(self, env):
+        assert env.list("LIST", default=None) is None
+
     def test_list_with_subcast(self, set_env, env):
         set_env({"LIST": "1,2,3"})
         assert env.list("LIST", subcast=int) == [1, 2, 3]