Skip to content

Make option_context a ContextDecorator. #34253

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 1 commit into from
May 25, 2020
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Other enhancements
and :class:`~pandas.io.stata.StataWriterUTF8` (:issue:`26599`).
- :meth:`HDFStore.put` now accepts `track_times` parameter. Parameter is passed to ``create_table`` method of ``PyTables`` (:issue:`32682`).
- Make :class:`pandas.core.window.Rolling` and :class:`pandas.core.window.Expanding` iterable(:issue:`11704`)
- Make ``option_context`` a :class:`contextlib.ContextDecorator`, which allows it to be used as a decorator over an entire function (:issue:`34253`).

.. ---------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"""

from collections import namedtuple
from contextlib import contextmanager
from contextlib import ContextDecorator, contextmanager
import re
from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type, cast
import warnings
Expand Down Expand Up @@ -379,7 +379,7 @@ def __doc__(self):
# Functions for use by pandas developers, in addition to User - api


class option_context:
class option_context(ContextDecorator):
"""
Context manager to temporarily set options in the `with` statement context.
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ def eq(val):
self.cf.set_option("a", 17)
eq(17)

# Test that option_context can be used as a decorator too (#34253).
@self.cf.option_context("a", 123)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment with this issue number here (and a comment on what we are testing here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

def f():
eq(123)

f()

def test_attribute_access(self):
holder = []

Expand Down