Skip to content

BUG: Option context applies on __enter__ #8925

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

Merged
Merged
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Bug Fixes




- BUG: Option context applies on __enter__ (:issue:`8514`)



Expand Down
11 changes: 5 additions & 6 deletions pandas/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,18 @@ def __init__(self, *args):
'option_context(pat, val, [(pat, val), ...)).'
)

ops = list(zip(args[::2], args[1::2]))
self.ops = list(zip(args[::2], args[1::2]))

def __enter__(self):
undo = []
for pat, val in ops:
for pat, val in self.ops:
undo.append((pat, _get_option(pat, silent=True)))

self.undo = undo

for pat, val in ops:
for pat, val in self.ops:
_set_option(pat, val, silent=True)

def __enter__(self):
pass

def __exit__(self, *args):
if self.undo:
for pat, val in self.undo:
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,24 @@ def f3(key):
options.c = 1
self.assertEqual(len(holder), 1)

def test_option_context_scope(self):
# Ensure that creating a context does not affect the existing
# environment as it is supposed to be used with the `with` statement.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add here the original issue number?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added link to ticket. Thanks.

# See https://github.com/pydata/pandas/issues/8514

original_value = 60
context_value = 10
option_name = 'a'

self.cf.register_option(option_name, original_value)

# Ensure creating contexts didn't affect the current context.
ctx = self.cf.option_context(option_name, context_value)
self.assertEqual(self.cf.get_option(option_name), original_value)

# Ensure the correct value is available inside the context.
with ctx:
self.assertEqual(self.cf.get_option(option_name), context_value)

# Ensure the current context is reset
self.assertEqual(self.cf.get_option(option_name), original_value)