diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 4605c14643fa2..4d29bd3a9bede 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -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`). .. --------------------------------------------------------------------------- diff --git a/pandas/_config/config.py b/pandas/_config/config.py index 8955a06187109..f5e16cddeb04c 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -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 @@ -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. diff --git a/pandas/tests/config/test_config.py b/pandas/tests/config/test_config.py index 51640641c78e6..4060ac1735c1b 100644 --- a/pandas/tests/config/test_config.py +++ b/pandas/tests/config/test_config.py @@ -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) + def f(): + eq(123) + + f() + def test_attribute_access(self): holder = []