Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unconfigure new settings with settings_stub #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions simple_settings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ def __getattr__(self, attr):
except KeyError:
raise AttributeError('You did not set {} setting'.format(attr))

def __delattr__(self, attr: str) -> None:
self.setup()
try:
del self._dict[attr]
except KeyError:
raise AttributeError('You did not set {} setting'.format(attr))

def add_strategy(self, strategy):
self.strategies += (strategy,)

Expand Down
3 changes: 3 additions & 0 deletions simple_settings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def __enter__(self):
settings.configure(**self.new_settings)

def __exit__(self, ext_type, exc_value, traceback):
transient_stubs = self.new_settings.keys() - self.old_settings.keys()
for stub in transient_stubs:
delattr(settings, stub)
settings.configure(**self.old_settings)


Expand Down
7 changes: 7 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def test_stub_settings_with_context_manager(self, current_settings):

assert current_settings.SIMPLE_STRING == 'simple'

def test_stub_settings_with_transient_setting(self, current_settings):
with settings_stub(TRANSIENT_SIMPLE_STRING='stubbed'):
assert current_settings.TRANSIENT_SIMPLE_STRING == 'stubbed'

with pytest.raises(AttributeError):
current_settings.TRANSIENT_SIMPLE_STRING

def test_stub_settings_with_decorator(self, current_settings):
@settings_stub(SIMPLE_STRING='stubed')
def get_simple_string_from_setting():
Expand Down