Skip to content

Commit

Permalink
fix dict iterator methods for flask DynaconfConfig (#581)
Browse files Browse the repository at this point in the history
Co-authored-by: Frank Battaglia <frank.battaglia@gmail.com>
Co-authored-by: Bruno Rocha <rochacbruno@users.noreply.github.com>
  • Loading branch information
3 people committed Jul 23, 2021
1 parent 8e00c62 commit 3223f65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
16 changes: 16 additions & 0 deletions dynaconf/contrib/flask_dynaconf.py
@@ -1,4 +1,5 @@
import warnings
from collections import ChainMap
from contextlib import suppress

try:
Expand Down Expand Up @@ -165,6 +166,21 @@ def __setitem__(self, key, value):
"""
return self._settings.__setitem__(key, value)

def _chain_map(self):
return ChainMap(self._settings, dict(dict.items(self)))

def keys(self):
return self._chain_map().keys()

def values(self):
return self._chain_map().values()

def items(self):
return self._chain_map().items()

def __iter__(self):
return self._chain_map().__iter__()

def __getattr__(self, name):
"""
First try to get value from dynaconf then from Flask Config
Expand Down
8 changes: 8 additions & 0 deletions tests/test_flask.py
Expand Up @@ -101,6 +101,14 @@ def test_flask_dynaconf(settings):

assert "MY_VAR" in app.config
assert "MY_VAR2" in app.config
assert "MY_VAR" in app.config.keys()
assert "MY_VAR2" in app.config.keys()
assert ("MY_VAR", "foo") in app.config.items()
assert ("MY_VAR2", "bar") in app.config.items()
assert "foo" in app.config.values()
assert "bar" in app.config.values()
assert "MY_VAR" in list(app.config)
assert "MY_VAR2" in list(app.config)

with pytest.raises(KeyError):
app.config["NONEXISTENETVAR"]
Expand Down

0 comments on commit 3223f65

Please sign in to comment.