diff --git a/pandas/_config/config.py b/pandas/_config/config.py index a78a50138d195..6d9e2a69abf47 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -503,6 +503,7 @@ def option_context(*args) -> Generator[None]: ) ops = tuple(zip(args[::2], args[1::2], strict=True)) + undo: tuple[tuple[Any, Any], ...] = () try: undo = tuple((pat, get_option(pat)) for pat, val in ops) for pat, val in ops: diff --git a/pandas/tests/config/test_config.py b/pandas/tests/config/test_config.py index b704d4a2c6472..9be1bc5554dc7 100644 --- a/pandas/tests/config/test_config.py +++ b/pandas/tests/config/test_config.py @@ -491,3 +491,9 @@ def test_no_silent_downcasting_deprecated(): cf.get_option("future.no_silent_downcasting") with tm.assert_produces_warning(Pandas4Warning, match="is deprecated"): cf.set_option("future.no_silent_downcasting", True) + + +def test_option_context_invalid_option(): + with pytest.raises(OptionError, match="No such keys"): + with cf.option_context("invalid", True): + pass