Skip to content

Commit 238e04f

Browse files
authored
Make option_context a ContextDecorator. (#34253)
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 0f2ca37 commit 238e04f

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-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 (:issue:`34253`).
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

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

413+
# Test that option_context can be used as a decorator too (#34253).
414+
@self.cf.option_context("a", 123)
415+
def f():
416+
eq(123)
417+
418+
f()
419+
413420
def test_attribute_access(self):
414421
holder = []
415422

0 commit comments

Comments
 (0)