Skip to content

Commit 3772b2d

Browse files
committed
Make option_context a ContextDecorator.
This makes it possible to use option_context as a decorator over an entire function, saving an indent level and making it easy to comment out (or in) the context while keeping correct indentation.
1 parent 45fee32 commit 3772b2d

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ Other enhancements
236236
and :class:`~pandas.io.stata.StataWriterUTF8` (:issue:`26599`).
237237
- :meth:`HDFStore.put` now accepts `track_times` parameter. Parameter is passed to ``create_table`` method of ``PyTables`` (:issue:`32682`).
238238
- Make :class:`pandas.core.window.Rolling` and :class:`pandas.core.window.Expanding` iterable(:issue:`11704`)
239+
- Make ``option_context`` a :class:`contextlib.ContextDecorator`, which allows it to be used as a decorator over an entire function.
239240

240241
.. ---------------------------------------------------------------------------
241242

pandas/_config/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"""
5050

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

381381

382-
class option_context:
382+
class option_context(ContextDecorator):
383383
"""
384384
Context manager to temporarily set options in the `with` statement context.
385385

pandas/tests/config/test_config.py

+6
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,12 @@ def eq(val):
410410
self.cf.set_option("a", 17)
411411
eq(17)
412412

413+
@self.cf.option_context("a", 123)
414+
def f():
415+
eq(123)
416+
417+
f()
418+
413419
def test_attribute_access(self):
414420
holder = []
415421

0 commit comments

Comments
 (0)