Skip to content

Commit ecef6d0

Browse files
jayfoadjreback
authored andcommitted
Raise OptionError instead of KeyError in __getattr__. Fixes #19789. (#19790)
1 parent 26dd5b1 commit ecef6d0

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -909,3 +909,4 @@ Other
909909
^^^^^
910910

911911
- Improved error message when attempting to use a Python keyword as an identifier in a ``numexpr`` backed query (:issue:`18221`)
912+
- Bug in accessing a :func:`pandas.get_option`, which raised ``KeyError`` rather than ``OptionError`` when looking up a non-existant option key in some cases (:issue:`19789`)

pandas/core/config.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ def __getattr__(self, key):
196196
if prefix:
197197
prefix += "."
198198
prefix += key
199-
v = object.__getattribute__(self, "d")[key]
199+
try:
200+
v = object.__getattribute__(self, "d")[key]
201+
except KeyError:
202+
raise OptionError("No such option")
200203
if isinstance(v, dict):
201204
return DictWrapper(v, prefix)
202205
else:

pandas/tests/test_config.py

+6
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,9 @@ def test_option_context_scope(self):
428428

429429
# Ensure the current context is reset
430430
assert self.cf.get_option(option_name) == original_value
431+
432+
def test_dictwrapper_getattr(self):
433+
options = self.cf.options
434+
# GH 19789
435+
pytest.raises(self.cf.OptionError, getattr, options, 'bananas')
436+
assert not hasattr(options, 'bananas')

0 commit comments

Comments
 (0)