Repository: rochacbruno/dynaconf
Pull request #176 (2019-04-23 10:22:05)

Fix `False` not being an acceptable env
When using `ENVVAR_PREFIX_FOR_DYNACONF=False` in LazySettings in combination with for example a TOML settings file, the base loader will raise an unexpected exception trying to `.lower`case the boolean. This MR appends `DYNACONF` as compatibility global_env instead when the `ENVVAR_PREFIX_FOR_DYNACONF` is `False`.

**Steps to reproduce the issue:**

1. Create TOML settings file `settings.toml` with the following contents:
  ```toml
  [global]
  var = 1
  ```
2. Try instantiating with `ENVVAR_PREFIX_FOR_DYNACONF=False`, and accessing an arbitrary value from the settings like so:

```python
settings = LazySettings(
    ENVVAR_PREFIX_FOR_DYNACONF=False, 
    SETTINGS_FILE_FOR_DYNACONF="settings.toml"
)
assert settings.VAR == 1
```

I added testcases to ensure this undefined behavior is properly prevented in the future. 

Cheers! 👋 

pep8speaks: Hello @janw! Thanks for opening this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:



* In the file [`tests/test_envvar_prefix.py`](https://github.com/rochacbruno/dynaconf/blob/93497a34fb956f8055c15fb0eb1cc6cf258d5897/tests/test_envvar_prefix.py):

> [Line 22:80](https://github.com/rochacbruno/dynaconf/blob/93497a34fb956f8055c15fb0eb1cc6cf258d5897/tests/test_envvar_prefix.py#L22): [E501](https://duckduckgo.com/?q=pep8%20E501) line too long (81 > 79 characters)


codecov[bot]: # [Codecov](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=h1) Report
> Merging [#176](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=desc) into [master](https://codecov.io/gh/rochacbruno/dynaconf/commit/1bb2c39ae6a39cbbd32ca8ef39ef7ea5732a9418?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/rochacbruno/dynaconf/pull/176/graphs/tree.svg?width=650&token=GXszVzJcqW&height=150&src=pr)](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=tree)

```diff
@@          Coverage Diff          @@
##           master   #176   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          18     18           
  Lines         705    705           
=====================================
  Hits          705    705
```


| [Impacted Files](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [dynaconf/loaders/base.py](https://codecov.io/gh/rochacbruno/dynaconf/pull/176/diff?src=pr&el=tree#diff-ZHluYWNvbmYvbG9hZGVycy9iYXNlLnB5) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=footer). Last update [1bb2c39...c26ea4e](https://codecov.io/gh/rochacbruno/dynaconf/pull/176?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

rochacbruno: good catch! :100: 

Patch:
diff --git a/dynaconf/loaders/base.py b/dynaconf/loaders/base.py
index 8a544fd..375fefe 100644
--- a/dynaconf/loaders/base.py
+++ b/dynaconf/loaders/base.py
@@ -68,7 +68,7 @@ class BaseLoader(object):
 
         # compatibility with older versions that still uses [dynaconf] as
         # [default] env
-        global_env = self.obj.get("ENVVAR_PREFIX_FOR_DYNACONF")
+        global_env = self.obj.get("ENVVAR_PREFIX_FOR_DYNACONF") or "DYNACONF"
         if global_env not in env_list:
             env_list.append(global_env)
 


Test patch:
diff --git a/tests/test_envvar_prefix.py b/tests/test_envvar_prefix.py
new file mode 100644
index 0000000..6e6fdab
--- /dev/null
+++ b/tests/test_envvar_prefix.py
@@ -0,0 +1,43 @@
+import os
+
+import pytest
+
+from dynaconf import LazySettings
+
+TOML = """
+[global]
+var = "my value"
+
+[false]
+thisvar = "should not be set"
+"""
+
+
+def test_envvar_prefix_lazysettings(tmpdir):
+    os.environ["DYNACONF_PREFIXED_VAR"] = "this is prefixed"
+    tmpfile = tmpdir.mkdir("sub").join("test_no_envvar_prefix.toml")
+    tmpfile.write(TOML)
+
+    settings = LazySettings(
+        ENVVAR_PREFIX_FOR_DYNACONF=False,
+        SETTINGS_FILE_FOR_DYNACONF=str(tmpfile),
+    )
+
+    assert settings.VAR == "my value"
+    assert settings.DYNACONF_PREFIXED_VAR == "this is prefixed"
+
+
+def test_envvar_prefix_false_from_envvar(tmpdir):
+    os.environ["DYNACONF_PREFIXED_VAR"] = "this is prefixed"
+    os.environ["ENVVAR_PREFIX_FOR_DYNACONF"] = "false"
+    tmpfile = tmpdir.mkdir("sub").join("test_no_envvar_prefix.toml")
+    tmpfile.write(TOML)
+
+    settings = LazySettings(SETTINGS_FILE_FOR_DYNACONF=str(tmpfile))
+
+    assert settings.VAR == "my value"
+    assert settings.DYNACONF_PREFIXED_VAR == "this is prefixed"
+
+    with pytest.raises(AttributeError):
+        assert settings.THISVAR == "should not be set"
+    del os.environ["ENVVAR_PREFIX_FOR_DYNACONF"]