From 41f475bb67ec24fe266ae18d30d6a6ec56f2ddda Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 17 Oct 2025 13:45:21 -0700 Subject: [PATCH 1/3] BUG: option_context with invalid option --- pandas/_config/config.py | 1 + pandas/tests/config/test_config.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index a78a50138d195..af83a3667578d 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 = () 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 From 4a7625465d3a91a926cf1eb3c7c3b76b6c0d0224 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:12:37 -0700 Subject: [PATCH 2/3] Typing --- pandas/_config/config.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index af83a3667578d..4024222bd1fb2 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -503,14 +503,14 @@ def option_context(*args) -> Generator[None]: ) ops = tuple(zip(args[::2], args[1::2], strict=True)) - undo = () + undo: tuple[tuple[Any, Any], ...] = () try: undo = tuple((pat, get_option(pat)) for pat, val in ops) for pat, val in ops: set_option(pat, val) yield finally: - for pat, val in undo: + for pat, val in undo: # type: ignore[misc] set_option(pat, val) From 6a3e34eb9590c185759b929085e17888eb26dd97 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 17 Oct 2025 14:40:38 -0700 Subject: [PATCH 3/3] Undo one type ignore --- pandas/_config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_config/config.py b/pandas/_config/config.py index 4024222bd1fb2..6d9e2a69abf47 100644 --- a/pandas/_config/config.py +++ b/pandas/_config/config.py @@ -510,7 +510,7 @@ def option_context(*args) -> Generator[None]: set_option(pat, val) yield finally: - for pat, val in undo: # type: ignore[misc] + for pat, val in undo: set_option(pat, val)